cpprestsdk:架构的未定义符号 x86_64

cpprestsdk: Undefined symbols for architecture x86_64

我已经访问了所有其他问题,但据我所知 none 是我的问题。

运行 OS MacBook Pro 16GB 内存 Intel Core I7 上的 X El Capitan 10.11.6

我也有 运行 brew doctor,但没有发现任何会导致此问题的问题。下面是我的 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.0.0)
project(WebClient VERSION 0.0.0)

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPLIER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPLIER_SUPPORTS_CXX0X)
if(COMPLIER_SUPPORTS_CXX14)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
elseif(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
    message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++14 support.
                    Please use a different C++ compiler.")
endif()

FIND_PACKAGE( Boost 1.62 COMPONENTS program_options REQUIRED )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )

set(OPT_CPPFLAGS "-I/usr/local/opt/openssl/include -I/usr/local/opt/libiconv/include")
set(OPT_LDFLAGS "-v -lcpprest -lboost_system -L/usr/local/opt/openssl/lib -L/usr/local/opt/libiconv/lib -lcrypto -lssl")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPT_CPPFLAGS} -v -g -O3 -fno-common -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OPT_LDFLAGS}")

add_executable(anyExecutable webclient.cpp)

TARGET_LINK_LIBRARIES( anyExecutable ${Boost_LIBRARIES} )

include(CPack)

我使用 brew 安装了 cpprestsdk/2.8.0,其中还包括 boost/1.62.0 和 openssl/1.0.2j 以及 libiconv/1.14。我的代码来自 wiki cpprestsdk 上的教程。

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <atomic>

using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams

int main(int argc, char* argv[]) {
  auto fileStream = std::make_shared<ostream>();

  // Open stream to output file.
  pplx::task<void> requestTask = fstream::open_ostream( U("results.xml") ).then(
    [=](ostream outFile) {
        *fileStream = outFile;

        // Create http_client to send the request.
        http_client client(U("http://www.dictionaryapi.com/api/v1/references/"));

        // Build request URI and start the request.
        uri_builder builder(U("thesaurus/xml/"));
        builder.append_path(U("ranking"),true);
        builder.append_query(U("?key"), U("--------------------"));
        return client.request(methods::GET, builder.to_string());
  })

  // Handle response headers arriving.
  .then( [=](http_response response ) {
    printf("Received response status code:%u\n", response.status_code());

    // Write response body into the file.
    return response.body().read_to_end(fileStream->streambuf());
  })

  // Close the file stream.
  .then( [=](size_t ) {
    return fileStream->close();
  });

  // Wait for all the outstanding I/O to complete and handle any exceptions
  try {
    requestTask.wait();
  } catch (const std::exception &e) {
    printf("Error exception:%s\n", e.what());
  }//try-catch BLOCK

  return 0;
}//Main

为了节省一些 space 只打印一些相同的错误找不到:

    [vscode] Executing cmake command: cmake --build /Users/gumpy/git-repos/webapi/build --target all --config Debug -- -j 10
[ 50%] Building CXX object CMakeFiles/anyExecutable.dir/webclient.cpp.o
Apple LLVM version 8.0.0 (clang-800.0.38)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name webclient.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 274.1 -v -dwarf-column-info -debug-info-kind=standalone -dwarf-version=2 -debugger-tuning=lldb -coverage-file /Users/gumpy/git-repos/webapi/build/CMakeFiles/anyExecutable.dir/webclient.cpp.o -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0 -I /usr/local/include -I /usr/local/opt/openssl/include -I /usr/local/opt/libiconv/include -stdlib=libc++ -O3 -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /Users/gumpy/git-repos/webapi/build -ferror-limit 19 -fmessage-length 0 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -o CMakeFiles/anyExecutable.dir/webclient.cpp.o -x c++ /Users/gumpy/git-repos/webapi/webclient.cpp
clang -cc1 version 8.0.0 (clang-800.0.38) default target x86_64-apple-darwin15.6.0
ignoring nonexistent directory "/usr/include/c++/v1"
ignoring duplicate directory "/usr/local/include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/opt/openssl/include
 /usr/local/opt/libiconv/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
[100%] Linking CXX executable anyExecutable
Apple LLVM version 8.0.0 (clang-800.0.38)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.11.0 -o anyExecutable -L/usr/local/opt/openssl/lib -L/usr/local/opt/libiconv/lib -search_paths_first -headerpad_max_install_names -lcpprest -lboost_system -lcrypto -lssl CMakeFiles/anyExecutable.dir/webclient.cpp.o /usr/local/lib/libboost_program_options-mt.dylib -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "boost::this_thread::interruption_point()", referenced from:
      boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in webclient.cpp.o
      boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, timespec const&) in webclient.cpp.o
  "boost::chrono::steady_clock::now()", referenced from:
      pplx::details::event_impl::wait(unsigned int) in webclient.cpp.o
  "boost::chrono::system_clock::now()", referenced from:
      pplx::details::event_impl::wait(unsigned int) in webclient.cpp.o
  "boost::detail::get_current_thread_data()", referenced from:
      boost::detail::interruption_checker::interruption_checker(_opaque_pthread_mutex_t*, _opaque_pthread_cond_t*) in webclient.cpp.o
