2016-05-30 173 views
21

我試圖在Visual Studio代碼中構建C/C++。我安裝了C/C++和所有相關的擴展。Visual Studio代碼includePath

#include <stdio.h> 
int main() { 
    printf("Test C now\n"); 
    return 0; 
} 

但有#include <stdio.h>下綠線說:「添加包括路徑設置」。當我點擊它時,它會移到「c_cpp_properties.json」。

如何以及在哪裏可以在下面的配置中添加包含路徑?

"configurations": [ 
    { 
     "name": "Mac", 
     "includePath": ["/usr/include"] 
    } 
] 

回答

24

如何和我在哪裏可以添加包括在下面的配置路徑?

該列表是一個逗號分隔列表,因此添加一個包含路徑看起來像是;

"configurations": [ 
    { 
     "name": "Mac", 
     "includePath": ["/usr/local/include", 
      "/path/to/additional/includes", 
      "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include" 
     ] 
    } 
] 

Source; cpptools blog 31 March 2016

鏈接的源文件有一個顯示Win32配置格式的gif,但其他格式也適用。

如果安裝了Xcode,上述示例包含SDK(OSX 10.11)路徑

注意我發現一旦包含路徑被更改可能需要一段時間才能更新。

cpptools擴展可以是found here

有關VSCode中C++語言支持的其他文檔(來自Microsoft)可以是found here


對於保存的緣故(從討論),下面是用於tasks.json文件的內容編譯和執行任一個C++文件,或C文件的基本片斷。它們允許文件名中有空格(需要使用\"轉義json中的額外引號)。該shell使用as the runner,因此允許該程序的編譯(clang...)和執行(&& ./a.out)。它還假定tasks.json「存在於」本地工作空間(在.vscode目錄下)。進一步的task.json細節,比如支持的變量等可以是found here

對於C++;

{ 
    "version": "0.1.0", 
    "isShellCommand": true, 
    "taskName": "GenericBuild", 
    "showOutput": "always", 
    "command": "sh", 
    "suppressTaskName": false, 
    "args": ["-c", "clang++ -std=c++14 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"] 
} 

對於C;

{ 
    "version": "0.1.0", 
    "isShellCommand": true, 
    "taskName": "GenericBuild", 
    "showOutput": "always", 
    "command": "sh", 
    "suppressTaskName": false, 
    "args": ["-c", "clang -std=c11 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"] // command arguments... 
} 
+0

讓我們[在聊天中繼續討論](http://chat.stackoverflow.com/rooms/113606/discussion-between-niall-and-stdio-h)。 – Niall

+0

@ stdio.h。我推薦一本關於C的好書,深入研究這門語言;在http://stackoverflow.com/q/562303/3747990上的SO上有一個列表。如果你有興趣,還有一個C++列表; http://stackoverflow.com/q/388242/3747990。這兩份名單都至少包含一本網上可用的書籍,可以幫助您掌握並運用這些書籍,併爲您在SO和其他教程網站上找到的許多其他材料提供了良好的背景。我喜歡這些列表,因爲它們是策劃列表,而不僅僅是隨機轉儲掉的互聯網。 – Niall

+0

非常感謝您發佈此內容。節省我很多分鐘! – sivabudh

3

對於Mac用戶只安裝命令行工具,而不是Xcode中,檢查/Library/Developer/CommandLineTools/目錄,例如::

"configurations": [{ 
    "name": "Mac", 
    "includePath": [ 
     "/Library/Developer/CommandLineTools/usr/lib/clang/8.1.0/include/", 
     "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1/tr1/", 
     "/usr/include/c++/4.2.1", 
     "/usr/local/include", 
     "/usr/include" 
    ] 
}] 

Note:您還可以生成或編輯c_cpp_properties.json文件與Command Palette(⇧⌘P)中的C/Cpp: Edit Configurations命令。