1.背景
C/C++項(xiàng)目的構(gòu)建編譯方式有多種,如在Windows平臺上直接使用Visual Studio IDE集成的編譯工具鏈進(jìn)行構(gòu)建編譯,或CMake進(jìn)行構(gòu)建編譯。當(dāng)然,如果對于跨平臺的IDE,可選Visual Studio Code(簡稱VSCode)。本文將針對VSCode配置C/C++的2種構(gòu)建編譯方式以及相關(guān)問題進(jìn)行介紹,即VSCode自有C/C++構(gòu)建編譯模式配置和CMake構(gòu)建編譯模式配置。
2.在VSCode上的兩種C/C++構(gòu)建編譯配置
2.1. 方式一:VSCode自有C/C++構(gòu)建編譯模式配置
個人推測該配置模式為微軟官方仿照Visual Studio IDE的C/C++設(shè)計(jì)的。
(1)詳細(xì)配置過程,請參考文檔和配套視頻。
(2).vscode隱藏目錄中的三個配置文件示例
- c_cpp_properties.json(編譯器路徑和智能代碼提示配置文件)
該文件主要負(fù)責(zé)編譯器的路徑設(shè)置和代碼智能提示,個人感覺該文件的參數(shù)配置并不影響項(xiàng)目編譯。這里,參數(shù)macFrameworkPath
、compilerPath
、compilerArgs
可設(shè)置為CommandLineTools的C/C++環(huán)境,也可以設(shè)置為XCode自帶的C/C++環(huán)境(如下示例)。
{
"configurations": [
{
"name": "Mac-C",
"includePath": [
"/usr/local/include",
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "macos-clang-x64",
"compilerArgs": [
"-L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib"
]
},
{
"name": "Mac-C++",
"includePath": [
"/usr/local/include",
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "macos-clang-x64",
"compilerArgs": [
"-L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib"
]
}
],
"version": 4
}
如果,基于CommandLineTools的C/C++環(huán)境設(shè)置參數(shù)macFrameworkPath
、compilerPath
、compilerArgs
,則有:
"macFrameworkPath": ["/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/System/Library/Frameworks"],
"compilerPath": "/Library/Developer/CommandLineTools/usr/bin/g++",
"compilerArgs": ["-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"],
- tasks.json(編譯器構(gòu)建配置文件)
該文件主要用于編譯參數(shù)的配置。這里的command
、args
的最后一個參數(shù)-L/***、detail
均可參考c_cpp_properties.json中對應(yīng)參數(shù)設(shè)置。
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ 生成活動文件",
"command": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"args": [
"-std=c++14",
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${workspaceFolder}/${workspaceRootFolderName}",
"-L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "編譯器: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++"
}
]
}
- launch.json(調(diào)試器設(shè)置配置文件)
該文件用于代碼調(diào)試配置。
{
// 使用 IntelliSense 了解相關(guān)屬性。
// 懸停以查看現(xiàn)有屬性的描述。
// 欲了解更多信息,請?jiān)L問 https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) 啟動",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${workspaceRootFolderName}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
注意一:
c_cpp_properties.json和tasks.json這兩個文件的配置項(xiàng)實(shí)際上是相互獨(dú)立的。它們之間沒有強(qiáng)制性的依賴關(guān)系。也就是說,c_cpp_properties.json 的配置項(xiàng)并不會影響 tasks.json,而 tasks.json 的配置項(xiàng)也不會影響 c_cpp_properties.json。
不過,在實(shí)際使用中,這兩個文件的配置項(xiàng)可能會有一些重疊的部分,例如編譯器的路徑、編譯選項(xiàng)等。這時候,你需要確保這些重疊部分的配置項(xiàng)是一致的,以避免可能的問題和錯誤。
注意二:
關(guān)于引用第三方庫,如OpenCV,配置方式參考博客。
2.2. 方式二:CMake構(gòu)建編譯模式配置
CMake編譯模式的配置過程,網(wǎng)上的博客比較多,這里推薦參考博客。
注意:在此需要強(qiáng)調(diào)的是,CMake構(gòu)建編譯模式的配置過程與方式一并沒有關(guān)系,也即并不依賴方式一的配置文件,是一種獨(dú)立的構(gòu)建編譯模式。
3. CommandLineTools與Xcode的關(guān)系
CommandLineTools與Xcode是兩個不同的工具集,但它們可以一起使用。
Xcode是蘋果公司開發(fā)的一款綜合性集成開發(fā)環(huán)境(IDE),主要用于開發(fā)macOS、iOS、watchOS和tvOS等操作系統(tǒng)的應(yīng)用程序。它包括了許多工具,如圖形化界面設(shè)計(jì)器、代碼編輯器、調(diào)試器、工程構(gòu)建工具等。
CommandLineTools是Xcode的一部分,它是一個獨(dú)立的開發(fā)者工具包,可以用于在終端中編譯和構(gòu)建軟件,而無需啟動Xcode本身。它包括了大量的開發(fā)工具和庫,如編譯器、調(diào)試器、構(gòu)建工具make、版本控制工具git等等。文章來源:http://www.zghlxwxcb.cn/news/detail-499994.html
因此,如果你只需要使用其中的一些工具,并不需要安裝整個Xcode,那么你可以只安裝CommandLineTools。但是,如果你需要使用Xcode中的完整功能(如圖形化界面設(shè)計(jì)器等),則還需要安裝Xcode。文章來源地址http://www.zghlxwxcb.cn/news/detail-499994.html
到了這里,關(guān)于MacOS + VSCode配置C/C++環(huán)境的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!