I am trying to compile a C++ shared library with cmake. Here is a minimal reproducible example from this third-party code demonstrating the issue:
Content of CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(foobar)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
-I/usr/include/c++/ \
-I${CMAKE_INCLUDE_PATH} ")
add_library(foobar SHARED src/foobar.cpp)
Content of foobar.cpp:
class Foobar {
public:
Foobar() {}
virtual ~Foobar() {}
};
I am getting this linking error which doesn't make sense, since shared libraries don't need a main function:
$ cmake .
...
$ make
[ 50%] Building CXX object CMakeFiles/foobar.dir/src/foobar.cpp.o
[100%] Linking CXX shared library libfoobar.so
/usr/lib/gcc-cross/aarch64-linux-gnu/11/../../../../aarch64-linux-gnu/bin/ld: /usr/lib/gcc-cross/aarch64-linux-gnu/11/../../../../aarch64-linux-gnu/lib/../lib/Scrt1.o: in function `_start':
(.text+0x1c): undefined reference to `main'
/usr/lib/gcc-cross/aarch64-linux-gnu/11/../../../../aarch64-linux-gnu/bin/ld: (.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/foobar.dir/build.make:97: libfoobar.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/foobar.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
With a slightly different flag order, I am getting this linking error:
relocation XYZ against `vtable for Foobar::~Foobar' can not be used
when making a shared object; recompile with -fPIC