Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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.

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.

like image 539
Ashoka K Avatar asked Dec 13 '12 10:12

Ashoka K


People also ask

How to solve undefined reference error in c++?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

What is undefined symbol in c?

A symbol remains undefined when a symbol reference in a relocatable object is never matched to a symbol definition. Similarly, if a shared object is used to create a dynamic executable and leaves an unresolved symbol definition, an undefined symbol error results.

What is unresolved external symbol error in c++?

unresolved external symbol 'symbol' referenced in function 'function' The compiled code for function makes a reference or call to symbol, but the linker can't find the symbol definition in any of the libraries or object files.


1 Answers

Another reason may be the incorrect order of your -lxx.

In brief, put -lrelied after -lrelying.

see here.

like image 102
weiweishuo Avatar answered Sep 27 '22 20:09

weiweishuo