- “Undefined reference ” to a function defined in static library
- In Library to be called from application:
- In Application:
- undefined reference to `main’ in C
- 5 Answers 5
- undefined reference to `main’ error in gcc 4.7
- 2 Answers 2
- GCC : undefined reference to “__asan_init_v1”
- 1 Answer 1
- How to fix undefined reference to `_imp__pthread_create’
- 1 Answer 1
“Undefined reference ” to a function defined in static library
I am trying to build a Library to use in an application. I built the library as below, and when i compile the application i get the below error:
I have done the beolw things.
In Library to be called from application:
Here i have lot of modules, but entry point to this library is func() (i.e., main () is replaced with func() so that i can call the module, also func () is not declared as ‘static’.)
In one of files:
Then built the Library as:
Also used ranlib and nm -s on libfun.a seperately to build symbol table, but the total size of archive did not change after using these commands and still got the linking error. Here $(OBJS) contains all the object files
In Application:
extern int func ();
Then i get the below error:
I tried to build symbol table with «ar s» and «ranlib» but the results is same.
One thing i observed is there is a difference in contents of «ar» which i built and the archives already present in project for other modules.
The archive built by me contains (ouput with «nm -s libfun.a» ):
But the other archives which i am using without any changes contain below strange pattern:
I am not sure what is the difference above. Is it a shared Library or a Static library ?
I am trying to compile with GCC and build archive with ‘ar’, but the other library files may be using g++ compiler. I am not sure. Just in case it matters.
What am i doing wrong here in building my library ? Please help?
undefined reference to `main’ in C
Hi I am getting below error while compiling a c code using gcc
I am trying to import the fftw() function into SystemVerilog. Here is my code
Here is a system Verilog code from where I am trying to call fftw
Here is an irun command to compile both systemverilg and C files
while running this command give below error: building library run.so ld: /home/fftw/local/lib/libfftw3.a(mapflags.o): relocation R_X86_64_32 against `.rodata’ can not be used when making a shared object; recompile with -fPIC /homefftw/local/lib/libfftw3.a: could not read symbols: Bad value collect2: ld returned 1 exit status make: * [/home/ss69/DPI/./INCA_libs/irun.lnx8664.12.20.nc/librun.so] Error 1 ncsc_run: *E,TBBLDF: Failed to build test library /home/DPI/./INCA_libs/irun.lnx8664.12.20.nc/librun.so
irun: *E,CCERR: Error during cc compilation (status 1), exiting.
When I simply try to compile C using gcc with below command:
gcc -g -Wall -Werror -I/home/fftw/local/include -L/home/ss69/fftw/local/lib \ fftw_test_DPI.c -lfftw3 -lm -o fftw_test_DPI
I get this error:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../lib64/crt1.o: In function _start’: (.text+0x20): undefined reference to main’ collect2: ld returned 1 exit status
5 Answers 5
Exactly how are you using the function void fftw(double FFT_in[],int size) from your comments it sounds like you are coding routine that is called as DLL or as part of a static library.
If this is the case then adding main() isn’t going to help, at all.
What you have written is ABSOLUTELY 100% OK, if it is to be used as a callable routine.
What you might need to do is compile this routine into a library, even a static lib. is probably ok. If this is the case then consult your GCC documentation on how to create a static or dynamic lib.
Finally, I have written Verilog code myself, so you can also provide any lines or references to Verilog documentation that you have read and whose instructions you are following. I assume that at some point you are invoking Verilog and supplying it with a list of libraries it can/should use. Your lib should be included in that list.
Am including comments from jxh per his request: To import the function into SystemVerilog, you need to compile your function into a shared object. Then, you would point SystemVerilog at the shared object. (I don’t use SystemVerilog, but that is what I gather from its web page.)
undefined reference to `main’ error in gcc 4.7
I created a program in C and I tried to compile it. When I use my gcc 4.8.1 compiler in Widows everything worked and my program too.
I compiled with the following arguments:
But in linux I getting the following error:
Why is that? My programm is working and I can’t understand why I getting compiling errors in linux.
2 Answers 2
You used -static option, which modifies the way executable is linked.
I was unable to reproduce your exact error message, but on my Linux it says that it is unable to link with -lc in static mode, and under my OSX it says that it is unable to locate -lcrt0.o . For me in both case, this means that the system is unable to locate the static stub.
If you remove -static it should work. If not, your problem is very strange.
The error you show indicates the linker is not finding the main() function in your code. As it is evident that you have included it in the source file, it is also evident you are not compiling with that command line (or you are compiling in other directory where you have a non-main() source called children.c , perhaps the build system makes a touch children.c if it doesn’t find the source, and then compiles it —on that case it will not have a main() routine). Check that the files are properly created and where, as I think you aren’t compiling that file anyway.
Try to use simple options before you go to more complicated ones. Try something like:
before trying to experiment with optimization or static linking anyway. Also, dynamic linking is normally better than static, so you’ll get smaller executables (8Kb vs. 800Kb, and multiple copies of libc loaded per executable). Also, you don’t need to include -lm as you aren’t using any of the functions (having it doesn’t hurt anyway).
I have compiled your source with the following command line without any problem, but I do have support for statically linked executables and perhaps you don’t (the command line I have put above would work in any linux, I suppose)
GCC : undefined reference to “__asan_init_v1”
I want to compile my C99 project with the flag -fsanitize=address . I need to use CMake and to compile with gcc on a Centos 7 distribution.
I currently have gcc 7.3.1 and cmake 3.13.4.
I added the -fsanitize=address flag in the compilation flags list and in the linker, but it still fails to compile with the following error:
Here is my CMakeLists.txt file, I can’t find what I am missing..
1 Answer 1
Your toolchain is somewhat confused about which version of GCC it is and the linkage failure is one consequence of that confusion.
You tell us you are using GCC 7.3.1, but the first of the unresolved references to __asan_init_v1 :
shows that the gcc that invoked the linkage with -fsanitize=address linked the address santitizer initialization code from your installation of GCC 4.8.5.
In GCC 7, the file libasan_preinit.o makes no references to __asan_init_v1 , as seen on the laptop I’m working on just now:
It calls __asan_init , which is indeed defined in the toolchain’s libasan :
On the other hand, in GCC 4.8.5, libasan did not define the function __asan_init and did define __asan_init_v1 :
There, __asan_init was just an internal #define aliasing the real function __asan_init_v1 , as per libsanitizer/asan/asan_interface_internal.h , but in that file in GCC 7
it is a real function, as reported by nm .
So the thrust of the evidence is that you are linking against the libasan of GCC 7.3.1 but also linking the library initialization code of GCC 4.8.5. I have no visibility of how you’ve landed in this GCC version mash-up — could be a packaging bug, for all I know — but you’ll have to fix that to solve these linkage failures.
How to fix undefined reference to `_imp__pthread_create’
I use MinGW on windows7 32bit. And I can’t compile my source which uses pthread.
My code is below.
Error happens as I compile it.
I use following pthread library.
And here is libpthread.dll.a in /usr/local/lib
Does anyone know how to fix this problem?
1 Answer 1
does not make sense.
-L is a linker option that directs the linker to search for required libraries in directory . Thus you are telling the linker to search for required libraries in path /usr/local/lib/libpthread.dll.a , which is not a directory, while on the other hand you are not telling the linker to link any libraries at all. That is why it fails to find any definition for _imp__pthread_create .
Neither does the program you have posted make sense. The lines:
If I have not defined a preprocessor macro pthread_create then compile:
Well if you had defined a preprocessor macro pthread_create , e.g.
then the code you would compile would be:
And since you have indeed not defined any such macro, the code you compile is:
which fails at linkage, as you see. And if that code was compiled with pthread_create so defined, it would be:
Rewrite your program as:
Remember that when you get the program compiled and linked, the appropriate pthreadGC. dll must be found at runtime in one of the places where the program loader searches for dlls.