-1

I created a static library (on linux machine), it is builded without mistakes, but when I link with other project in the result many error. Linked by cmake. When I built this library to the executable it is work.

CMakeLists.txt - where build library

cmake_minimum_required(VERSION 3.0)

project(log_lib LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(THREADS_PREFER_PTHREAD_FLAG ON)
set(Boost_USE_STATIC_LIBS ON)

add_library(log_lib STATIC
  log_lib_global.h
  log_lib.cpp
  log_lib.h
)

find_package(Boost COMPONENTS system thread log log_setup REQUIRED)
find_package(Threads REQUIRED)

target_link_libraries(log_lib 
Boost::system
Boost::thread
Boost::log
Boost::log_setup
)
target_link_libraries(log_lib Threads::Threads)

target_link_libraries(log_lib -ldl)#for UNIX

INSTALL(
  TARGETS log_lib
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  COMPONENT library
)

target_compile_definitions(log_lib LOG_LIB_LIBRARY)

CMakeLists.txt - cmake where linking library to executable

cmake_minimum_required (VERSION 3.0)

project (test_log_lib)

include_directories(../log_lib/)


add_executable (test_log_lib main.cpp)

target_link_libraries (test_log_lib /home/valdemar/prog/log_lib/build/liblog_lib.a)

and result cmake (it is not all)

[main] Building folder: test_log_lib test_log_lib
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/valdemar/prog/test_log_lib/build --config Debug --target test_log_lib -j 6 --
[build] [ 50%] Linking CXX executable test_log_lib
[build] /usr/bin/ld: CMakeFiles/test_log_lib.dir/main.cpp.o: в функции «boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<logger::severity_level>, boost::log::v2s_mt_posix::sources::single_thread_model>::~basic_logger()»:
[build] /usr/include/boost/log/sources/basic_logger.hpp:79: неопределённая ссылка на «boost::log::v2s_mt_posix::attribute_set::~attribute_set()»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/build/liblog_lib.a(log_lib.cpp.o): в функции «logger::Log_lib::log_init()»:
[build] /home/valdemar/prog/log_lib/log_lib.cpp:60: неопределённая ссылка на «boost::log::v2s_mt_posix::sinks::basic_text_ostream_backend<char>::add_stream(boost::shared_ptr<std::ostream> const&)»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/log_lib.cpp:62: неопределённая ссылка на «boost::log::v2s_mt_posix::sinks::basic_text_ostream_backend<char>::add_stream(boost::shared_ptr<std::ostream> const&)»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/log_lib.cpp:64: неопределённая ссылка на «boost::log::v2s_mt_posix::sinks::basic_text_ostream_backend<char>::add_stream(boost::shared_ptr<std::ostream> const&)»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/log_lib.cpp:86: неопределённая ссылка на «boost::log::v2s_mt_posix::core::get()»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/log_lib.cpp:86: неопределённая ссылка на «boost::log::v2s_mt_posix::core::add_sink(boost::shared_ptr<boost::log::v2s_mt_posix::sinks::sink> const&)»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/log_lib.cpp:87: неопределённая с��ылка на «boost::log::v2s_mt_posix::core::get()»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/log_lib.cpp:87: неопределённая ссылка на «boost::log::v2s_mt_posix::core::add_sink(boost::shared_ptr<boost::log::v2s_mt_posix::sinks::sink> const&)»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/log_lib.cpp:88: неопределённая ссылка на «boost::log::v2s_mt_posix::core::get()»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/log_lib.cpp:88: неопределённая ссылка на «boost::log::v2s_mt_posix::core::add_sink(boost::shared_ptr<boost::log::v2s_mt_posix::sinks::sink> const&)»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/build/liblog_lib.a(log_lib.cpp.o): в функции «boost::log::v2s_mt_posix::aux::light_rw_mutex::light_rw_mutex()»:
[build] /usr/include/boost/log/detail/light_rw_mutex.hpp:103: неопределённая ссылка на «pthread_rwlock_init»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/build/liblog_lib.a(log_lib.cpp.o): в функции «boost::log::v2s_mt_posix::aux::light_rw_mutex::~light_rw_mutex()»:
[build] /usr/include/boost/log/detail/light_rw_mutex.hpp:107: неопределённая ссылка на «pthread_rwlock_destroy»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/build/liblog_lib.a(log_lib.cpp.o): в функции «boost::log::v2s_mt_posix::aux::light_rw_mutex::lock_shared()»:
[build] /usr/include/boost/log/detail/light_rw_mutex.hpp:111: неопределённая ссылка на «pthread_rwlock_rdlock»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/build/liblog_lib.a(log_lib.cpp.o): в функции «boost::log::v2s_mt_posix::aux::light_rw_mutex::unlock_shared()»:
[build] /usr/include/boost/log/detail/light_rw_mutex.hpp:115: неопределённая ссылка на «pthread_rwlock_unlock»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/build/liblog_lib.a(log_lib.cpp.o): в функции «boost::log::v2s_mt_posix::aux::light_rw_mutex::lock()»:
[build] /usr/include/boost/log/detail/light_rw_mutex.hpp:119: неопределённая ссылка на «pthread_rwlock_wrlock»
[build] /usr/bin/ld: /home/valdemar/prog/log_lib/build/liblog_lib.a(log_lib.cpp.o): в функции «boost::log::v2s_mt_posix::aux::light_rw_mutex::unlock()»:
[build] /usr/include/boost/log/detail/light_rw_mutex.hpp:123: неопределённая ссылка на «pthread_rwlock_unlock»
  • https://www.boost.org/doc/libs/1_75_0/libs/log/doc/html/log/rationale/namespace_mangling.html – Ghasem Ramezani Aug 17 '21 at 08:04
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Andy Newman Aug 17 '21 at 08:24
  • Duplicate of https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix. Basically you have forgotten to link the library during the build (linking step, not compiling step). – Andy Newman Aug 17 '21 at 08:25

1 Answers1

0

You have multiple issues in your CMakeLists.txt, which amount to that your executable is not being linked with Boost.Log.

First, you should not specify full library filenames with path or compiler options in target_link_libraries. For example, instead of

target_link_libraries (test_log_lib /home/valdemar/prog/log_lib/build/liblog_lib.a)

you should be writing

target_link_libraries (test_log_lib log_lib)

Note that if log_lib and test_log_lib are part of the same CMake project tree then CMake will be able to pull dependencies of log_lib to link test_log_lib. This is possible because in this case log_lib will be interpreted as a CMake target rather than an external static library. If the executable and the library are separate then you have to specify all dependencies of log_lib for test_log_lib explicitly (as on Linux static libraries by themselves do not carry information about their dependencies).

Andrey Semashev
  • 10,046
  • 1
  • 17
  • 27