使用CMake和Beast Library编译Hello World项目

时间:2019-07-16 21:02:56

标签: c++ cmake boost-beast

我想用CMake构建一个包含boost / beast库的Hello World项目。

https://github.com/boostorg/beast

main.cpp:

  1 #include <boost/beast.hpp>
  2 
  3 using namespace std;
  4 
  5 int main(){
  6     
  7     printf("hello, world!\n");
  8 
  9     return 0;
 10 }

在我的项目目录中,我做了:

git clone --recursive https://github.com/boostorg/boost.git

然后我创建了CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.10)
project(beast_test_project)
add_executable(beast_test main.cpp)
target_include_directories(beast_test PUBLIC ${PROJECT_SOURCE_DIR}/boost/libs/beast/include)

这在运行make时给我一个错误

fatal error: boost/core/exchange.hpp: No such file or directory
 #include <boost/core/exchange.hpp>

然后我添加了

#target_include_directories(beast_test PUBLIC ${PROJECT_SOURCE_DIR}/boost/libs/mp11/include)

但是make给了我另一个相似。

因此,我一一添加了其他target_include_directories,直到摆脱了所有这些错误。这将导致我当前的CMakeLists.txt文件。

cmake_minimum_required(VERSION 3.10)
project(beast_test_project)

#include(${PROJET_SOURCE_DIR}/boost/libs/mp11/CMakeLists.txt)
#include(${PROJET_SOURCE_DIR}/boost/libs/asio/CMakeLists.txt)
#include(${PROJET_SOURCE_DIR}/boost/libs/core/CMakeLists.txt)
#include(${PROJET_SOURCE_DIR}/boost/libs/config/CMakeLists.txt)

add_executable(beast_test main.cpp)

#target_include_directories(beast_test PUBLIC ${PROJECT_SOURCE_DIR}/beast/include)
#include(${PROJECT_SOURCE_DIR}/boost/libs/beast/CMakeLists.txt)
target_include_directories(beast_test PUBLIC ${PROJECT_SOURCE_DIR}/boost/libs/beast/include)
target_include_directories(beast_test PUBLIC ${PROJECT_SOURCE_DIR}/boost/libs/mp11/include)
target_include_directories(beast_test PUBLIC ${PROJECT_SOURCE_DIR}/boost/libs/asio/include)
target_include_directories(beast_test PUBLIC ${PROJECT_SOURCE_DIR}/boost/libs/core/include)
target_include_directories(beast_test PUBLIC ${PROJECT_SOURCE_DIR}/boost/libs/config/include)

我以这样的方式制作CMakeLists.txt文件,并假设Boost.Beast是仅标头的库(如github页上所述)。

我当前的错误消息:

In file included from /usr/include/boost/smart_ptr/detail/sp_counted_base_std_atomic.hpp:18:0,
                 from /usr/include/boost/smart_ptr/detail/sp_counted_base.hpp:48,
                 from /usr/include/boost/smart_ptr/detail/shared_count.hpp:29,
                 from /usr/include/boost/smart_ptr/shared_ptr.hpp:28,
                 from /usr/include/boost/shared_ptr.hpp:17,
                 from /usr/include/boost/date_time/time_clock.hpp:17,
                 from /usr/include/boost/date_time/posix_time/posix_time_types.hpp:10,
                 from /home/robert/cpp/beast_test/boost/libs/asio/include/boost/asio/time_traits.hpp:23,
                 from /home/robert/cpp/beast_test/boost/libs/asio/include/boost/asio/detail/timer_queue_ptime.hpp:22,
                 from /home/robert/cpp/beast_test/boost/libs/asio/include/boost/asio/detail/deadline_timer_service.hpp:29,
                 from /home/robert/cpp/beast_test/boost/libs/asio/include/boost/asio/basic_waitable_timer.hpp:21,
                 from /home/robert/cpp/beast_test/boost/libs/asio/include/boost/asio/steady_timer.hpp:22,
                 from /home/robert/cpp/beast_test/boost/libs/beast/include/boost/beast/core/detail/stream_base.hpp:13,
                 from /home/robert/cpp/beast_test/boost/libs/beast/include/boost/beast/core/basic_stream.hpp:14,
                 from /home/robert/cpp/beast_test/boost/libs/beast/include/boost/beast/core.hpp:16,
                 from /home/robert/cpp/beast_test/boost/libs/beast/include/boost/beast.hpp:15,
                 from /home/robert/cpp/beast_test/main.cpp:1:
/home/robert/cpp/beast_test/boost/libs/core/include/boost/detail/sp_typeinfo.hpp:23:54: note: #pragma message: This header is deprecated. Use <boost/core/typeinfo.hpp> instead.
 BOOST_HEADER_DEPRECATED( "<boost/core/typeinfo.hpp>" )
                                                      ^
[100%] Linking CXX executable beast_test
CMakeFiles/beast_test.dir/main.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0xd1): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0xdd): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0xe9): undefined reference to `boost::system::system_category()'
CMakeFiles/beast_test.dir/main.cpp.o: In function `boost::system::error_category::std_category::equivalent(int, std::error_condition const&) const':
main.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition[_ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition]+0xb8): undefined reference to `boost::system::generic_category()'
main.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition[_ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition]+0xf3): undefined reference to `boost::system::generic_category()'
CMakeFiles/beast_test.dir/main.cpp.o: In function `boost::system::error_category::std_category::equivalent(std::error_code const&, int) const':
main.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0xb8): undefined reference to `boost::system::generic_category()'
main.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0xf3): undefined reference to `boost::system::generic_category()'
main.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0x1d2): undefined reference to `boost::system::generic_category()'
CMakeFiles/beast_test.dir/main.cpp.o: In function `boost::asio::error::get_system_category()':
main.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
CMakeFiles/beast_test.dir/main.cpp.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
main.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to `pthread_detach'
CMakeFiles/beast_test.dir/main.cpp.o: In function `boost::asio::detail::posix_thread::join()':
main.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[_ZN5boost4asio6detail12posix_thread4joinEv]+0x2b): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
CMakeFiles/beast_test.dir/build.make:94: recipe for target 'beast_test' failed
make[2]: *** [beast_test] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/beast_test.dir/all' failed
make[1]: *** [CMakeFiles/beast_test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

如果任何人都可以为我指明如何编译此基础项目的正确方向,我将不胜感激。

0 个答案:

没有答案