linker error “relocation R_X86_64_PC32 against und

2019-02-22 15:46发布

问题:

I'm compiling a c++ program using the command line

g++ -c prog.cc -std=c++11 -march=native -fPIC -fopenmp

and then try to make a shared object via

g++ prog.o -shared -fopenmp -o lib/libprog.so

This has always worked. But today I get:

/usr/bin/ld: prog.o: relocation R_X86_64_PC32 against undefined symbol 
  `_ZTVN12_GLOBAL__N_111handle_baseE' can not be used when making a shared
  object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

The symbol _ZTVN12_GLOBAL__N_111handle_baseE de-mangles to vtable for (anonymous namespace)::handle_base (handle_base is a polymorphic class defined in the anonymous namespace in prog.cc and yes I do call dynamic_cast<handle_base>().)

I'm using gcc version 4.7.0 (GCC) and GNU ld (GNU Binutils; openSUSE 11.1) 2.19. Can anybody help (suggest solutions [other than doing without shared object or the dynamic cast])?

回答1:

I just ran into something similar when upgrading to ubuntu 14.04. I had to add -fkeep-inline-functions to the source file that defined the 'missing' symbol. No idea if your problem is similar.



回答2:

You just need to make the default visibility hidden for your base class(handle_base). You can do this by -

#define VISIBILITY __attribute__((visibility("hidden")))
class VISIBILITY handle_base;