undefined reference to c functions in c++ code

2019-04-03 00:08发布

问题:

I have a strange problem: the code bellow perfectly compiled.
src.cpp:

extern "C" {
    #include "header.h"
}

void A::Execute() {
    B::Instance().Reset(ix);
    c_func(ix);// this is c functions declared in header.h
    C::Instance().Erase(ix);
}

But when I comment out the c_funk() I get linkage error in all places where I use c functions from the header.h file.

With that minor change:

void A::Execute() {
    B::Instance().Reset(ix);
    //c_func(ix);// this is c function declared in header.h
    C::Instance().Erase(ix);
}

I get: undefined reference to c_func().
Any ideas how to solve it? Thanks.

Update:
I have added a dummy function to header.h: foo_for_linkage_problem();
and in such way solved the problem. As I understand the linker try to make some optimization that couses this problem. new code:

void A::Execute() {
    B::Instance().Reset(ix);
    foo_for_linkage_problem();// this is c empty function declared in header.h 
    C::Instance().Erase(ix);
}

回答1:

tldnr: You have problem in the order the libraries are provided to linker.

I think that I know what is wrong. Let's assume that you have four files:

main.c:

#include <stdio.h>

int a();
int b();

int main()
{
    printf("%d\n", a());
    printf("%d\n", b());

    return 0;
}

main2.c:

#include <stdio.h>

int a();
int b();

int main()
{
    //printf("%d\n", a());
    printf("%d\n", b());

    return 0;
}

a.c:

int a()
{
    return 67;
}

b.c:

int a();

int b()
{
    return a()+5;
}

We create liba.a and libb.a:

gcc a.c -c -o a.o
gcc b.c -c -o b.o
ar rcs liba.a a.o
ar rcs libb.a b.o

Now we compile our exec:

gcc main.c liba.a libb.a -o test

And everything works just fine

gcc main2.c liba.a libb.a -o test

I get:

libb.a(b.o): In function `b':
b.c:(.text+0xa): undefined reference to `a'
collect2: error: ld returned 1 exit status

let's examine which symbols my libraries provides/requires:

$nm liba.a

a.o:
0000000000000000 T a

Symbol type T means that this library have file a.o which provides symbol a

$nm libb.a

b.o:
                 U a
0000000000000000 T b

libb.a contains file b.o which provides symbol b but requires symbol a

The linker does not include whole static library when asked. It looks which .o files are needed and links only those. Thus the order you provide the files is crucial.

When I executes following command, main.c gets compiled and it needs both a and b symbols. When linker gets liba.a library it links a.o file because it provides a symbol. When linker gets libb.a library it links b.o file because it provides b symbol. Everything works fine.

gcc main.c liba.a libb.a -o test

When I executes the main2 command, main2.c gets compiled and it needs only b symbols. When linker gets liba.a it does not link a.o becuase a is not needed at all. Then linker gets libb.a library it links b.o file because it provides b symbol. But the b.o file needs symbol a, and it is too late to link a.o, liba.a was already processed. Note that when I switch libraries it will compile cleanly:

gcc main.c libb.a liba.a -o test


标签: c++ linkage