CMake error: undefined reference to "name_of_my_funciton"

Viewed 41

I have a c++ project with CMake. Project tree

CMakeLists.txt code:

cmake_minimum_required(VERSION 3.22 FATAL_ERROR)

project(client-server
VERSION 1.0
LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "-Wall -Wpedantic -Wextra")

include_directories(${CMAKE_BINARY_DIR}/src)
include_directories(${CMAKE_BINARY_DIR}/src/Client)
include_directories(${CMAKE_BINARY_DIR}/src/Server)
include_directories(${CMAKE_BINARY_DIR}/headers)

set(CLIENT_SOURCES src/Client/main.cpp
    src/Client/client.cpp
    src/strmanip.cpp
    src/shared_buffer_file.cpp
    headers/strmanip.hpp
    headers/server_settings.hpp
    headers/shared_buffer_file.hpp)

set(SERVER_SOURCES src/Server/main.cpp
    src/Server/server.cpp
    src/strmanip.cpp
    headers/strmanip.hpp
    headers/server_settings.hpp)

add_executable(client ${CLIENT_SOURCES})
add_executable(server ${SERVER_SOURCES})

set_target_properties(client PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/src/Client")
set_target_properties(server PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/src/Server")

Then, when I try to run following commands in terminal:

cmake .
cmake --build .

I have this kind of errors:

/usr/bin/ld: CMakeFiles/client.dir/src/Client/main.cpp.o: in function `Produce(shared_buffer_file<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >&)':
main.cpp:(.text+0x90): undefined reference to `strmanip::is_digits() const'
/usr/bin/ld: main.cpp:(.text+0xa3): undefined reference to `strmanip::is_less_or_64symbols() const'
/usr/bin/ld: main.cpp:(.text+0x101): undefined reference to `shared_buffer_file<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::push(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: CMakeFiles/client.dir/src/Client/main.cpp.o: in function `Consume(shared_buffer_file<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >&)':
main.cpp:(.text+0x223): undefined reference to `shared_buffer_file<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::pop()'
/usr/bin/ld: CMakeFiles/client.dir/src/Client/main.cpp.o: in function `main':
main.cpp:(.text+0x382): undefined reference to `shared_buffer_file<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::shared_buffer_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: main.cpp:(.text+0x440): undefined reference to `shared_buffer_file<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::~shared_buffer_file()'
/usr/bin/ld: main.cpp:(.text+0x4a9): undefined reference to `shared_buffer_file<std::__cxx11::basic_string<char, std::char_traits<char>,   std::allocator<char> > >::~shared_buffer_file()'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/client.dir/build.make:145: src/Client/client] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/client.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2

Accordingly, I have header files in the headers folder and their implementation in the src, src/Client and src/Server folders. Did I split the files correctly, if not, how to do it correctly, if yes, how to correctly describe CMake where to look when he does not see the implementation of my functions from header files

0 Answers
Related