c++ project link static and shared libraries

311 views Asked by At

I have a c++ project. Its project structure is presented below:

.
|--src
|   |--foo
|   |   |--foo.h
|   |   |--foo.cpp
|   |   |--CMakeLists.txt
|   |
|   |--bar
|       |--bar.h
|       |--bar.cpp
|       |--CMakeLists.txt
|
|--main.cpp
|--CMakeLists.txt

I add foo and bar as static libraries by using add_library(foo STATIC ${FOO_DIR}) in CMakeLists.txt.

main.cpp links to foo and bar libs using static linking.

Now I have some 3rd party shared libs, for example, abc.so.

In top level CMakeLists.txt:

project(my_proj)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc -static")

#find abc libs
find_package(abc REQUIRED)
...

#adding foo bar subdirectories
...

add_executable(my_proj main.cpp)
target_link_libraries(my_proj foo bar abc)

When main.cpp links foo, bar and abc.so, it shows the error: attempted static link of dynamic object /xxx/abc.so

What should I do to link shared and static libs simultaneously?

update

If I try to remove -static, it shows the error:

ibQt5Core.a(qstringlist.o): undefined reference to symbol '_ZNSt3pmr25monotonic_buffer_resourceD1Ev@@GLIBCXX_3.4.28'
/usr/bin/ld: /usr/lib/libstdc++.so.6: error adding symbols: DSO missing from command line

It links to libstdc++ shared library, even though I explicitly used

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")

What should I do to link static libstdc++?

0

There are 0 answers