gpt4 book ai didi

c++ - 将编译器命令行转换为 Casablanca Rest SDK 的 CMakeLists.txt

转载 作者:行者123 更新时间:2023-11-28 04:45:54 25 4
gpt4 key购买 nike

我很难将编译器命令行调用转换为 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 

但是运行 cmakemake 我得到:

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

最佳答案

好的伙计们,多亏了 Tsyvarev,它现在可以构建了。如前所述,它应该指向 link_directories 的 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})

关于c++ - 将编译器命令行转换为 Casablanca Rest SDK 的 CMakeLists.txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49273341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com