当前位置: 代码迷 >> 综合 >> CMakeLists.txt 中配置Boost库
  详细解决方案

CMakeLists.txt 中配置Boost库

热度:88   发布时间:2023-12-12 13:33:58.0

未配置正确时报错

‘__static_initialization_and_destruction_0(int, int)’中:
/usr/include/boost/system/error_code.hpp:221:对‘boost::system::generic_category()’未定义的引用
/usr/include/boost/system/error_code.hpp:222:对‘boost::system::generic_category()’未定义的引用
/usr/include/boost/system/error_code.hpp:223:对‘boost::system::system_category()’未定义的引用

CMakeLists.txt中,增加以下配置


add_definitions(-DBOOST_ERROR_CODE_HEADOR_ONLY)# for boostfind_package(Boost REQUIRED COMPONENTS system thread)#for boostinclude_directories(${Boost_INCLUDE_DIRS})# 可执行文件编译时的配置
add_executable(_node_name_
src/_file_name_.cpp)
target_link_libraries(_node_name_ ${PROJECT_NAME}${Boost_LIBRARIES})

 

示例:

src/main.cpp

#include <iostream>
#include <boost/thread/thread.hpp> 
#include <boost/thread/mutex.hpp> boost::mutex mutex;void print_block(int n, char c)
{mutex.lock();for (int i = 0; i < n; ++i){std::cout << c;}std::cout << '\n';mutex.unlock();
}int main(int argc, char* argv[])
{boost::thread thread1(&print_block, 300, '*');boost::thread thread2(&print_block, 300, '$');thread1.join();thread2.join();return 0;
}

CMakeLists.txt配置:

# cmake needs this line
cmake_minimum_required(VERSION 2.8)# Define project name
project(mutex_project)SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11")## System dependencies are found with CMake's conventions
find_package(Boost REQUIRED COMPONENTSthread
)if(NOT Boost_FOUND)message("NOT found Boost")
endif()include_directories(${Boost_INCLUDE_DIRS})
# Declare the executable target built from your sources
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})