windows下使用microsoft、intel、gnu不同編譯器利用cmake和vscode對(duì)c++和fortran程序進(jìn)行編譯和調(diào)試
由于編譯器支持特性、編譯后程序運(yùn)行速度、安裝使用便捷程度等的不同,我們往往會(huì)安裝多種不同的編譯器。對(duì)于c++語(yǔ)言主要的編譯器有:microsoft、intel、gnu等,對(duì)于fortran語(yǔ)言則更多,包括gnu、intel、pgi等等。不同的編譯器在一個(gè)系統(tǒng)下,往往需要利用一些手段進(jìn)行區(qū)分,比如環(huán)境變量的臨時(shí)設(shè)置等方式,便于區(qū)別使用。
本文介紹一下對(duì)于同一個(gè)程序利用不同的編譯器進(jìn)行編譯的不同方法。
1. 編譯器準(zhǔn)備
本文介紹主要針對(duì)c++和fortran,但以c++為例,fortran的方式是類(lèi)似的。
c++程序可以采用3種編譯器:microsoft、intel、gnu。
其中前兩種編譯器的安裝在【前文】 介紹過(guò)了,這里不再說(shuō)明。
GNU編譯器在windows最常用的是mingw和cygwin。這里我們使用【mingw-w64】。幾年前mingw-w64主要下載的工具是【MingW-W64-builds工具】。但近今年維護(hù)似乎不再持續(xù),而轉(zhuǎn)向msys2了。所以下載msys2【下載地址】 即可。msys2用法參考其【官網(wǎng)】 。需要注意的是,下載安裝msys2后只有msys2支持環(huán)境,編譯工具需要另外下載,打開(kāi)msys2命令行,輸入如下命令下載編譯工具:
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
編譯器下載完成后,需要將工具所在路徑加入系統(tǒng)路徑,如:
D:\msys64\mingw64\bin
在命令行下使用命令path或者env可以看到該路徑已經(jīng)加入了系統(tǒng)路徑中。
注意:如果是在msys2提供的命令行下工作,那么可以修改home下的.bashrc文件,將mingw的地址加入到msys2的系統(tǒng)目錄中,比如:
export PATH="/mingw64/bin/:$PATH"
如此每次打開(kāi)msys2命令行就能找到g++等工具了。觀測(cè)path可知:
2. 編譯工具和編輯器準(zhǔn)備
編譯工具我們使用命令行和cmake,【前文】介紹過(guò),安裝即可 。
編輯器使用【vscode】 。
需要安裝C/C++,C/C++ Extension Pack 兩個(gè)插件,后者已經(jīng)包含了CMake Tools插件。
3. 程序準(zhǔn)備
C/C++測(cè)試程序使用一個(gè)簡(jiǎn)單的程序(這個(gè)示例是vscode網(wǎng)址上的)
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int, char**) {
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
cout << "Hello, world!\n";
int i=0;
for (const string& word : msg)
{
cout << word << " ";
i++;
}
cout << endl;
}
4. 命令行結(jié)合cmake使用三種編譯器編譯c++程序
4.1 使用GNU編譯器
建立一個(gè)文件夾,放入c++程序,并創(chuàng)建一個(gè)CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# set the project name
project(main)
# add the executable
add_executable(main main.cpp)
enable_testing()
add_test(NAME test_serial COMMAND main)
然后進(jìn)入普通的命令行,使用如下命令進(jìn)行編譯和測(cè)試:
cmake -G "MinGW Makefiles"
cmake --build .
ctest -VV
結(jié)果為:
D:\test\projects\icltestcmkcgnu>cmake -G "MinGW Makefiles"
CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/msys64/mingw64/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/msys64/mingw64/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/test/projects/icltestcmkcgnu
D:\test\projects\icltestcmkcgnu>cmake --build .
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.obj
[100%] Linking CXX executable main.exe
[100%] Built target main
D:\test\projects\icltestcmkcgnu>ctest -VV
UpdateCTestConfiguration from :D:/test/projects/icltestcmkcgnu/DartConfiguration.tcl
UpdateCTestConfiguration from :D:/test/projects/icltestcmkcgnu/DartConfiguration.tcl
Test project D:/test/projects/icltestcmkcgnu
Constructing a list of tests
Done constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
test 1
Start 1: test_serial
1: Test command: D:\test\projects\icltestcmkcgnu\main.exe
1: Test timeout computed to be: 10000000
1: Hello, world!
1: Hello C++ World from VS Code and the C++ extension!
1/1 Test #1: test_serial ...................... Passed 0.46 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.48 sec
D:\test\projects\icltestcmkcgnu>
4.2 使用ms編譯器
建立一個(gè)文件夾,放入c++程序,并創(chuàng)建一個(gè)CMakeLists.txt,與前一小結(jié)相同。
然后使用如下命令進(jìn)行編譯和測(cè)試:
cmake .
cmake --build .
ctest -C Debug -VV
注意:進(jìn)入的命令行是Developer Command Prompt for VS 2019,這是安裝ms build工具得到的。
注意:ctest的時(shí)候使用選項(xiàng)-C Debug
,因?yàn)閙s這種編譯器區(qū)分不同的模式。
D:\test\projects\icltestcmkcms>cmake .
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.18363.
-- The C compiler identification is MSVC 19.29.30139.0
-- The CXX compiler identification is MSVC 19.29.30139.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/test/projects/icltestcmkcms
D:\test\projects\icltestcmkcms>cmake --build .
用于 .NET Framework 的 Microsoft (R) 生成引擎版本 16.11.2+f32259642
版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。
Checking Build System
Building Custom Rule D:/test/projects/icltestcmkcms/CMakeLists.txt
main.cpp
main.vcxproj -> D:\test\projects\icltestcmkcms\Debug\main.exe
Building Custom Rule D:/test/projects/icltestcmkcms/CMakeLists.txt
D:\test\projects\icltestcmkcms>ctest -C Debug -VV
UpdateCTestConfiguration from :D:/test/projects/icltestcmkcms/DartConfiguration.tcl
UpdateCTestConfiguration from :D:/test/projects/icltestcmkcms/DartConfiguration.tcl
Test project D:/test/projects/icltestcmkcms
Constructing a list of tests
Done constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
test 1
Start 1: test_serial
1: Test command: D:\test\projects\icltestcmkcms\Debug\main.exe
1: Test timeout computed to be: 10000000
1: Hello, world!
1: Hello C++ World from VS Code and the C++ extension!
1/1 Test #1: test_serial ...................... Passed 0.32 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.35 sec
D:\test\projects\icltestcmkcms>
4.3 使用intel編譯器
建立一個(gè)文件夾,放入c++程序,并創(chuàng)建一個(gè)CMakeLists.txt,與前一小結(jié)相同。
然后使用如下命令進(jìn)行編譯和測(cè)試:
cmake -T "Intel C++ Compiler 2022" -DCMAKE_CXX_COMPILER="icl" .
cmake --build .
ctest -C Debug -VV
注意:進(jìn)入的命令行是Intel oneAPI command prompt for Intel 64 for Visual Studio 2019,這是安裝intel oneapi工具得到的。
結(jié)果為:
D:\test\projects\icltestcmkcintel>cmake -T "Intel C++ Compiler 2022" -DCMAKE_CXX_COMPILER="icl" .
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.18363.
-- The C compiler identification is IntelLLVM 2022.0.0 with MSVC-like command-line
-- The CXX compiler identification is IntelLLVM 2022.0.0 with MSVC-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: D:/Program Files/intel/compiler/2022.0.0/windows/bin/intel64/icl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/Program Files/intel/compiler/2022.0.0/windows/bin/intel64/icl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/test/projects/icltestcmkcintel
D:\test\projects\icltestcmkcintel>cmake --build .
用于 .NET Framework 的 Microsoft (R) 生成引擎版本 16.11.2+f32259642
版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。
Checking Build System
Building Custom Rule D:/test/projects/icltestcmkcintel/CMakeLists.txt
Building Custom Rule D:/test/projects/icltestcmkcintel/CMakeLists.txt
D:\test\projects\icltestcmkcintel>ctest -C Debug -VV
UpdateCTestConfiguration from :D:/test/projects/icltestcmkcintel/DartConfiguration.tcl
UpdateCTestConfiguration from :D:/test/projects/icltestcmkcintel/DartConfiguration.tcl
Test project D:/test/projects/icltestcmkcintel
Constructing a list of tests
Done constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
test 1
Start 1: test_serial
1: Test command: D:\test\projects\icltestcmkcintel\Debug\main.exe
1: Test timeout computed to be: 10000000
1: Hello, world!
1: Hello C++ World from VS Code and the C++ extension!
1/1 Test #1: test_serial ...................... Passed 0.32 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.34 sec
D:\test\projects\icltestcmkcintel>
4.4 fortran程序
方法是類(lèi)似的,創(chuàng)建一個(gè)CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
enable_language(Fortran)
# set the project name
project(main)
# add the executable
add_executable(main main.f90)
enable_testing()
add_test(NAME test_serial COMMAND main)
GNU編譯器,進(jìn)入普通的命令行,用命令:
cmake -G "MinGW Makefiles"
cmake --build .
ctest -VV
編譯。
Intel編譯器,則進(jìn)入Intel oneAPI command prompt for Intel 64 for Visual Studio 2019命令行,
使用命令:
cmake -G "NMake Makefiles"
cmake --build .
ctest -VV
編譯。
5. 使用vscode和三種編譯器編譯調(diào)試c++程序
5.1 gnu 編譯器
使用一般的命令行,進(jìn)入當(dāng)前c++程序所在的文件夾,使用命令:
code .
打開(kāi)vscode。
從vscode菜單:終端-》配置默認(rèn)生成任務(wù),選擇g++,會(huì)生成tasks.JSON,自動(dòng)放在文件加下的.vscode下。
其內(nèi)容為:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活動(dòng)文件",
"command": "D:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "編譯器: D:\\msys64\\mingw64\\bin\\g++.exe"
}
]
}
在編輯器中選擇cpp文件,并
從vscode菜單:終端-》運(yùn)行任務(wù),選擇g++,這時(shí)可以生成執(zhí)行文件。
在編輯器中選擇cpp文件,并
從vscode菜單:運(yùn)行-》添加配置,選項(xiàng)gdb,可以生成調(diào)試所需的json配置,自動(dòng)放在文件加下的.vscode下。需要注意:若沒(méi)有g(shù)db選項(xiàng),可以隨意選擇,然后用下述json配置替換即可。
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "d:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活動(dòng)文件"
}
]
}
修改其中的"stopAtEntry": true,
,那么可使在調(diào)試時(shí)逐句執(zhí)行。
在編輯器中選擇cpp文件,并
從vscode菜單:運(yùn)行-》啟動(dòng)調(diào)試,則可進(jìn)入調(diào)試。
從調(diào)試欄中選擇單步跳過(guò),不斷執(zhí)行,可以在左側(cè)欄中看到變量的變化。
結(jié)果為:
5.2 ms 編譯器
使用命令行Developer Command Prompt for VS 2019,進(jìn)入當(dāng)前c++程序所在的文件夾,使用命令:
code .
打開(kāi)vscode。
從vscode菜單:終端-》配置默認(rèn)生成任務(wù),選擇C/C++: cl.exe 生成活動(dòng)文件
,會(huì)生成tasks.JSON,自動(dòng)放在文件加下的.vscode下。
其內(nèi)容為:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe 生成活動(dòng)文件",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "編譯器: cl.exe"
}
]
}
在編輯器中選擇cpp文件,并
從vscode菜單:終端-》運(yùn)行任務(wù),選擇cl.exe,這時(shí)可以生成執(zhí)行文件。
在編輯器中選擇cpp文件,并
從vscode菜單:運(yùn)行-》添加配置,選項(xiàng)c++(windows),可以生成調(diào)試所需的json配置,
,自動(dòng)放在文件加下的.vscode下。
{
// 使用 IntelliSense 了解相關(guān)屬性。
// 懸停以查看現(xiàn)有屬性的描述。
// 欲了解更多信息,請(qǐng)?jiān)L問(wèn): https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "cl.exe - 生成和調(diào)試活動(dòng)文件",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"console": "externalTerminal",
"preLaunchTask": "C/C++: cl.exe 生成活動(dòng)文件"
}
]
}
修改其中的"stopAtEntry": true,
,那么可使在調(diào)試時(shí)逐句執(zhí)行。
在編輯器中選擇cpp文件,并
從vscode菜單:運(yùn)行-》啟動(dòng)調(diào)試,則可進(jìn)入調(diào)試。
從調(diào)試欄中選擇單步跳過(guò),不斷執(zhí)行,可以在左側(cè)欄中看到變量的變化。
結(jié)果為:
5.3 intel 編譯器
將c++編譯器更改為intel編譯可以進(jìn)行編譯和運(yùn)行。但目前無(wú)法使用oneapigdb進(jìn)行調(diào)試(估計(jì)以后這個(gè)問(wèn)題會(huì)解決。)
首先設(shè)置c++編譯器:
從vscode菜單:查看-》命令面板,選擇C/C++ 編輯配置
,找到編譯器路徑
,設(shè)置為指定的intel編譯器:D:/Program Files/intel/compiler/latest/windows/bin/intel64/icl.exe
如此可以得到一個(gè)c_cpp_properties.json
自動(dòng)放在.vscode
目錄下。
內(nèi)容為:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "D:/Program Files/intel/compiler/latest/windows/bin/intel64/icl.exe",
"cStandard": "c17",
"cppStandard": "c++17"
}
],
"version": 4
}
從vscode菜單:終端-》配置默認(rèn)生成任務(wù),選擇C/C++: icl.exe 生成活動(dòng)文件
,會(huì)生成tasks.JSON,自動(dòng)放在文件加下的.vscode下。
其內(nèi)容為:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: icl.exe 生成活動(dòng)文件",
"command": "D:/Program Files/intel/compiler/latest/windows/bin/intel64/icl.exe",
"args": [
"-fdiagnostics-color=always",
"/Zi",
"/Od",
"/Z7",
"/debug:all",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:/Program Files/intel/compiler/latest/windows/bin/intel64"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "編譯器: \"D:/Program Files/intel/compiler/latest/windows/bin/intel64/icl.exe\""
}
]
}
在編輯器中選擇cpp文件,并
從vscode菜單:終端-》運(yùn)行任務(wù),選擇icl.exe,這時(shí)可以生成執(zhí)行文件。
在編輯器中選擇cpp文件,并
從vscode菜單:運(yùn)行-》添加配置,選項(xiàng)oneapi,可以生成調(diào)試所需的json配置,
,自動(dòng)放在文件加下的.vscode下。
在編輯器中選擇cpp文件,并
從vscode菜單:運(yùn)行-》啟動(dòng)調(diào)試,則可進(jìn)入調(diào)試,但實(shí)際只能執(zhí)行而無(wú)法調(diào)試。
測(cè)試表明windows下無(wú)論gdb-oneapi、gdb都無(wú)法進(jìn)行有效調(diào)試,直接在命令行下執(zhí)行也不行。
結(jié)果為:
這個(gè)調(diào)試問(wèn)題目前看windows下是沒(méi)有解決的。從如下問(wèn)題可以看到。
【How do I read files with gdb-oneapi, Intel’s debugger, on Windows?】
【Debugging Intel C++ compiled code with GDB】
【Intel’s oneapi C compiler not producing debug information [duplicate]】
5.4 命令行g(shù)db調(diào)試
這里只介紹簡(jiǎn)單的命令:
進(jìn)入調(diào)試:
gdb execfile
或者
gdb
file execfile
symbol-file symbofile
執(zhí)行:
run
列出源代碼:
list
加入斷點(diǎn)
break linenumber
逐步執(zhí)行:
step
6. 使用vscode和cmake及gnu和ms編譯器編譯調(diào)試c++程序
6.1 gnu編譯器
使用一般的命令行,進(jìn)入當(dāng)前c++程序所在的文件夾,使用命令:
code .
打開(kāi)vscode。
從vscode菜單:查看-》命令面板,輸入cmake
,選擇cmake:配置
,自動(dòng)進(jìn)行cmake配置。
從vscode菜單:查看-》命令面板,輸入cmake
,選擇cmake:生成
,編譯程序。
從左側(cè)文件欄可以看到生成了執(zhí)行程序main.exe
在build
目錄下面。
執(zhí)行程序,只要在終端進(jìn)入build
目錄,直接輸入main.exe
回車(chē)即可。
cmake調(diào)試程序,首先需要插入斷點(diǎn),然后從vscode菜單:查看-》命令面板,輸入cmake
,選擇cmake:調(diào)試
,對(duì)程序進(jìn)行調(diào)試,從調(diào)試欄中選擇單步跳過(guò),不斷執(zhí)行,可以在左側(cè)欄中看到變量的變化。
結(jié)果為:
6.2 ms編譯器
使用命令行Developer Command Prompt for VS 2019,進(jìn)入當(dāng)前c++程序所在的文件夾,使用命令:
code .
打開(kāi)vscode。
從vscode菜單:查看-》命令面板,輸入cmake
,選擇cmake:配置
,自動(dòng)進(jìn)行cmake配置。
從vscode菜單:查看-》命令面板,輸入cmake
,選擇cmake:生成
,編譯程序。
從左側(cè)文件欄可以看到在build目錄下,debug目錄下生成了執(zhí)行程序main.exe
。
在終端進(jìn)入改目錄,直接輸入main.exe
回車(chē)即可執(zhí)行程序。
cmake調(diào)試程序,首先需要插入斷點(diǎn),然后從vscode菜單:查看-》命令面板,輸入cmake
,選擇cmake:調(diào)試
,對(duì)程序進(jìn)行調(diào)試,從調(diào)試欄中選擇單步跳過(guò),不斷執(zhí)行,可以在左側(cè)欄中看到變量的變化。
結(jié)果為:
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-447102.html
7. 小結(jié)
本文初步總結(jié)了windows下使用不同編譯器編譯c++和fortran程序的方法,以及結(jié)合vscode和cmake進(jìn)行調(diào)試的方法,為windows下c++程序和fortran程序編譯提供示范。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-447102.html
參考
- https://code.visualstudio.com/docs/cpp/cmake-linux#_create-a-cmake-hello-world-project
- https://devblogs.microsoft.com/cppblog/cmake-tools-extension-for-visual-studio-code/
- https://blog.csdn.net/u013525455/article/details/52813637
到了這里,關(guān)于windows下使用microsoft、intel、gnu不同編譯器利用cmake和vscode對(duì)c++和fortran程序進(jìn)行編譯和調(diào)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!