visual studio 2022 远程开发:linux+cmake+vcpkg开发环境配置

最近根据项目需求,需要搭建在widowds下 使用visual studio 2022 IDE 工具远程在Linux平台c++环境。 上查了很多资料,可供参考的资料特别少,经过多轮配置,现将配置的基本步骤分享给大家。

要求如下:

1、c++11标准

2、编译工具Cmake

3、编译器GCC

4、包管理工具Vcpkg(微软 C++ 团队开发的适用于 C 和 C++ 库的跨平台开源软件包管理器)

5、jsoncpp(一款基于c++的json三方处理库)

一、camke 安装

1、下载 址:
https://cmake.org/download/

2、版本:Cmake 3.23.1

3、安装步骤

tar -zvxf cmake-3.23.1.tar.gz cd cmake-3.23.1/ 
./bootstrap 
make & make install

如出现如下提示:请安装openssl-devel

Could not find OpenSSL. Install an OpenSSL development package or

configure CMake with -DCMAKE_USE_OPENSSL=OFF to build without OpenSSL.

rm -f CMakeCache.txt 
yum -y install ncurses-devel 
yum install openssl-devel

二、Linux 下 vcpkg安装

1、下载 址:
https://github.com/Microsoft/vcpkg

2、安装步骤

git clone https://github.com/microsoft/vcpkg
cd vcpkg
./bootstrap-vcpkg.sh 

3、设置环境变量

vi /etc/profile
export PATH=$PATH:/root/vcpkg/
source /etc/profile

三、vcpkg 下 jsoncpp 安装

vcpkg integrate install
vcpkg install jsoncpp:x64-linux

生成的提示信息,很重要,cmakeLists一定要按照如下配置

jsoncpp provides CMake targets:

# this is heuristically generated, and may not be correct

find_package(jsoncpp CONFIG REQUIRED) target_link_libraries(main PRIVATE jsoncpp_object jsoncpp_static JsonCpp::JsonCpp)

这段代码加入cmakeLists.txt

四、vs2022配置

1、配置vs2022远程跨平台链接

工具->选项->跨平台->添加SSH linux服务器链接

2、新建工程

选择,Cmake项目(生成不依赖于.sln或.vcxproj支持的跨平台C++应用)

3、cmakeLists.txt配置

# CMakeList.txt: video 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.23.1)
project ("video")
set(CMAKE_CXX_STANDARD 11)
set(SOURCES "src/")
find_package(jsoncpp CONFIG REQUIRED)
# 用于创建可执行文件,第一个参数是可执行文件名字,第二个参数为需要用到的源文件
add_executable (video "${SOURCES}main.cpp" "${SOURCES}header/main.h" "${SOURCES}OperationVideo.cpp" "${SOURCES}header/OperationVideo.h" "${SOURCES}header/OperJson.h" "${SOURCES}OperJson.cpp")
# 用于指明构建目标的依赖库,第一个参数为构建目标,第三个参数为依赖库
target_link_libraries(video PRIVATE jsoncpp_object jsoncpp_static JsonCpp::JsonCpp)

4、CmakeSettings.json配置文件如下

{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\out\build\${name}",
"installRoot": "${projectDir}\out\install\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
},
{
"addressSanitizerEnabled": false,
"buildCommandArgs": "",
"cmakeCommandArgs": "",
"cmakeExecutable": "cmake",
"cmakeToolchain": "/root/vcpkg/scripts/buildsystems/vcpkg.cmake",
"configurationType": "Debug",
"ctestCommandArgs": "",
"generator": "Unix Makefiles",
"inheritEnvironments": [ "linux_x64" ],
"intelliSenseMode": "linux-gcc-x64",
"name": "Linux-GCC-Debug",
"remoteBuildRoot": "$HOME/vs/${projectDirName}/${workspaceHash}/out/build/${name}",
"remoteCMakeListsRoot": "$HOME/vs/${projectDirName}/${workspaceHash}/src",
"remoteCopyAdditionalIncludeDirectories": [ "/usr/local/lib/", "usr/local/include/", "/root/vcpkg/installed/x64-linux/include" ],
"remoteCopyBuildOutput": true,
"remoteCopySources": true,
"remoteCopySourcesExclusionList": [ ".vs", ".git", "out" ],
"remoteCopySourcesMethod": "rsync",
"remoteInstallRoot": "$HOME/vs/${projectDirName}/${workspaceHash}/out/install/${name}",
"remoteMachineName": "-1539221974;192.168.2.188 (username=root, port=22, authentication=Password)",
"rsyncCommandArgs": "-t --delete --delete-excluded",
"variables": [
{
"name": "_VCPKG_INSTALLED_DIR",
"value": "/root/vcpkg/installed",
"type": "PATH"
},
{
"name": "VCPKG_APPLOCAL_DEPS",
"value": "true",
"type": "BOOL"
}
]
},
{
"name": "Linux-GCC-Release",
"generator": "Unix Makefiles",
"configurationType": "RelWithDebInfo",
"cmakeExecutable": "cmake",
"remoteCopySourcesExclusionList": [ ".vs", ".git", "out" ],
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "linux_x64" ],
"remoteMachineName": "${defaultRemoteMachineName}",
"remoteCMakeListsRoot": "$HOME/vs/${projectDirName}/${workspaceHash}/src",
"remoteBuildRoot": "$HOME/vs/${projectDirName}/${workspaceHash}/out/build/${name}",
"remoteInstallRoot": "$HOME/vs/${projectDirName}/${workspaceHash}/out/install/${name}",
"remoteCopySources": true,
"rsyncCommandArgs": "-t --delete --delete-excluded",
"remoteCopyBuildOutput": false,
"remoteCopySourcesMethod": "rsync"
},
{
"name": "WSL-GCC-Debug",
"generator": "Unix Makefiles",
"configurationType": "Debug",
"buildRoot": "${projectDir}\out\build\${name}",
"installRoot": "${projectDir}\out\install\${name}",
"cmakeExecutable": "cmake",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "linux_x64" ],
"wslPath": "${defaultWSLPath}"
}
]
}

5、新建main.cpp

#include "json/json.h"
#include <iostream>
#include <memory>
/**
* brief Parse a raw string into Value object using the CharReaderBuilder
* class, or the legacy Reader class.
* Example Usage:
* $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString
* $./readFromString
* colin
* 20
*/
int main() {
const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
const auto rawJsonLength = static_cast<int>(rawJson.length());
constexpr bool shouldUseOldWay = false;
JSONCPP_STRING err;
Json::Value root;
if (shouldUseOldWay) {
Json::Reader reader;
reader.parse(rawJson, root);
} else {
Json::CharReaderBuilder builder;
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
&err)) {
std::cout << "error" << std::endl;
return EXIT_FAILURE;
}
}
const std::string name = root["Name"].asString();
const int age = root["Age"].asInt();
std::cout << name << std::endl;
std::cout << age << std::endl;
return EXIT_SUCCESS;
}

以上就是vs2022远程linux下开发环境配置,亲测可以用。

声明:本站部分文章内容及图片转载于互联 、内容不代表本站观点,如有内容涉及侵权,请您立即联系本站处理,非常感谢!

(0)
上一篇 2022年5月23日
下一篇 2022年5月25日

相关推荐