Can we include C source file in another way? - c

I am doing a bare metal project on Cyclone V and now I am trying to make a bare metal application in C. However, I have some issue in including source file.
For example, I use the function alt_fpga_state_get() from alt_fpga_manager.h but it gives me error message
"undefined reference to 'atl_fpga_state_get()'
Knowing that it needs a source file containing the function, I import the source file with no problem. However, here come another thing that inside alt_fpga_manager.c it also gives an error message
"undefined reference to 'alt_clock_is_enable'
Then I have to import source file for alt_clock_manager.h and the problem keep going on like that until I end up import the whole src folder. After that all the "undefined reference" problem are solved but it come with another problem telling me that my OCRAM is overflowed ( I think because of adding many source file ).
I would like to know if there is any solution for this because keep importing source file is not a convenient way to do. I did look at some examples and I found that in their makefile having this line
HWLIBS_SRC := alt_reset_manager.c alt_clock_manager.c alt_spi.c alt_globaltmr.c alt_timers.c alt_watchdog.c
I think it is the way they include the source file but I am not sure. Hope someone can give me a clue to solve this problem.
Thanks !

when you #include an header file you get the function declarations (etc depending on header file contents), that does not define them, you missed to link with needed object or lib files
Example :
main.c is
#include "f.h"
int main()
{
f();
}
f.h is
extern void f();
f.c is
void f() {}
If I just consider main.c :
/tmp % gcc main.c
/tmp/ccFs7Gyz.o: In function `main':
main.c:(.text+0xa): undefined reference to `f'
collect2: ld returned 1 exit status
But also considering f.c :
/tmp % gcc main.c f.c
Of course can also do in several steps
/tmp % gcc -c f.c
/tmp % gcc main.c f.o
etc
P.S. do not #include a source file, so no #include "f.c" in mainc.c

Related

ld returned 1 exit status - c file structure

