本文基于的開發(fā)環(huán)境說明
- window系統(tǒng):Windows 10 企業(yè)版 64位操作系統(tǒng)
- Linux系統(tǒng):BigCloud Enterprise Linux 7.8 (Core)
- Visual Studio:Microsoft Visual Studio Enterprise 2019 版本 16.10.4
安裝 Visual Studio Linux 工作負(fù)載
根據(jù)Microsoft官網(wǎng)文檔介紹,
開始之前
首先,請確保已安裝 Visual Studio Linux 工作負(fù)載,包括 CMake 組件。 它屬于 Visual Studio 安裝程序中的“使用 C++ 的 Linux 開發(fā)”工作負(fù)載。 如果不確定是否安裝了此項,請參閱在官方說明《 Visual Studio 中安裝 C++ Linux 工作負(fù)載》。
使用 Visual Studio Installer 安裝即可。
在Linux系統(tǒng)上安裝依賴工具
根據(jù)Microsoft官網(wǎng)文檔介紹,
此外,請確保在遠(yuǎn)程計算機上安裝了以下項:
gcc
gdb
rsync
zip
ninja-build(Visual Studio 2019 或更高版本)
可以使用 Visual Studio 2019 在遠(yuǎn)程 Linux 系統(tǒng)或 WSL 上生成和調(diào)試,CMake 將在該系統(tǒng)上調(diào)用。 應(yīng)在目標(biāo)計算機上安裝 Cmake 版本 3.14 或更高版本。
請確保目標(biāo)計算機具有最新版本的 CMake。 發(fā)行版的默認(rèn)包管理器提供的版本通常不夠新,不足以支持 Visual Studio 所需的所有功能。 Visual Studio 2019 會檢測 Linux 系統(tǒng)上是否安裝了最新版本的 CMake。 如果未找到,Visual Studio 將在編輯器窗格頂部顯示一個信息欄。 它用于從 https://github.com/Microsoft/CMake/releases 為你安裝 CMake。
上述啰嗦了一堆,其實就是讓我們在連接的遠(yuǎn)程Linux服務(wù)器上安裝gcc、gdb、rsync、zip、ninja-build、CMake等工具。特別強調(diào)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調(diào)試工具,使用命令 rpm -qa | grep gdb, 如果有安裝則跳過此步驟,如果沒有安裝則使用命令 yum install gdb 進行安裝就可以了
這里先查看gdb版本,發(fā)現(xiàn)沒有安裝,所以經(jīng)使用安裝命令安裝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.
>>>>>>>>>>>>>> 省略部分內(nèi)容顯示 <<<<<<<<<<<<<<
[root@localhost ~]#
這里 zip 工具已經(jīng)安裝了 Zip 3.0 版本,就不在安裝了。
5. 安裝ninja-build
ninja-build 的安裝請參考文章《ninja在BC-Linux服務(wù)器上源碼編譯和安裝》,該文章中講述了多種安裝方式,挑選自己喜歡的方式安裝即可,此處不再累贅。這里已經(jīng)進行了安裝,安裝版本為當(dāng)前 release 最新版 1.10.2,如下:
[root@localhost ~]# ninja --version
1.10.2
[root@localhost ~]#
6. 安裝CMake
首先查看當(dāng)前是否安裝了 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
>>>>>>>>>>>>>> 省略部分內(nèi)容顯示 <<<<<<<<<<<<<<
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 無法達(dá)到要求。由于本章講述的內(nèi)容對 CMake 的版本有要求,所以準(zhǔn)備直接安裝 GitHub 上的 CMake 最新版本來用。
CMake 最新版本的安裝請參考《CMake在BC-Linux服務(wù)器上源碼編譯和安裝》進行安裝,此處不再累贅。這里已經(jīng)安裝了當(dāng)前最新版本 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]#
到這里依賴的工具就安裝完成了。
創(chuàng)建 CMake 項目
打開 Visual Studio,點擊創(chuàng)建新項目,進入如下頁面。
創(chuàng)建后如下圖所示
連接到目標(biāo)遠(yuǎn)程Linux系統(tǒng)
1. 在遠(yuǎn)程 Linux 系統(tǒng)上安裝 openssh
OpenSSH 是 SSH (Secure SHell) 協(xié)議的免費開源實現(xiàn)。SSH協(xié)議族可以用來進行遠(yuǎn)程控制, 或在計算機之間傳送文件。SSH是加密的,更安全。
Visual Studio 就是通過 SSH 和遠(yuǎn)程 Linux 系統(tǒng)進行遠(yuǎn)程控制和文件傳輸?shù)摹?/p>
[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
啟動并查看狀態(tài)
[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服務(wù)已經(jīng)啟動,并且偵聽端口為 22。
Visual Studio配置連接
Visual Studio編譯配置
保存后 [輸出] 窗口會有如下信息:
1> 已為配置“x64-Debug (默認(rèn)值)”啟動 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 (默認(rèn)值)” -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 (默認(rèn)值)
1> [CMake] – Configuring done
1> [CMake] – Generating done
1> [CMake] – Build files have been written to: D:/MyProTest/MyC++/CMakeTest/out/build/x64-Debug (默認(rèn)值)
1> 已提取 CMake 變量。
1> 已提取源文件和標(biāo)頭。
1> 已提取代碼模型。
1> 已提取包含路徑。
1> CMake 生成完畢。
說明配置OK。
編譯代碼
按F6,使用 Linux-gcc-Debug 配置進行編譯代碼,出現(xiàn)一個錯誤:找不到C++編譯器。因此我們需要安裝一個C++編譯器。
如果錯誤是 No CMAKE_CXX_COMPILER could be found.,也是因為沒有安裝C++編譯器導(dǎo)致的。
[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.
>>>>>>>>>>>>>> 省略部分內(nèi)容顯示 <<<<<<<<<<<<<<
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]#
再次編譯代碼:
在遠(yuǎn)程Linux機器上驗證
上面提示生成了一個可執(zhí)行文件CMakeTest文件,所以可以在遠(yuǎn)程Linux上看下在配置的路徑下是否有這個文件。上面在設(shè)置生成路徑的時候路徑為:/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]#
檢查發(fā)現(xiàn)確實有一個 CMakeTest 的可執(zhí)行文件。
執(zhí)行驗證如下:文章來源:http://www.zghlxwxcb.cn/news/detail-644901.html
[root@localhost Linux-GCC-Debug]# ./CMakeTest
Hello CMake.
[root@localhost Linux-GCC-Debug]#
執(zhí)行文件后,打印出了代碼中編寫的 Hello CMake. 內(nèi)容。文章來源地址http://www.zghlxwxcb.cn/news/detail-644901.html
好了,Visual Studio連接Linux服務(wù)器編譯CMake項目 的分享完畢,希望幫到你,謝謝閱覽。
到了這里,關(guān)于Visual Studio連接Linux服務(wù)器編譯CMake項目,生成在Linux上運行的程序的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!