我尝试搜索相同的问题,但是没有一种解决方案对我不起作用。我无法编译它。我附上cmake文件和错误代码。
Cmake文件:
cmake_minimum_required(VERSION 3.13.3)
project(proj)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(Threads)
find_package(Boost 1.68.0 REQUIRED COMPONENTS system filesystem thread)
include_directories(include ${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
file(GLOB SOURCES source/*.cpp)
message(${Boost_LIBRARIES})
message(${Boost_LIBRARY_DIRS})
message(${Boost_INCLUDE_DIRS})
add_executable(proj ${SOURCES})
target_link_libraries(proj ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})
链接器错误:
Main.cpp:(.text._ZN5boost6system15system_categoryEv[_ZN5boost6system15system_categoryEv]+0x5): undefined reference to `boost::system::detail::system_category_instance'
CMakeFiles/proj.dir/source/Main.cpp.o: In function `boost::system::generic_category()':
Main.cpp:(.text._ZN5boost6system16generic_categoryEv[_ZN5boost6system16generic_categoryEv]+0x5): undefined reference to `boost::system::detail::generic_category_instance'
CMakeFiles/proj.dir/source/Server.cpp.o: In function `boost::asio::detail::socket_ops::close(int, unsigned char&, bool, boost::system::error_code&)':
Server.cpp:(.text._ZN5boost4asio6detail10socket_ops5closeEiRhbRNS_6system10error_codeE[_ZN5boost4asio6detail10socket_ops5closeEiRhbRNS_6system10error_codeE]+0x6b): undefined reference to `boost::system::detail::system_category_instance'
CMakeFiles/proj.dir/source/Server.cpp.o: In function `boost::asio::detail::socket_holder::~socket_holder()':
方法:
void Server::startListening()
{
while (true)
{
tcp::socket socket(m_io_service);
m_acceptor.accept(socket);
std::thread t(&Server::handleConnection, this, std::move(socket));
t.detach();
}
}
void Server::handleConnection(tcp::socket socket)
{
...
}
答案 0 :(得分:0)
您不应使用基于目录的api,而应使用基于目标的api。
正如@Someprogrammerdude在评论中所说,您应该找到并使用如下的Boost库:
find_package(Boost 1.68.0 REQUIRED COMPONENTS system filesystem thread)
file(GLOB SOURCES source/*.cpp) # File GLOB is a bad CMake practice
add_executable(proj ${SOURCES})
target_link_libraries(proj PUBLIC
boost::system
boost::filesystem
boost::thread
)
target_link_libraries
将添加所有必要的包含目录和链接器标志。