vcpkg的使用在Linux平台与Windows平台没什么差异,就不过多阐述。
有一点需要注意的是,Windows上安装的包默认是x86,而Linux上默认是x64。
vcpkg安装cpprest
cpprest库依赖很多库,比如boost,openssl等,但不用操心,vcpkg会帮我们自带安装。
./vcpkg install cpprestsdk
问题
需要注意的是,安装过程很有可能会失败,除网络原因外,还有一个地方会出错:
CMake Error at scripts/cmake/vcpkg_execute_build_process.cmake:134 (message):
Command failed: /home/vcpkg/downloads/tools/cmake-3.25.1-linux/cmake-3.25.1-linux-x86_64/bin/cmake --build . --config Debug --target install -- -v -j3
Working Directory: /home/vcpkg/buildtrees/openssl/x64-linux-dbg
See logs for more information:
/home/vcpkg/buildtrees/openssl/install-x64-linux-dbg-out.log
Call Stack (most recent call first):
installed/x64-linux/share/vcpkg-cmake/vcpkg_cmake_build.cmake:74 (vcpkg_execute_build_process)
installed/x64-linux/share/vcpkg-cmake/vcpkg_cmake_install.cmake:16 (vcpkg_cmake_build)
ports/openssl/unix/portfile.cmake:21 (vcpkg_cmake_install)
ports/openssl/portfile.cmake:33 (include)
scripts/ports.cmake:147 (include)
error: building openssl:x64-linux failed with: BUILD_FAILED
error: Please ensure you're using the latest port files with `git pull` and `vcpkg update`.
Then check for known issues at:
https://github.com/microsoft/vcpkg/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+openssl
You can submit a new issue at:
https://github.com/microsoft/vcpkg/issues/new?template=report-package-build-failure.md&title=[openssl]+Build+error
Include '[openssl] Build error' in your bug report title, the following version information in your bug description, and attach any relevant failure logs from above.
vcpkg-tool version: 2023-01-24-8a88d63f241d391772fbde69af9cab96c3c64c75
vcpkg-scripts version: 91dd61bd4 2023-01-27 (2 days ago)
通过查看/home/vcpkg/buildtrees/openssl/install-x64-linux-dbg-out.log
日志可以发现
[1/3] cd /home/Listening/vcpkg/buildtrees/openssl/x64-linux-dbg/nssl-3.0.7-96d825e305.clean && export PATH=/bin:/opt/rh/devtoolset-8/root/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/bin:/home/Listening/vcpkg/downloads/tools/ninja/1.10.2-linux && export CC=/opt/rh/devtoolset-8/root/usr/bin/cc && export AR=/opt/rh/devtoolset-8/root/usr/bin/ar && export LD=/opt/rh/devtoolset-8/root/usr/bin/ld && export RANLIB=/opt/rh/devtoolset-8/root/usr/bin/ranlib && export MAKE=/bin/make && export MAKEDEPPROG=/opt/rh/devtoolset-8/root/usr/bin/cc && export WINDRES= && /bin/perl Configure no-shared no-module enable-static-engine no-zlib no-ssl2 no-idea no-cast no-seed no-md2 no-tests linux-x86_64 --prefix=/home/Listening/vcpkg/packages/openssl_x64-linux/debug --libdir=/home/Listening/vcpkg/packages/openssl_x64-linux/debug/lib --openssldir=/etc/ssl "-fPIC -g" && /home/Listening/vcpkg/downloads/tools/cmake-3.25.1-linux/cmake-3.25.1-linux-x86_64/bin/cmake -DDIR=/home/Listening/vcpkg/buildtrees/openssl/x64-linux-dbg/nssl-3.0.7-96d825e305.clean -P /home/Listening/vcpkg/ports/openssl/unix/remove-deps.cmake
FAILED: nssl-3.0.7-96d825e305.clean/Makefile /home/Listening/vcpkg/buildtrees/openssl/x64-linux-dbg/nssl-3.0.7-96d825e305.clean/Makefile
cd /home/Listening/vcpkg/buildtrees/openssl/x64-linux-dbg/nssl-3.0.7-96d825e305.clean && export PATH=/bin:/opt/rh/devtoolset-8/root/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/bin:/home/Listening/vcpkg/downloads/tools/ninja/1.10.2-linux && export CC=/opt/rh/devtoolset-8/root/usr/bin/cc && export AR=/opt/rh/devtoolset-8/root/usr/bin/ar && export LD=/opt/rh/devtoolset-8/root/usr/bin/ld && export RANLIB=/opt/rh/devtoolset-8/root/usr/bin/ranlib && export MAKE=/bin/make && export MAKEDEPPROG=/opt/rh/devtoolset-8/root/usr/bin/cc && export WINDRES= && /bin/perl Configure no-shared no-module enable-static-engine no-zlib no-ssl2 no-idea no-cast no-seed no-md2 no-tests linux-x86_64 --prefix=/home/Listening/vcpkg/packages/openssl_x64-linux/debug --libdir=/home/Listening/vcpkg/packages/openssl_x64-linux/debug/lib --openssldir=/etc/ssl "-fPIC -g" && /home/Listening/vcpkg/downloads/tools/cmake-3.25.1-linux/cmake-3.25.1-linux-x86_64/bin/cmake -DDIR=/home/Listening/vcpkg/buildtrees/openssl/x64-linux-dbg/nssl-3.0.7-96d825e305.clean -P /home/Listening/vcpkg/ports/openssl/unix/remove-deps.cmake
Can't locate IPC/Cmd.pm in @INC (@INC contains: /home/Listening/vcpkg/buildtrees/openssl/x64-linux-dbg/nssl-3.0.7-96d825e305.clean/util/perl /opt/rh/devtoolset-8/root//usr/lib64/perl5/vendor_perl /opt/rh/devtoolset-8/root/usr/lib/perl5 /opt/rh/devtoolset-8/root//usr/share/perl5/vendor_perl /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 . /home/Listening/vcpkg/buildtrees/openssl/x64-linux-dbg/nssl-3.0.7-96d825e305.clean/external/perl/Text-Template-1.56/lib) at /home/Listening/vcpkg/buildtrees/openssl/x64-linux-dbg/nssl-3.0.7-96d825e305.clean/util/perl/OpenSSL/config.pm line 19.
BEGIN failed--compilation aborted at /home/Listening/vcpkg/buildtrees/openssl/x64-linux-dbg/nssl-3.0.7-96d825e305.clean/util/perl/OpenSSL/config.pm line 19.
Compilation failed in require at Configure line 23.
BEGIN failed--compilation aborted at Configure line 23.
ninja: build stopped: subcommand failed.
里面有个关键词Can't locate IPC/Cmd.pm
说明在编译OpenSSL时,需要的Perl模块 IPC::Cmd
没有安装。
解决
Perl模块 IPC::Cmd
没有安装,需要安装CPAN模块,再安装IPC::Cmd
在Linux或Unix系统中,通常可以使用包管理器安装CPAN模块。比如,在Debian或Ubuntu上,使用以下命令安装CPAN:
sudo apt-get install cpanminus
如果在其他系统上,查找与您的系统相对应的包管理器命令。比如我这里是CentOS 7系统:
sudo yum install perl-CPAN
安装完后,使用以下命令安装所需的模块:
cpanm IPC::Cmd //Debian或Ubuntu
cpan IPC::Cmd // CentOS
CMake使用cpprest
CMake中使用vcpkg安装的包,需要注意几点:
-
CMakeLists.txt
文件中设置vcpkg的root目录,且需要在project()
语句之前 - cpprest配置文件路径
cmake_minimum_required(VERSION 3.20)
# 设置VCPKG_ROOT所在的位置
set(VCPKG_ROOT "home/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE PATH "")
set(CMAKE_TOOLCHAIN_FILE ${VCPKG_ROOT})
project(demo)
set(CMAKE_CXX_STANDARD 14)
# 设置cppresst库所在路径
set(cpprest_DIR "/home/vcpkg/installed/x64-linux/share/cpprestsdk")
find_package(cpprestsdk CONFIG REQUIRED)
add_executable(demo main.cpp)
target_link_libraries(demo PRIVATE pthread cpprestsdk::cpprest)
通过CMake + vcpkg有个好处就是不用手写cpprest所依赖的库,不然target_link_libraries
还得加上boost、openssl等。
接下来,就可以使用cpprest库了:文章来源:https://www.toymoban.com/news/detail-458760.html
//main.cpp
#include <iostream>
#include <string>
#include "cpprest/http_client.h"
#include "cpprest/uri_builder.h"
#include "cpprest/http_msg.h"
#include "pplx/pplxtasks.h"
#include "cpprest/json.h"
#include "cpprest/uri.h"
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace web::json;
std::string send_request(const std::string &url, const std::string& arg) {
web::http::client::http_client client(url);
// Build request URI and start the request.
uri_builder builder(arg);
std::string re{};
client.request(methods::GET, builder.to_string())
// continue when the response is available
.then([](http_response response) -> pplx::task<web::json::value> {
// if the status is OK extract the body of the response into a JSON value
// works only when the content type is application\json
if(response.status_code() == status_codes::OK) {
return response.extract_json();
}
// return an empty JSON value
return pplx::task_from_result(web::json::value());
})
// continue when the JSON value is available
.then([&re](pplx::task<web::json::value> previousTask) {
// get the JSON value from the task and display content from it
try {
// web::json::value const& v = previousTask.get();
re = previousTask.get().serialize();
} catch (http_exception const & e) {
printf("Error exception:%s\n", e.what());
}
})
.wait();
return re;
}
int main()
{
std::string url{"http://10.2.3.4:2345"};
std::string arg{"api/storage/service-alarm-info/"};
auto result = send_request(url,arg);
std::cout<<result<<"\n";
while (1)
{
/* code */
}
return 0;
}
注意,由于是事后记录当时为了使用vcpkg + cmake+ cpprest方案所踩的坑,故有不完整的地方或遗漏,还请谅解!文章来源地址https://www.toymoban.com/news/detail-458760.html
到了这里,关于Linux上使用vcpkg + CMake配置cpprest的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!