views:

68

answers:

3

I made a static library with GCC. Building of the library was OK.

When I use it the linker throws undefined reference errors on some functions. But nm says the functions are defined and exported in the static library (marked with T). I know about the linking order that I need to put the libraries after that module that needs them so this can not be a problem.

The static library was built from 3 C files. A.c B.c and D.c The D module depend on A and B (includes their headers).

No problem when I use functions from A and B but when I try to use any function from D I get undefined reference errors on them.

If I move these functions in A or B it works. But not if they are in the D module.

I'm completely run out of ideas what's going on or what is I'm overlooked.

I'm using Code::Blocks and working with plain C files.

A: 

Perhaps you should use ranlib or the approriate ar option to provide an index to your .a file.

Jens Gustedt
A: 

An old trick that many times works: List each static library twice in the linking phase.

i.e., in your makefile (or whatever you're using), put:

gcc -o <outfile> <liba> <libb> <libc> <liba> <libb> <libc>

Anyway, I hope you get the idea.

adamk
+1  A: 

I found out that I added A .cpp file to my project and I just renamed it to .c. I chose C language instead of C++ when I created the project. I did't think this could cause problems

I thought the file extension decides when the IDE chooses between gcc and g++. But not. In Code::Blocks if you add a file with a .cpp extension it will use g++. If you add a file with a .c extension it will use gcc. But if you rename the file it will use the same compiler. You have to change it explicitly in the project options.

That D module was built using g++ instead of gcc.

I realized this when I set the IDE to show me the entire command line when building not just "Compiling foo.c".

Calmarius