linker error “relocation R_X86_64_PC32 against undefined symbol” despite compilation with -fPIC

妖精的绣舞 提交于 2019-12-01 17:27:26

问题


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; 


来源:https://stackoverflow.com/questions/14779260/linker-error-relocation-r-x86-64-pc32-against-undefined-symbol-despite-compila

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!