I am editing an open source C project to customize it for my usage. Basically it has the following file structure:
|--- /include ----- loremipsum.h
|
/loremipsum ----- |--- /ex ----- loremipsum.c and more
|
|--- /src ----- other helpful functions
In loremipsum.h one can find a list of functions that are defined in /src, via
#ifdef __cplusplus
extern "C" {
#endif
but this has not been edited by me, so i suppose it should work.
In loremipsum.c there are:
#include "loremipsum.h"
plenty of extern void function_defined_in_ex()(1)
some more definitions.
main
After editing the c-files and trying to recreate a new exefile via
$ gcc loremipsum.c -o customloremipsum.exe
first I got an error, that loremipsum.h was not found, so I changed the include to "path/to/include/loremipsum.h", I did that in all .c files with that include.
Then, all of the (1) had an error, namely
"/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccBt2CkE.o:loremipsum.c:(.text+0x3745): undefined reference to function_defined_in_ex'
ld returned 1 exit status".
I really can't figure out, why.
I think the first include error is already strange, in my understanding, there should be no problem to find the .h file. Also, I can't figure out, how to make clear where to find the functions in (1).
I'm very sorry for that long post, I'm just afraid, I'm forgetting something fundamental.
I am working on Windows 10 and compiling in a Cygwin shell.
Thank you for your help.
You need to tell the compiler where to look for include files using the -I parameter, for example:
gcc -I../include loremipsum.c -o customloremipsum.exe
Don't specify the full path in the #include lines.
Any undefined references you get are cause by the linker because the .c files or objects or libraries defining the symbols are not compiled in yet.
Also, it's better to split compilation and linking steps to help you where things go wring. Like this:
gcc -c -I../include loremipsum.c -o customloremipsum.o
gcc customloremipsum.o -o customloremipsum.exe

Building against ancient C Object

I have a project I'm working on that has code from 1988 and is on Solaris Sparc machines.
I'm not a C programmer, but there is a header file and object file for the old code but no sourcecode.
I can live without it, if I can use the functions in the old object file to plug stuff into and prod the outputs.
/usr/sfw/bin/gcc -Wall -o test main.c
Outputs
Undefined first referenced
symbol in file
KES_ld_kb /var/tmp//cccjn5UP.o
Is there anything I can do to get gcc to build against the object so I can run some of this old code?
-- Edit --
got asked what the code looks like:
#include "kes.h"
#include <stdio.h>
int main(){
KES_ld_kb("/tmp/kb.kb", 50000L);
return 0;
}
-- Another Edit --
I have original .o and .h files, these are SPARC based and I'm doing my work on an old SPARC VM, so that much I do know.
I tried to create an archive with
ar ruv libkes.a kes.o
Which when I then build it returns
bash-3.2$ /usr/sfw/bin/gcc -L/export/home/zmmyks/ps -Wall -o test main.c -lkes
ld: warning: file /export/home/zmmyks/ps/libkes.a ignored: unable to locate archive symbol table
I really don't think you can live without the implementation of functions. A header file serves as a blueprint for the source, but it is not a replacement for it. If KES_ld_kb is defined in those missing source files you need to track those down or this will never work.

Using a function from another C file placed in another directory?

Say I have a parent directory A with two subdirectories B and C.
Sub-directory C has a helper.c and helper.h as shown:
//helper.c
void print(){
printf("Hello, World!\n");
}
//helper.h
void print();
Now, in sub directory B, I have a main.c which just calls the print function:
//main.c
#include<stdio.h>
#include"../C/helper.h"
void main(){
print();
}
I tried the following commands for compiling main.c:
Command 1: gcc main.c //Gives undefined reference to 'print' error
Command 2: gcc main.c ../C/helper.c //Compiles successfully
Now I removed the #include"../C/helper.h" from main .c and tried the Command 2 again. It still works.
So I have the following questions:
i) What difference does it make whether the helper.h file is included or
helper.c?
ii) Why command 1 fails?
iii) Is there a way to compile my C program without having to specify
helper.c everytime?
What happens when you execute:
Command 1: gcc main.c //Gives undefined reference to 'print' error
When execute gcc main.c
Compiler compiles main.c and creates objective file. This file will contain unresolved link to function print(). Because there is no implementation of function print() in main.c file.
After compilation gcc tries to make full executable file. To do this gcc combines all objective files and tries to resolve all unresolved links. As you remember there is unresolved link for function print(), gcc can't find implementation and raise the error.
When you execute
Command 2: gcc main.c ../C/helper.c //Compiles successfully
gcc compiles both files. Second file ../C/helper.c contains implementation of function print(), so linker can find it and resolve reference to it in function main().
i) What difference does it make whether the helper.h file is included or helper.c?
In your case helper.h contains forward declaration of function print(). This gives information to compiler how to make call of function print().
ii) Why command 1 fails?
See above.
iii) Is there a way to compile my C program without having to specify helper.c everytime?
Use make utility. Compile helper.c in separate objective file helper.o and use it in linkage command.
helper.o: ../C/helper.c ../C/helper.h
gcc -c ../C/helper.c
main.o: main.c main.h
gcc -c main.c
testprog: main.o helper.o
g++ main.o helper.o -o testprog
See make utility manual for details.
Commands should be indented by TAB.
First you need to understand that #include simply adds whatever text is in the #include parameter to the position in the file the statement is in, for example:
//file1.h
void foo();
//main.c
#include "file1.txt"
int main(int argc, char **argv)
{
foo();
return 0;
}
Will cause the pre-compilation to generate this unified file for compilation:
//main.c.tmp
void foo();
int main(int argc, char **argv)
{
foo();
return 0;
}
So to answer your first and second questions:
When you include a header file (or any file) that only contains declarations (i.e function signatures) without definitions (i.e function implementations), as in the example above, the linker will fail in finding the definitions and you will get the 'undefined reference' error.
When you include a c code file (or any file) that contains definitions, these definitions will be merged to your code and the linker will have them, that's why it works.
and as for your third question
It is bad practice to include c files directly in other c files, the common approach is to keep separate c files with headers exposing the functionality they provide, include the header files and link against the compiled c files, for example in your case:
gcc main.c helper.c -o out
Will allow you to include helper.c in main.c and still work because you instructed the compiler to compile both files instead of just main.c so when linking occurs the definitions from the compilation will be found and you will not get the undefined behavior error
This is, in a nutshell. I abstracted a lot of what's going on to pass on the general idea. this is a nice article describing the compilation process in fair detail and this is a nice overview of the entire process.
I'll try to answer:
i) What difference does it make whether the helper.h file is included or helper.c?
When you include a file, you don't want to expose your implementation, hence its better to include h files, that contains only the "signatures" - api of your implementation.
ii) Why command 1 fails?
When you compile you must add all your resources to the executable, otherwise he won't compile.
iii) Is there a way to compile my C program without having to specify
helper.c everytime?
You can use Makefile to compile your program. Maybe this tutorial can help you.
i) What difference does it make whether the helper.h file is included
or helper.c?
Including helper.c means that helper.c gets compiled each time as if it were part of main.c
Including helper.h lets the compiler know what argument types the function print() takes and returns so the compiler can give an error or warning if you call print() incorrectly
ii) Why command 1 fails?
The compiler is not being told where to find the actual code for the print function. As explained, including the .h file only helps the compiler with type checking.
iii) Is there a way to compile my C program without having to specify
helper.c everytime?
You can compile it once into an object file and optionally you can add that obj to a static or dynamically loaded library. You still need to help the compiler find that obj or library. For example,
gcc -c helper.c
gcc main.c helper.o
The correct way to avoid compiling modules that don't need compiling is to use a Makefile. A Makefile compares when a module was last compiled compared to when it was last modified and that way it knows what needs to be compiled and what doesn't.

