Home > Software engineering >  Compiles error:Undefined reference, which is caused mainly by dependency with libraries?
Compiles error:Undefined reference, which is caused mainly by dependency with libraries?

Time:09-27

I met a c compile error that almost drives me mad these days. The output info is (/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: undefined reference to symbol '__libc_start_main@@GLIBC_2.2.5' //lib/x86_64-linux-gnu/libc.so.6: error adding symbols: DSO missing from command line

it's not undefined reference to `main', to be careful.)

The basic case is very simple. library B depends on library C. excutable A depends on library B, and thus depends on library C. Below is my code, it's very simple as well.

**c.h**
void kk();

**c.cpp**
#include <iostream>
using namespace std;

void kk()
{
    cout<<"111"<<endl;
} 

**b.h**
#include "c.h"
void pp();

**b.cpp**
#include "b.h"
void pp()
{
    kk();
}

**a.cpp**
#include "b.h"
int main()
{
    pp();
}

And This is my Compiling process: make c && b respectively to be a shared library, and build a through linking against them.

1. g   -fpic -shared c.cpp -o libc.so
2. g   -fpic -shared b.cpp -o libb.so
3. g   a.cpp -o a -lb -lc -L. 

Besides,I tried many ways to solve this problem. None worked. And I found that in the fianl step, If I do not link library c, the output is the same.It seems that I failed to link c finally,But I just did it, who knows the reason. The g version??

CodePudding user response:

Your libc.so conflicts with the one installed by glibc. You would need to change to use another name

  • Related