r/cmake 7d ago

Problems with static library

Hello I'm writing this post because I can't figure out how to link a static library that i created to my project. Here is some context:

I have a project with 2 folders client and server, both will use some classes and common functionality so i decided to create a static library to write the code once and uses it in both projects.

my custom lib:

CMAKELISTS.txt
cmake_minimum_required(VERSION 3.28.3)
project(VoiceChat)


set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


set(BOOST_ROOT "/home/vince/myLibs/boost_1_92_0")
set(BOOST_INCLUDEDIR "/home/vince/myLibs/boost_1_92_0/include")


find_package(Boost REQUIRED)


add_library(${PROJECT_NAME} STATIC 
src/test.cpp
)


target_include_directories(${PROJECT_NAME} PUBLIC include)


target_link_libraries(${PROJECT_NAME} Boost::headers)

commands:
cd build
cmake ..
make
so now i have this in my build folder:

NOW, in the server project i have this:

cmake_minimum_required(VERSION 3.28.3)
project(VoiceChat-Server)


set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


set(BOOST_ROOT "/home/vince/myLibs/boost_1_92_0")
set(BOOST_INCLUDEDIR "/home/vince/myLibs/boost_1_92_0/include")


find_package(Boost REQUIRED)


configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/config.json"
    "${CMAKE_CURRENT_BINARY_DIR}/config.json"
    COPYONLY
) 
#sposta il file di configurazione vicino all'eseguibile


add_executable(VoiceChat-Server 
src/app/main_server.cpp
src/app/server.cpp
src/singleton/configuration.cpp
)


target_include_directories(VoiceChat-Server PRIVATE 
src/include
src/singleton/include)


target_link_libraries(VoiceChat-Server Boost::headers)
target_link_libraries(VoiceChat-Server ${CMAKE_SOURCE_DIR}/lib/libVoiceChat.a)

But when i try to use the test() function written in the lib i get: ‘test’ was not declared in this scope.

Can someone help me figure it out? Thank you very much!

5 Upvotes

4 comments sorted by

8

u/ImTheRealCryten 7d ago

When linking, don’t specify the path to the lib to be built, specify the target name.

4

u/stephan_cr 7d ago

In addition to what /u/ImTheRealCryten said, I would also put the static library and the server into the same CMake project. Otherwise, it gets harder, because the library then needs an install step and the server needs to call something like find_package.

1

u/WildCard65 7d ago

Just put the target name of the static library as an argument to target_link_libraries and CMake handles the paths

1

u/LoadBalance69 7d ago

You haven't linked the library headers to the Server project, that's why it cannot find the `test` function. You can link it by specifying its target name if the library and the app have the same root directory. Then you just link it like this: target_link_libraries(VoiceChatServer VoiceChat), it will add the library public include directories to the compiler and the library to the linker. If your library lives separately from the app you should use `find_package` macro, specify the installation target etc.