ld: symbol(s) not found for architecture x86_64

看起来 this 可能与您遇到的问题相同,答案是 link 反对 -lcrypto

编辑:您的问题是您正在尝试构建一个需要针对其他“库”进行 linked 的程序 - 也就是说,通常预编译的其他代码片段 - 即在你系统的某个地方,它定义了“符号”,这基本上是另一种表达变量或函数名称的方式。

您通过在编译命令中包含参数来让编译器知道您需要哪些库,这些参数表示您需要 linked - 在这种情况下,您的前几个问题消失了,因为您 linked with -lcrypto,从那时起你的代码就能够找到 'symbols' - 即变量和函数名称 - 以前是 'undefined.'

对于您添加 'lcrypto,' 的其他问题,尝试使用 this wiki 中的参数在 Linux 上构建 cpprestsdk。标志似乎是:

-lboost_system -lssl -lcpprest -lboost_chrono

最后但并非最不重要的一点是,您是否按照 mac OSX 和 their site? 上的说明构建此软件,这可能会让您省去很多痛苦! :)

编辑:添加了一些。

原题:

  "web::uri_builder::append_path(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)", referenced from:
      std::__1::__function::__func<main::$_0, std::__1::allocator<main::$_0>, pplx::task<web::http::http_response> (Concurrency::streams::basic_ostream<unsigned char>)>::operator()(Concurrency::streams::basic_ostream<unsigned char>&&) in webclient.cpp.o
  "web::uri_builder::append_query(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)", referenced from:
      web::uri_builder& web::uri_builder::append_query<char [37]>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const (&) [37], bool) in webclient.cpp.o
  "web::uri_builder::to_string()", referenced from:
      std::__1::__function::__func<main::$_0, std::__1::allocator<main::$_0>, pplx::task<web::http::http_response> (Concurrency::streams::basic_ostream<unsigned char>)>::operator()(Concurrency::streams::basic_ostream<unsigned char>&&) in webclient.cpp.o
  "web::uri::encode_impl(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::function<bool (int)> const&)", referenced from:
      web::uri_builder& web::uri_builder::append_query<char [37]>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const (&) [37], bool) in webclient.cpp.o
  ...

::__1 是 LLVM 的匿名命名空间。您在上面看到的是混合和匹配运行时的症状。即 -stdlib=XXX.

的使用不一致

您需要使用 GNU 的工具和 -stlib=libstdc++ 构建所有内容;或者您需要使用 LLVM 的工具和 -stdlib=libc++.

构建所有内容

我发现最能避免用户问题和问题的方法是始终在 OS X 和 iOS 上使用 LLVM 的工具。即,始终使用 -stdlib=libc++.


更新问题:

Undefined symbols for architecture x86_64:
  "boost::this_thread::interruption_point()", referenced from:
      boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in webclient.cpp.o
      boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, timespec const&) in webclient.cpp.o
  "boost::chrono::steady_clock::now()", referenced from:
      pplx::details::event_impl::wait(unsigned int) in webclient.cpp.o
  "boost::chrono::system_clock::now()", referenced from:
      pplx::details::event_impl::wait(unsigned int) in webclient.cpp.o
  "boost::detail::get_current_thread_data()", referenced from:
      boost::detail::interruption_checker::interruption_checker(_opaque_pthread_mutex_t*, _opaque_pthread_cond_t*) in webclient.cpp.o
ld: symbol(s) not found for architecture x86_64

有关更新后的问题,请参阅 Boost 用户指南中的 Using and building the library。您必须构建具有线程支持的 Boost(Brew 会这样做吗?),并且您必须 link 到 Boost 线程库(CMake 会这样做吗?)。

我不使用 Boost 或 CMake,所以我不能带你进一步。抱歉。


set(OPT_CPPFLAGS "-I/usr/local/opt/openssl/include -I/usr/local/opt/libiconv/include")

我不是 CMake 专家,但这也可能有问题。 CPPFLAGS 是 C 预处理器的标志。它们可能会或可能不会添加到 CFLAGSCXXFLAGS.

为了以防万一,您绝对应该为 CFLAGSCXXFLAGS 调出相同的标志。如果这是一个 Autotools 项目,那么我会将 should 更改为 must。但是就像我说的,无论怎么想,我都不是 CMake 专家。


最后一点……您不能使用 CMake 构建 OpenSSL。你必须configure an build it yourself。安装后,您可以以 find-openssl.cmake 类型的方式引用已安装的 OpenSSL。

原因不明... OpenSSL 的 Configure 脚本设置了一些重要的设置,稍后将在构建过程中使用。两个最重要的设置文件是 <opensslconf.h> and <bn.h>.

您还可以通过 Configureenable-ec_nistp_64_gcc_128 结合,在基于 Intel 的 x86_64 机器上获得令人印象深刻的 Diffie-Hellman 和椭圆曲线速度提升(2 倍到 4 倍)。 =34=]

缺少符号名称是由于缺少库。必要的 CPPFLAGSLDFLAGS 是:

CPPFLAGS = -stdlib=libc++
LDFLAGS = -lcpprest -lboost_system -lboost_thread-mt -lboost_chrono-mt -lssl -lcrypto

非常感谢大家的帮助。