In Eclipse I developed a C++ - client for Basex (an Open source XML-database) and it is my intention to deploy that client as shared library. A simple CMakelists.txt is enough to build an so. However, according to this article “(Modern CMake tutorial for C++ library developers | by Anton Pantyukhin | Medium)” , it takes more to really use an so effectively.
Based on the accompagning sample code I converted my code to this environment.
- In CMakeLists.txt, I replaced all occurrences of mylib and MYLIB with BaseXCpp and BASEXCPP, respectively.
- In the public header BasexCpp.h, I changed all class definitions to “class BASEXCPP_EXPORT …”.
And after adjusting some paths in header files, the command 'cmake -S . -B Build/Debug/ --fresh -DCMAKE_INSTALL_PREFIX=/home//lib -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G “Ninja” results in so-files in the “Build/Debug” directory.
However, when I use the following CMakeLists.txt to link the just created so to a test file, I get an error message for each method that is called in the so (example: /usr/bin/ld: libBasexTest.cpp:(.text+0x34a): undefined reference to `BasexCpp::BasexClient::getStatusabi:cxx11’)
cmake_minimum_required(VERSION 3.14)
project(lib-Test LANGUAGES CXX)
include("../../cmake/utils.cmake")
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)
if(is_top_level)
find_package(BasexCpp REQUIRED)
endif()
set(sources libBasexTest.cpp)
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})
add_executable(lib-Test)
target_sources(lib-Test PRIVATE ${sources})
target_link_libraries(lib-Test PRIVATE BasexCpp::BasexCpp)
if(NOT is_top_level)
win_copy_deps_to_target_dir(lib-Test BasexCpp::BasexCpp)
endif()
This is the code in compile_commands.json that is related to the test application. It seems that libBasexTest.cpp.o is not linked to the shared library.
{
"directory": "/home/<user>/Thuis/C_Test2/libBasexCpp/Build/Debug",
"command": "/usr/lib64/ccache/c++ -I/home/<user>/Thuis/C_Test2/libBasexCpp/include -I/home/bengbers/Thuis/C_Test2/libBasexCpp/Build/Debug/include -fvisibility=hidden -fvisibility-inlines-hidden -o examples/Test/CMakeFiles/lib-Test.dir/libBasexTest.cpp.o -c /home/bengbers/Thuis/C_Test2/libBasexCpp/examples/Test/libBasexTest.cpp",
"file": "/home/<user>/Thuis/C_Test2/libBasexCpp/examples/Test/libBasexTest.cpp",
"output": "examples/Test/CMakeFiles/lib-Test.dir/libBasexTest.cpp.o"
}
I have 2 questions:
- Is adding BASEXCPP_EXPORT to the class defiitions in BasexCpp.h the correct way to export symbols?
- Should I add something to CMakeLists,txt to make sure libBasexTest.o is linked to the shared library?
Ben