Consider the following three files, all in the same directory.
Lib.h
namespace Foo {
void bar();
}
Lib.cc
namespace Foo {
void bar() { }
}
App.cc
#include "Lib.h"
int main(){
Foo::bar();
}
Here are the commands I tried:
g++ -std=c++11 -fPIC -shared -o Lib.o Lib.cc
g++ -std=c++11 -shared -o libLib.so Lib.o
g++ -std=c++11 -o App App.cc -L. -lLib
Unfortunately, the last line gives the following undefined reference error.
g++ -std=c++11 -o App App.cc -L. -lLib
/tmp/ccwtxTdl.o: In function `main':
App.cc:(.text+0x5): undefined reference to `Foo::bar()'
collect2: error: ld returned 1 exit status
I also tried the following with the same error.
g++ -std=c++11 -o App App.cc libLib.so
Here is the output of nm libLib.so
, which does not appear to include bar
.
0000000000201030 B __bss_start
0000000000201030 b completed.6992
w __cxa_finalize
00000000000004e0 t deregister_tm_clones
0000000000000550 t __do_global_dtors_aux
0000000000200e48 t __do_global_dtors_aux_fini_array_entry
0000000000201028 d __dso_handle
0000000000200e58 d _DYNAMIC
0000000000201030 D _edata
0000000000201038 B _end
00000000000005c8 T _fini
0000000000000590 t frame_dummy
0000000000200e40 t __frame_dummy_init_array_entry
00000000000005d8 r __FRAME_END__
0000000000201000 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
0000000000000490 T _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
0000000000200e50 d __JCR_END__
0000000000200e50 d __JCR_LIST__
w _Jv_RegisterClasses
0000000000000510 t register_tm_clones
0000000000201030 d __TMC_END__
Which compiler flag am I missing in my build on the so
file?
I have already examined this question but it did not resolve the issue.
Note that I am trying to build a shared library out of Lib.cc
and Lib.h
, and then link App.cc
against it. I am not trying to compile all the source files together.