我正在尝试使用cpprest-sdk库:https://github.com/Microsoft/cpprestsdk
为rest API创建一个HTTP客户端。我正在将MacBook Pro与最新版本的MacO(10.14.2)配合使用。
我使用brew作为包管理器,并安装了boost命令:
酿造助推器
依赖性版本:
clang:Apple LLVM版本10.0.0(clang-1000.11.45.5)
cmake:3.13.2
提升:稳定的1.68.0(瓶装),HEAD
首先,我尝试遵循此规则:
https://github.com/Microsoft/cpprestsdk/wiki/How-to-build-for-Mac-OS-X
所以我也用boost安装了cpprestsdk:
加强安装cpprestsdk
然后,我在一个main.cpp中使用了本教程中的代码:
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
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.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("http://www.bing.com/"));
// Build request URI and start the request.
uri_builder builder(U("/search"));
builder.append_query(U("q"), U("cpprestsdk github"));
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());
}
return 0;
}
然后我创建了一个CMake构建文件CMakeLists.txt,如它们在cpprestsdk的自述文件中所述:
cmake_minimum_required(VERSION 3.13)
project(cpprest-example)
set(CMAKE_CXX_STANDARD 14)
find_package(cpprestsdk REQUIRED)
add_executable(cpprest-example main.cpp)
target_link_libraries(cpprest-example PRIVATE cpprestsdk::cpprest)
首先,我遇到了CMake无法优化openssl的问题,因此我不得不添加两个env参数,如下所述:
CMake not able to find OpenSSL library
然后它可以编译,但是在链接期间失败,并显示错误:
Scanning dependencies of target cpprest-example
[ 50%] Building CXX object CMakeFiles/cpprest-example.dir/main.cpp.o
[100%] Linking CXX executable cpprest-example
Undefined symbols for architecture x86_64:
"boost::this_thread::interruption_point()", referenced from:
boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in main.cpp.o
boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, boost::detail::real_platform_timepoint const&) in main.cpp.o
"boost::chrono::steady_clock::now()", referenced from:
bool boost::condition_variable::wait_for<long long, boost::ratio<1l, 1000l>, pplx::details::event_impl::wait(unsigned int)::'lambda0'()>(boost::unique_lock<boost::mutex>&, boost::chrono::duration<long long, boost::ratio<1l, 1000l> > const&, pplx::details::event_impl::wait(unsigned int)::'lambda0'()) in main.cpp.o
bool boost::condition_variable::wait_until<boost::chrono::steady_clock, boost::chrono::duration<long long, boost::ratio<1l, 1000000000l> >, pplx::details::event_impl::wait(unsigned int)::'lambda0'()>(boost::unique_lock<boost::mutex>&, boost::chrono::time_point<boost::chrono::steady_clock, boost::chrono::duration<long long, boost::ratio<1l, 1000000000l> > > const&, pplx::details::event_impl::wait(unsigned int)::'lambda0'()) in main.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 main.cpp.o
"boost::system::detail::system_category_instance", referenced from:
boost::system::system_category() in main.cpp.o
"boost::system::detail::generic_category_instance", referenced from:
boost::system::generic_category() in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cpprest-example] Error 1
make[1]: *** [CMakeFiles/cpprest-example.dir/all] Error 2
make: *** [all] Error 2
奇怪的是,如果我使用CMake构建cpprestsdk代码库,它可以正常工作,并且其中一个示例是相同的代码: https://github.com/Microsoft/cpprestsdk/tree/master/Release/samples/BingRequest
该示例项目已成功构建并运行正常。
我还尝试从brew上卸载cpprestsdk,并对克隆的存储库进行CMake构建,然后“ make -j4 && make install”。这样会在本地安装cpprestsdk标头和库,但最终会导致相同的错误。
似乎没有找到Boost库,我尝试使用find_package(BOOST 1.68.0)和类似的东西,但是由于相同的错误而失败。
任何人都可以看到我在做什么吗?