Visual Studio连接Linux服务器编译CMake项目,生成在Linux上运行的程序

这篇具有很好参考价值的文章主要介绍了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 安装即可。
vslinux,Linux札记,linux,服务器,visual studio,c++

在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,点击创建新项目,进入如下页面。
vslinux,Linux札记,linux,服务器,visual studio,c++
vslinux,Linux札记,linux,服务器,visual studio,c++
创建后如下图所示
vslinux,Linux札记,linux,服务器,visual studio,c++

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

vslinux,Linux札记,linux,服务器,visual studio,c++

Visual Studio编译配置

vslinux,Linux札记,linux,服务器,visual studio,c++
vslinux,Linux札记,linux,服务器,visual studio,c++
vslinux,Linux札记,linux,服务器,visual studio,c++

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

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。

编译代码

vslinux,Linux札记,linux,服务器,visual studio,c++
按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]# 

再次编译代码:
vslinux,Linux札记,linux,服务器,visual studio,c++

在远程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. 内容。文章来源地址https://www.toymoban.com/news/detail-644901.html


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

到了这里,关于Visual Studio连接Linux服务器编译CMake项目,生成在Linux上运行的程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • visual studio 编译cmake项目

    当编译出现问题 message(FATAL_ERROR \\\"The C compilern  \\\"${CMAKE_C_COMPILER}\\\"n\\\"       \\\"is not able to compile a simple test program.nIt fails \\\"       \\\"with the following output:n  ${_output}nn\\\"       \\\"CMake will not be able to correctly generate this project.\\\") 控制台提示如下: 1 已为默认配置“x64-Debug”启动 CMake 生成

    2024年02月06日
    浏览(49)
  • C++服务器框架开发11——编译调试1/cmake学习

    该专栏记录了在学习一个开发项目的过程中遇到的疑惑和问题。 其教学视频见:[C++高级教程]从零开始开发服务器框架(sylar) 上一篇:C++服务器框架开发10——日志系统1~9代码 学习到第6个视频的00:59,由于不了解编译,这次先学习下cmake。下图是CMakeLists.txt中的内容。 参考自文

    2024年02月16日
    浏览(49)
  • vscode(visual studio code) 免密登陆服务器

    1.生成密钥 首先,在本地,打开命令输入框: Win+R–弹出输入框,输入cmd,打开命令框。 然后,在命令框,输入 按两次回车键,问你是否重写,选择y。 接着,输入两次回车,即输入空白密码,,没有密码。 最后,便可以看到,在本地的目录下,生成了公钥和私钥。 选择画红

    2024年01月16日
    浏览(59)
  • Visual Studio 2022 取消 Microsoft符号服务器的方法

    最近使用 Visual Studio 2022 调试时,发现一直在加载某些DLL文件,造成调试卡住,等了很久都不正常 点击取消 符号服务器的加载,马上就能正常的进入调试,好像这个对调试影响不大 但是每次调试,都会加载这个:【Microsoft符号服务器】 菜单栏【工具】-- 【选项】 在搜索栏里

    2024年02月13日
    浏览(52)
  • Visual Studio使用Web Deploy发布.NET Web应用到指定服务器的IIS中

    今天要讲的是在Window 2008 R2版本的服务器下如何配置Web Deploy并使用Visual Studio进行.NET Web项目发布部署。 Web Deploy其实主要是为了解决Web应用程序和Web站点到IIS服务器的部署问题,管理员可以使用Web Deploy同步IIS服务器或迁移到较新版本的IIS。Web Deploy Tool还使管理员和委派用户能

    2024年01月25日
    浏览(50)
  • Goland连接服务器/虚拟机远程编译开发

    SSH用于与远程服务器建立连接 Settings - Tools - SSH Configurations 添加新的ssh连接,Host为ip地址,Username为用户名,认证方式这里选择密码验证 全部填完后可以点击Test Connection测试连接是否成功 Deployment用于构建本地与远程服务器的路径映射 Settings - Build,Execution,Deployment - Deployment 添

    2024年02月06日
    浏览(43)
  • SQL Server Management Studio(SSMS)连接服务器失败

    情况如下图所示: step1:打开 SQL Server配置管理器 step2:启动 SQL SERVER 至此问题就解决了~ 参考博客 SQL Server Management Studio(SSMS)无法连接到服务器,及解决方案_不支持通过ssms的专用管理员连接-CSDN博客 文章浏览阅读1.5w次,点赞13次,收藏48次。很多初次使用SSMS的小伙伴,由

    2024年04月27日
    浏览(39)
  • Linux环境编译服务器中修改python版本方式

    一、系统级修改 1、用whereis python查看服务器中存在的python 版本 2、删除原有的python版本环境 3、切换python 版本 4、查看python 版本 二、用户级修改 1、查看home/username目录下有没有以下两个隐藏文件 如果没有上面两个文件,从 /etc/skel/目录拷贝.bashrc、.profile两个文件到home/user

    2024年02月09日
    浏览(40)
  • vscode连接linux服务器

    目录 下载vscode,这是微软开源软件,打开后到下载扩展页面 在下载扩展页面下载中文和ssh远程连接扩展 安装后会在左边新生成一个图标点击齿轮 选择第一个 配置连接信息 远程隧道右边刷新,等刷出来hostname的主机后 连接ip出来后,点击右边箭头 在软件页面中间部分选择

    2024年02月03日
    浏览(64)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包