I am trying to call cplus_demangle() function from libiberty.a, but I am getting “undefined reference to ‘cplus_demangle’ error.
This is my simple main:
extern "C" char *cplus_demangle(const char *, int);
int main() {
cplus_demangle("a", 0);
}
and my build command (libiberty is in ../../install/lib64):
g++ -L../../install/lib64 -liberty main.cpp -o main
If I rename libiberty.a to something else I’d get “cannot find -liberty” error. So, I assume the linker sees the library. And I can see cplus_demangle in libiberty.a:
$> nm -C --defined-only libiberty.a | grep "cplus_demangle"
0000000000002230 T cplus_demangle
...
But I am getting this error when I compile:
main.cpp:(.text+0xf): undefined reference to `cplus_demangle'
collect2: ld returned 1 exit status
make: *** [main] Error 1
I’d appreciate your help. Thanks!
Move the -liberty to the end of the command line. The library should be searched after main.o is created. Order matters.