问题
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.
I use:
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
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:
int func ();
...
int func ()
{ ... }
Then built the Library as:
gcc -Wall file.c -o file.o
...
...
ar rvs libfun.a $(OBJS)
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 ();
Compile with:
gcc -Wall -L./path-to-lib -lfun -o appl
Then i get the below error:
In function `main':
undefined reference to `func()'
collect2: ld returned 1 exit status
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" ):
Archive index:
Cfg1 in f1.o
mCfg1 in f1.o
dpCfg in f1.o
But the other archives which i am using without any changes contain below strange pattern:
Archive index:
_Z29platformSetjP38tagTCPIP_INTERFACE_INSTANCE_ATTRIBUTES in platform.o
_Z27platformTestSetTcpjP20tagTCPIP_CONFIG_DATAPh in platform.o
_Z23platformSetTcpIpjP20tagTCPIP_CONFIG_DATA in platform.o
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?
Regards.
回答1:
Another reason may be the incorrect order of your -lxx
.
In brief, put -lrelied
after -lrelying
.
see here.
回答2:
I found out the reason for the error. I was mixing the static library with other libraries compiled with position independent code (PIC) and some other flags. Adding these flags solved the issue.
来源:https://stackoverflow.com/questions/13857439/undefined-reference-to-a-function-defined-in-static-library