将编译器命令行转换为 Casablanca Rest SDK 的 CMakeLists.txt
Posted
技术标签:
【中文标题】将编译器命令行转换为 Casablanca Rest SDK 的 CMakeLists.txt【英文标题】:Convert compiler command line to CMakeLists.txt for Casablanca Rest SDK 【发布时间】:2018-03-14 08:56:45 【问题描述】:我很难将编译器命令行调用转换为 CMakeLists.txt,目标是使用 Microsoft Rest SDK casablanca。 因为我是新手,所以我不知道我的 CMakeLists.txt 有什么问题
我使用它成功构建(在 macOS 上):
$ clang++ request.cpp -o request -std=c++11 -Wall -stdlib=libc++ -I/usr/local/Cellar/cpprestsdk/2.9.1/include/ -I/usr/local/Cellar/openssl/1.0.2l/include/ -L/usr/local/Cellar/openssl/1.0.2l/lib/ -lssl -lcrypto -lcpprest -lboost_system -lboost_thread-mt -lboost_chrono-mt
但运行 cmake 和 make 我得到:
clang: warning: -lssl: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lcrypto: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lcpprest: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lboost_system: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lboost_thread-mt: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lboost_chrono-mt: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-L/usr/local/Cellar/openssl/1.0.2l/lib/' [-Wunused-command-line-argument]
/Users/mtobal/Documents/exercises/sandbox/request.cpp:2:10: fatal error: 'cpprest/http_client.h' file not found
#include <cpprest/http_client.h>
^~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/request.dir/request.cpp.o] Error 1
make[1]: *** [CMakeFiles/request.dir/all] Error 2
make: *** [all] Error 2
我知道问题在于找不到库,因为我设法通过命令行调用解决了问题,但在 CMakeLists.txt 上却没有。 有什么想法吗?
CMakeLists.txt:
cmake_minimum_required (VERSION 2.8.11)
project (REST)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_COMPILER "clang++")
SET(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS $GCC_COVERAGE_COMPILE_FLAGS -stdlib=libc++ -I/usr/local/Cellar/openssl/1.0.2l/include/ -L/usr/local/Cellar/openssl/1.0.2l/lib/ -lssl -lcrypto -lcpprest -lboost_system -lboost_thread-mt -lboost_chrono-mt ")
set(CMAKE_EXE_LINKER_FLAGS "$CMAKE_EXE_LINKER_FLAGS -stdlib=libc++ -I/usr/local/Cellar/openssl/1.0.2l/include/ -L/usr/local/Cellar/openssl/1.0.2l/lib/ -lssl -lcrypto -lcpprest -lboost_system -lboost_thread-mt -lboost_chrono-mt ")
include_directories("/usr/local/Cellar/cpprestsdk/2.9.1/include")
include_directories("/usr/local/Cellar/boost/1.64.0_1/include")
include_directories("/usr/local/Cellar/openssl/1.0.2l/include")
link_directories("/usr/local/Cellar/openssl/1.0.2l/lib/")
link_libraries(ssl crypto cpprest boost_system boost_thread-mt boost_chrono-mt)
#find_library(ssl crypto cpprest boost_system boost_thread-mt boost_chrono-mt)
set(SOURCE_FILES request.cpp)
add_executable (request $SOURCE_FILES)
C++ 代码:
#include <iostream>
#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()
// std::cout << "Hello again!" << std::endl;
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());
-- 更新-- 现在尝试这个(遵循 Tsyvarev 的建议)一些路径版本是错误的:
cmake_minimum_required (VERSION 2.8.11)
project (rest)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_COMPILER "g++")
include_directories("/usr/local/Cellar/cpprestsdk/2.10.2/include")
include_directories("/usr/local/Cellar/boost/1.66.0/include")
include_directories("/usr/local/Cellar/openssl/1.0.2l/include")
link_directories("/usr/local/Cellar/openssl/1.0.2l/lib/")
link_directories("/usr/local/Cellar/cpprestsdk/2.10.2/include")
link_directories("/usr/local/Cellar/boost/1.66.0/include")
link_libraries(ssl crypto cpprest boost_system boost_thread-mt boost_chrono-mt)
set(SOURCE_FILES request.cpp)
add_executable (request $SOURCE_FILES)
现在我明白了:
[ 50%] Linking CXX executable request
ld: library not found for -lcpprest
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [request] Error 1
make[1]: *** [CMakeFiles/request.dir/all] Error 2
make: *** [all] Error 2
【问题讨论】:
I have no idea what's wrong in my CMakeLists.txt
- 然后显示你的CMakeLists.txt
。
完成了,我忘了包括它!现在 CMakeLists.txt 在那里
从 CMAKE_CXX_FLAGS 和 CMAKE_EXE_LINKER_FLAGS 变量中删除 -I
、-l
和 -L
标志 - 您已经设置了与这些相对应的东西标志,通过 CMake 命令。这样,您将消除“输入未使用”的警告。然后检查,你确实有一个文件/usr/local/Cellar/cpprestsdk/2.9.1/include/cpprest/http_client.h
。
现在出现不同的错误...我已经更新了我的 CMakeLists.txt
在link_directories("/usr/local/Cellar/cpprestsdk/2.10.2/include")
中,您可能需要lib
子目录,而不是include
。下一次link_directories
命令调用也是如此。
【参考方案1】:
好的,感谢 Tsyvarev,它现在可以构建。 如前所述,它应该指向链接目录的 lib。 检查安装的软件(库)的版本也很重要,否则找不到任何库。
我在 MacOS 上使用 clang 构建的最终 CMakeLists.txt 是:
cmake_minimum_required (VERSION 2.8.11)
project (rest)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_COMPILER "clang++")
include_directories("/usr/local/Cellar/cpprestsdk/2.10.2/include")
include_directories("/usr/local/Cellar/boost/1.66.0/include")
include_directories("/usr/local/Cellar/openssl/1.0.2l/include")
link_directories("/usr/local/Cellar/openssl/1.0.2l/lib/")
link_directories("/usr/local/Cellar/cpprestsdk/2.10.2/lib")
link_directories("/usr/local/Cellar/boost/1.66.0/lib")
link_libraries(ssl crypto boost_system boost_thread-mt boost_chrono-mt cpprest)
set(SOURCE_FILES request.cpp)
add_executable (request $SOURCE_FILES)
【讨论】:
以上是关于将编译器命令行转换为 Casablanca Rest SDK 的 CMakeLists.txt的主要内容,如果未能解决你的问题,请参考以下文章
怎么将.jpg .bmp .gif 图片转换成 iso exe res dll 等格式?
使用 C++ REST SDK (Casablanca) 从文件中读取 JSON