8

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.

2
  • 2
    gcc -Wall -L./path-to-lib -lfun -o appl there is no source/object file mentioned here, so I suspect it's appended to the end. The library to link with, probably needs to go after the source, so move -lfun to the end of the command line. Commented Dec 13, 2012 at 23:49
  • @Daniel : Yes i used OBJS containing all the compiled sources of application during compiling final application. I tried to move the libraries ($(LIBS) all around the command line, but it did not help. i use cross compiler for ARM embedded processors. ** $(CROSS_COMPILE)gcc -Wall -o appl $(OBJS) -L./path-to-lib -lfun ** Commented Dec 14, 2012 at 5:15

2 Answers 2

2

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

In brief, put -lrelied after -lrelying.

see here.

Sign up to request clarification or add additional context in comments.

1 Comment

Had the same problem. Yeah, as it turns out, when you specify multiple inter-dependent static libraries via -l flag - dependants must go first, then their dependencies. The -fPIC flag has nothing to do with this. Absolutely stupid. Why can't ld discard stuff at the later stage?
1

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 "-fPIC" flag solved the issue.

2 Comments

I have the same problem. Which flags did you use?
@Kevin: adding "-fPIC" solved the issue. Other flags didn't matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.