How to organize Header files

Using header files in this way gives me the error "undefined reference to somefunc". What is the proper way to make sure somefunc.c is seen so this error doesn't occur? It seems simply including somefile.h in main.c isn't enough to see the definitions in somefile.c
main.c
#include "somefile.h"
int main() {
somefunc();
return 0;
}
somefile.h
#ifndef SOMEFILE_H
#define SOMEFILE_H
void somefunc();
#endif
somefile.c
#include <stdio.h>
#include "somefile.h"
void somefunc() {
printf("hello\n");
}
I don't understand why I am getting errors because this is the same manner in which they are used in tutorials and videos i've been viewing while looking for an answer. The code above is an answer given earlier but it is still has the same error.
Undefined reference to somefunc is a linker error, not a compiler error.
This means that, although when compiling main.c the header somefile.h is found, you are not compiling the file somefile.c together with main.c. So when linking occurs the linker is not able to find the implementation of somefunc in any object file to resolve the call from main().
If you are using GCC or Clang just compile both source files to your command, eg
gcc somefunc.c main.c -o output
If you are using an IDE instead, make sure that somefile.c is compiled together with main.c when building the application.
This doesn't appear to be a problem with the header file.
This appears to be a problem in linking, which depends on how you build the project. If you use an IDE, it means that somefile.c is not included in the project. If you're using make and a makefile, it means that somefile.c is not listed in the makefile, or at least not included for the linker. If you're building at the command line (not using make or some build tool, but using gcc), then you're not including somefile.c in the command.
The undefined reference error means the linker couldn't find the code in somefile.c, because the linker didn't know to include it.

Building object files that depends on other object files

EDITS: Including link to my makefile
I have a main program that calls a bunch of functions defined in other source files. This is not a problem because I am using cc -c functionSource.c functionHeader.h and generating object files before compiling my main program with cc main.c func1.o func2.o .... -o test.o
I am having problems when one of my functions depends on another function.
For example:
My main program calls an shuffle function which is defined in it's own source file and the shuffle function calls a swap function which in turn is defined in it's own source file.
When i try to generate the shuffle.o file for my main program using cc -c shuffle.o I get an undefined reference to swap error.
When I try to cc shuffle.c swap.o i get an undefined reference to main error.
Here is my makefile
How do I go about fixing this?
Found the problem. I had a swap function declared inside insertionSort.h and shuffle.h but no implementations.
Have a look to the man page: '-c' makes the compiler generating object files only (not trying to link).
Do the following:
cc -c insertionSort.c # => gives insertionSort.o
cc -c -c functionSource.c # => gives functionSource.o
cc insertionSort.o functionSource.o ...and-so-on... main.c -o test
It's not necessary to specify header files - it doesn't help.
BTW: If you have mor than one implementation file, it is rather useful
(a) to learn make
(b) stick to the convention that object files and programs should be named like th sources.
E.g:
foo.c => foo.o
bar.c => bar
etc - you get the picture.
This has nothing to do with make. You need to get a book on introductory C programming, that will explain how to use the preprocessor, and you need to examine the documentation for your compiler so you understand what the different compiler flags do (such as when you want to use the -c flag and when you don't, and what the difference is).
It's wrong to include header files (.h files) on the compile line. Only source files (.c) should be included on the command line when building object (.o) files. You should be adding the headers you need into your .c files using the #include directive: #include "insertionSort.h". If you're missing a reference to a function, then #include the header file that declares that function: #include "swap.h".

Resources