如何修复C ++ Boost HTTP示例中的'x86_64体系结构未找到的符号'错误

时间:2019-01-07 11:00:03

标签: c++ macos boost

我想用boost http server示例c ++ 11版本来做一个http服务器。 我使用CLion构建按钮构建了项目,它报告了编译错误:

/Applications/CMake.app/Contents/bin/cmake --build /Users/chenyimin/CLionProjects/test_server/cmake-build-debug --target test_server -- -j 2
[ 50%] Linking CXX executable test_server
Undefined symbols for architecture x86_64:
  "http::server::server::run()", referenced from:
      _main in main.cpp.o
  "http::server::server::server(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我的环境:

  • CLion Mac 2018.2

  • cmake 3.12.4

  • c编译器:gcc-8

  • c ++编译器:g ++-8

  • GDB 8.0.1

  • 使用boost 1.68.0 lib。

我的boost 1.68.0安装了: brew install boost --cc = gcc-8

我的boost lib安装的编译器gcc-8应该与CLion构建编译器gcc-8相同。

但是它报告了此错误:ld:找不到架构x86_64的符号

main()代码:

//
// main.cpp
// ~~~~~~~~
//
// Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include "server.hpp"

int main(int argc, char* argv[])
{
  try
  {
    // Check command line arguments.
    if (argc != 4)
    {
      std::cerr << "Usage: http_server <address> <port> <doc_root>\n";
      std::cerr << "  For IPv4, try:\n";
      std::cerr << "    receiver 0.0.0.0 80 .\n";
      std::cerr << "  For IPv6, try:\n";
      std::cerr << "    receiver 0::0 80 .\n";
      return 1;
    }

    // Initialise the server.
    http::server::server s(argv[1], argv[2], argv[3]);

    // Run the server until stopped.
    s.run();
  }
  catch (std::exception& e)
  {
    std::cerr << "exception: " << e.what() << "\n";
  }

  return 0;
}

我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)

project(test_server)

set(CMAKE_CXX_STANDARD 11)


SET (BOOST_ROOT "/usr/local/Cellar/boost/1.68.0/")

SET (BOOST_INCLUDEDIR "/usr/local/Cellar/boost/1.68.0/include")

SET (BOOST_LIBRARYDIR "/usr/local/Cellar/boost/1.68.0/lib")


set(Boost_USE_STATIC_LIBS ON)

set(Boost_USE_MULTITHREADED ON)

set(Boost_USE_STATIC_RUNTIME ON)

find_package( Boost REQUIRED COMPONENTS program_options system thread filesystem REQUIRED )


if(Boost_FOUND)


  include_directories(${Boost_INCLUDE_DIRS})


  add_executable(test_server main.cpp)


  target_link_libraries(test_server ${Boost_LIBRARIES})

endif()

如何解决此问题?请帮助我。非常感谢!

2 个答案:

答案 0 :(得分:0)

我知道为什么。 我更改了CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
project(test_server)

set(CMAKE_CXX_STANDARD 14)

SET (BOOST_ROOT "/usr/local/Cellar/boost/1.68.0/")
SET (BOOST_INCLUDEDIR "/usr/local/Cellar/boost/1.68.0/include")
SET (BOOST_LIBRARYDIR "/usr/local/Cellar/boost/1.68.0/lib")

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
find_package( Boost REQUIRED COMPONENTS program_options system thread filesystem REQUIRED )

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(test_server main.cpp server.cpp
            connection.cpp connection_manager.cpp
            request_handler.cpp request_parser.cpp
            reply.cpp request.hpp mime_types.cpp)
    target_link_libraries(test_server ${Boost_LIBRARIES})
endif()

一切正常。

答案 1 :(得分:0)

请确保在CMakeLists.txt的以下行中具有必需的软件包。

find_package( Boost REQUIRED COMPONENTS program_options system thread filesystem serialization REQUIRED )

我只使用boost的序列化组件,只需添加该软件包就可以很好地编译