转自:Visual Studio连接Linux服务器编译CMake项目,生成在Linux上运行的程序_linux_枫叶2000-Linux
本文基于的开发环境说明
- window系统:Windows 10 企业版 64位操作系统
- Linux系统:BigCloud Enterprise Linux 7.8 (Core)
- Visual Studio:Microsoft Visual Studio Enterprise 2019 版本 16.10.4
安装 Visual Studio Linux 工作负载
根据Microsoft官网文档介绍,
开始之前
首先,请确保已安装 Visual Studio Linux 工作负载,包括 CMake 组件。 它属于 Visual Studio 安装程序中的“使用 C++ 的 Linux 开发”工作负载。 如果不确定是否安装了此项,请参阅在官方说明《 Visual Studio 中安装 C++ Linux 工作负载》。
使用 Visual Studio Installer 安装即可。
在Linux系统上安装依赖工具
根据Microsoft官网文档介绍,
此外,请确保在远程计算机上安装了以下项:
gcc
gdb
rsync
zip
ninja-build(Visual Studio 2019 或更高版本)
可以使用 Visual Studio 2019 在远程 Linux 系统或 WSL 上生成和调试,CMake 将在该系统上调用。 应在目标计算机上安装 Cmake 版本 3.14 或更高版本。
请确保目标计算机具有最新版本的 CMake。 发行版的默认包管理器提供的版本通常不够新,不足以支持 Visual Studio 所需的所有功能。 Visual Studio 2019 会检测 Linux 系统上是否安装了最新版本的 CMake。 如果未找到,Visual Studio 将在编辑器窗格顶部显示一个信息栏。 它用于从 https://github.com/Microsoft/CMake/releases 为你安装 CMake。
上述啰嗦了一堆,其实就是让我们在连接的远程Linux服务器上安装gcc、gdb、rsync、zip、ninja-build、CMake等工具。特别强调CMake要用3.14以上的版本,最好是最新版本。
centos下载安装命令是yum,ubuntu的下载安装命令是apt-get,如:
sudo apt-get update
:更新软件源列表34。sudo apt-get install package_name
:安装指定的软件包34。sudo apt-get remove package_name
:卸载指定的软件包34。
1. 安装gcc
首先查看是否安装了gcc编译器,使用命令 rpm -qa | grep gcc, 如果有安装则跳过此步骤,如果没有安装则使用命令 yum install gcc 进行安装就可以了
[root@localhost ~]# rpm -qa | grep gcc [root@localhost ~]# yum install gcc Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile No package g++ available. Resolving Dependencies --> Running transaction check ---> Package gcc.x86_64 0:4.8.5-39.el7 will be installed >>>>>>>>>>>>>> 中间的安装过程省略 <<<<<<<<<<<<<< Installed: gcc.x86_64 0:4.8.5-39.el7 Dependency Installed: cpp.x86_64 0:4.8.5-39.el7 glibc-devel.x86_64 0:2.17-326.el7_9 glibc-headers.x86_64 0:2.17-326.el7_9 kernel-headers.x86_64 0:3.10.0-1127.19.1.el7 libmpc.x86_64 0:1.0.1-3.el7 mpfr.x86_64 0:3.1.1-4.el7 Dependency Updated: glibc.x86_64 0:2.17-326.el7_9 glibc-common.x86_64 0:2.17-326.el7_9 Complete! [root@localhost ~]#
查看安装的gcc是 4.8.5 版本,使用命令 man gcc 查看可知是支持 C++11 的。
[root@localhost ~]# gcc --version gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39) Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@localhost ~]#
2. 安装gdb
首先查看是否安装了gdb调试工具,使用命令 rpm -qa | grep gdb, 如果有安装则跳过此步骤,如果没有安装则使用命令 yum install gdb 进行安装就可以了
这里先查看gdb版本,发现没有安装,所以经使用安装命令安装gdb
[root@localhost ~]# rpm -qa | grep gdb [root@localhost ~]# yum install gdb Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package gdb.x86_64 0:7.6.1-119.el7 will be installed --> Finished Dependency Resolution >>>>>>>>>>>>>> 中间的安装过程省略 <<<<<<<<<<<<<< Installed: gdb.x86_64 0:7.6.1-119.el7 Complete! [root@localhost ~]#
然后再次查看gdb安装版本就有了,这里安装的是gdb-7.6.1版本。
[root@localhost ~]# gdb --version GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-119.el7 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. [root@localhost ~]#
3. 安装rsync
首先查看是否安装了rsync工具,使用命令 rpm -qa | grep rsync, 如果有安装则跳过此步骤,如果没有安装则使用命令 yum install rsync 进行安装就可以了
[root@localhost ~]# rpm -qa | grep rsync [root@localhost ~]# yum install rsync Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package rsync.x86_64 0:3.1.2-10.el7 will be installed --> Finished Dependency Resolution >>>>>>>>>>>>>> 中间的安装过程省略 <<<<<<<<<<<<<< Installed: rsync.x86_64 0:3.1.2-10.el7 Complete! [root@localhost ~]
这里安装的是rsync 3.1.2版本。
[root@localhost ~]# rsync --version rsync version 3.1.2 protocol version 31 Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others. Web site: http://rsync.samba.org/ Capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, append, ACLs, xattrs, iconv, symtimes, prealloc rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details. [root@localhost ~]#
4. 安装zip
和前面一样,查看是否安装了 zip 工具,使用命令 rpm -qa | grep zip, 如果有安装则跳过此步骤,如果没有安装则使用命令 yum install zip 进行安装就可以了
[root@localhost ~]# rpm -qa | grep zip unzip-6.0-21.el7.x86_64 bzip2-libs-1.0.6-13.el7.x86_64 bzip2-1.0.6-13.el7.x86_64 zip-3.0-11.el7.x86_64 gzip-1.5-10.el7.x86_64 [root@localhost ~]# zip --version Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license. This is Zip 3.0 (July 5th 2008), by Info-ZIP. Currently maintained by E. Gordon. Please send bug reports to the authors using the web page at www.info-zip.org; see README for details. >>>>>>>>>>>>>> 省略部分内容显示 <<<<<<<<<<<<<< [root@localhost ~]#
这里 zip 工具已经安装了 Zip 3.0 版本,就不在安装了。
5. 安装ninja-build
ninja-build 的安装请参考文章《ninja在BC-Linux服务器上源码编译和安装》,该文章中讲述了多种安装方式,挑选自己喜欢的方式安装即可,此处不再累赘。这里已经进行了安装,安装版本为当前 release 最新版 1.10.2,如下:
re2c在BC-Linux服务器上源码编译和安装-CSDN博客
re2c源码在githup的路径
https://github.com/skvadrik/re2c/tree/2.2
ninja源码路径:
GitHub - ninja-build/ninja at v1.10.0
[root@localhost ~]# ninja --version 1.10.2 [root@localhost ~]#
6. 安装CMake
首先查看当前是否安装了 cmake,以及其版本:
[root@localhost ~]# rpm -qa | grep cmake [root@localhost ~]#
我这里查看没有安装 cmake,因此先来使用 yum install cmake 来安装,看能不能满足要求。
[root@localhost ~]# yum install cmake Configuration file /etc/yum/pluginconf.d/license-manager.conf not found Unable to find configuration file for plugin license-manager Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * epel: hkg.mirror.rackspace.com Resolving Dependencies --> Running transaction check ---> Package cmake.x86_64 0:2.8.12.2-2.el7 will be installed >>>>>>>>>>>>>> 省略部分内容显示 <<<<<<<<<<<<<< Installed: cmake.x86_64 0:2.8.12.2-2.el7 Dependency Installed: libarchive.x86_64 0:3.1.2-14.el7_7 Complete! [root@localhost ~]# cmake --version cmake version 2.8.12.2 [root@localhost ~]#
这里安装的版本是 CMake 2.8.12.2,版本小于 官方要求的 3.14 版本,所以 yum 软件仓库 中的 cmake 无法达到要求。由于本章讲述的内容对 CMake 的版本有要求,所以准备直接安装 GitHub 上的 CMake 最新版本来用。
CMake 最新版本的安装请参考《CMake在BC-Linux服务器上源码编译和安装》进行安装,此处不再累赘。这里已经安装了当前最新版本 3.26,如下:
[root@localhost bin]# cmake3 --version cmake version 3.26.20230303-gc465e0e CMake suite maintained and supported by Kitware (kitware.com/cmake). [root@localhost bin]#
到这里依赖的工具就安装完成了。
创建 CMake 项目
打开 Visual Studio,点击创建新项目,进入如下页面。
创建后如下图所示
连接到目标远程Linux系统
1. 在远程 Linux 系统上安装 openssh
OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现。SSH协议族可以用来进行远程控制, 或在计算机之间传送文件。SSH是加密的,更安全。
Visual Studio 就是通过 SSH 和远程 Linux 系统进行远程控制和文件传输的。
[root@localhost ~]# yum install openssh-server Last metadata expiration check: 0:11:41 ago on Sun 05 Mar 2023 09:03:07 PM EST. Package openssh-server-8.0p1-4.el8.x86_64 is already installed. >>>>>>>>>>>>>> 中间的安装过程省略 <<<<<<<<<<<<<< Upgraded: openssh-8.0p1-13.el8.x86_64 openssh-clients-8.0p1-13.el8.x86_64 openssh-server-8.0p1-13.el8.x86_64 Complete! [root@localhost ~]#
2. 启动 openssh
启动并查看状态
[root@localhost bin]# systemctl start sshd [root@localhost bin]# service sshd status Redirecting to /bin/systemctl status sshd.service ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2023-03-05 21:14:55 EST; 18min ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 14515 (sshd) Tasks: 1 (limit: 4773) Memory: 1.2M CGroup: /system.slice/sshd.service └─14515 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openss> Mar 05 21:14:55 localhost.localdomain systemd[1]: Starting OpenSSH server daemon... Mar 05 21:14:55 localhost.localdomain sshd[14515]: Server listening on 0.0.0.0 port 22. Mar 05 21:14:55 localhost.localdomain sshd[14515]: Server listening on :: port 22. Mar 05 21:14:55 localhost.localdomain systemd[1]: Started OpenSSH server daemon.
由上看到,openssh服务已经启动,并且侦听端口为 22。
Visual Studio配置连接
Visual Studio编译配置
上述如果是新创建的项目,只需要选择远程计算机名称,然后保存,即可。其他的用默认,可以省去很多错误。不然改了,很有可能生成不成功。
保存后 [输出] 窗口会有如下信息:
1> 已为配置“x64-Debug (默认值)”启动 CMake 生成。
1> 命令行: “cmd.exe” /c “%SYSTEMROOT%\System32\chcp.com 65001 >NUL && “C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\ENTERPRISE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe” -G “Ninja” -DCMAKE_BUILD_TYPE:STRING=“Debug” -DCMAKE_INSTALL_PREFIX:PATH=“D:\MyProTest\MyC++\CMakeTest\out\install\x64-Debug (默认值)” -DCMAKE_C_COMPILER:FILEPATH=“C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe” -DCMAKE_CXX_COMPILER:FILEPATH=“C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe” -DCMAKE_MAKE_PROGRAM=“C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\ENTERPRISE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe” “D:\MyProTest\MyC++\CMakeTest” 2>&1”
1> 工作目录: D:\MyProTest\MyC++\CMakeTest\out\build\x64-Debug (默认值)
1> [CMake] – Configuring done
1> [CMake] – Generating done
1> [CMake] – Build files have been written to: D:/MyProTest/MyC++/CMakeTest/out/build/x64-Debug (默认值)
1> 已提取 CMake 变量。
1> 已提取源文件和标头。
1> 已提取代码模型。
1> 已提取包含路径。
1> CMake 生成完毕。
说明配置OK。
编译项目:
在解决方案上点击切换到可用视图,
双击CMake目标视图,显示可执行文件
此一步很关键。我之前是直接创建完项目后,手动写完cmakelist.txt文件后,怎么也生成不了可执行文件。后来重新创建新项目,在cmakesetting.json那里只改动远程计算机名,其他保持不了,然后到这一步,才看到有可执行文件生成。
1.在cmaketest(可执行文件)上右键点击,选择生成,即进行编译代码,在linux远程主机上生成可执行文件。生成结果如下:
开始将文件复制到远程计算机。
[rsync] rsync -t --delete --delete-excluded -v -r --exclude=.vs --exclude=.git --exclude=out -8 "." rsync://root@localhost:33253/-root-.vs-cmaketest-2d45102d-3a90-461b-a638-e6e6656d92ad-src
[rsync] sending incremental file list
[rsync] deleting cmaketest/cmaketest.h
[rsync] deleting cmaketest/cmaketest.cpp
[rsync] cmaketest/
[rsync] cmaketest/CMakeLists.txt
[rsync]
[rsync] sent 2,261 bytes received 127 bytes 955.20 bytes/sec
[rsync] total size is 10,586,266 speedup is 4,433.11
已完成复制文件(运行时间 00h:00m:03s:171ms)。
cd $HOME/.vs/cmaketest/2d45102d-3a90-461b-a638-e6e6656d92ad/out/build/Linux-GCC-Debug;/usr/local/bin/cmake --build $HOME/.vs/cmaketest/2d45102d-3a90-461b-a638-e6e6656d92ad/out/build/Linux-GCC-Debug --target cmaketest --config Debug;
ninja: no work to do.
表示生成可执行文件成功。
2.在cmaketest(可执行文件)上右键点击,选择调试,即可进行项目的调试,打断点,单步调试等。
此时去linux下的目录
/root/.vs/cmaketest/2d45102d-3a90-461b-a638-e6e6656d92ad/out/build/Linux-GCC-Debug
下看到有可执行文件生成了:
安装ninja参考如下文章:
CentOS 6.X 安装使用Ninja_centos ninja-CSDN博客
先从官方下载Ninja的Linux二进制文件ninja,目前最新版本为1.9.0,解压并复制到/usr/local/bin,下载路径如下:
Releases · ninja-build/ninja · GitHub
可以直接下载源码编译。