Visual Studio连接Linux服务器编译CMake项目,生成在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以上的版本,最好是最新版本

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,如下:

[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,点击创建新项目,进入如下页面。
创建新项目
添加项目名称和位置
创建后如下图所示
![项目创建完成](https://img-blog.csdnimg.cn/f08e76106fd4463eaba3efc8975b4490.png

连接到目标远程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配置连接

配置Linux远程服务器

Visual Studio编译配置

进入管理配置
添加Linux-gcc编译配置
设置Linux-gcc-Debug编译配置

保存后 [输出] 窗口会有如下信息:

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。

编译代码

编译源代码
按F6,使用 Linux-gcc-Debug 配置进行编译代码,出现一个错误:找不到C++编译器。因此我们需要安装一个C++编译器。

如果错误是 No CMAKE_CXX_COMPILER could be found.,也是因为没有安装C++编译器导致的。

[root@localhost vspro]# yum install gcc-c++
Last metadata expiration check: 1:10:23 ago on Sun 05 Mar 2023 09:03:07 PM EST.
Dependencies resolved.
                                           
>>>>>>>>>>>>>> 省略部分内容显示 <<<<<<<<<<<<<<
Installed:
  gcc-c++-8.3.1-5.0.1.an8.x86_64             libstdc++-devel-8.3.1-5.0.1.an8.x86_64            

Complete!
[root@localhost vspro]# 

再次编译代码:
再次编译代码

在远程Linux机器上验证

上面提示生成了一个可执行文件CMakeTest文件,所以可以在远程Linux上看下在配置的路径下是否有这个文件。上面在设置生成路径的时候路径为:/home/vspro/,所以我们在这个目录下搜索。

[root@localhost Linux-GCC-Debug]# find /home/vspro/ -name CMakeTest 
/home/vspro/.vs/CMakeTest
/home/vspro/.vs/CMakeTest/1597f888-7125-4f93-814d-8e7c3dacbecc/out/build/Linux-GCC-Debug/CMakeTest
[root@localhost Linux-GCC-Debug]# cd /home/vspro/.vs/CMakeTest/1597f888-7125-4f93-814d-8e7c3dacbecc/out/build/Linux-GCC-Debug/
[root@localhost Linux-GCC-Debug]# ls -l
total 56
-rw-r--r--. 1 root root 16885 Mar  5 22:16 build.ninja
-rw-r--r--. 1 root root 14290 Mar  5 22:16 CMakeCache.txt
drwxr-xr-x. 6 root root   188 Mar  5 22:16 CMakeFiles
-rw-r--r--. 1 root root  1797 Mar  5 22:16 cmake_install.cmake
-rwxr-xr-x. 1 root root 13408 Mar  5 22:26 CMakeTest
[root@localhost Linux-GCC-Debug]# 

检查发现确实有一个 CMakeTest 的可执行文件。
执行验证如下:

[root@localhost Linux-GCC-Debug]# ./CMakeTest 
Hello CMake.
[root@localhost Linux-GCC-Debug]# 

执行文件后,打印出了代码中编写的 Hello CMake. 内容。


好了,Visual Studio连接Linux服务器编译CMake项目 的分享完毕,希望帮到你,谢谢阅览。

  • 15
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫叶2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值