IntelliSense使用c_cpp_properties.json >> includePath查找自动完成的标头,但是我注意到我仍然需要在task.json >> task >> args内部指定包含路径。
我在文档中发现includePath与我在“ -I”中指定的路径几乎相同:
为此设置指定的路径与 您将通过-I开关将其发送到编译器。当您的来源 文件被解析后,IntelliSense引擎会将这些路径添加到 尝试在您的#include指令中指定的文件 解决它们。这些路径不是递归搜索的。*
这是我的c_cpp_properties.json的示例:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/github/dependencies/SDL2-2.0.8/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"${workspaceFolder}/**"
]
}
}
],
"version": 4
}
和task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"main2.cpp",
"-ID:\\github\\dependencies\\SDL2-2.0.8\\include",
"-LD:\\github\\dependencies\\SDL2-2.0.8\\lib\\x64",
"-lSDL2main","-lSDL2", "-lopengl32",
"-o",
"test-sdl"
]
}
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher":"$gcc"
}
这是一个简单的问题,但是我是VSCode的新手(对不起)。
答案 0 :(得分:3)
大部分。不可避免的事实是必须两次指定包含路径(一次在c_cpp_properties.json
中,一次在描述您的内部版本的文件中)。在VSCode中,构建系统和编辑器彼此不了解,并且都需要此信息。相反,使用Visual Studio(无“代码”),只需指定一次路径即可;这是使用“真正的”集成开发环境的好处之一。 (但是也有缺点;我并不是想阻止您使用VSCode。)
但是,我不建议直接将包含路径放入tasks.json
中。相反,通常会有一个可以从命令行调用的独立构建系统,然后tasks.json
也会调用该命令。
作为一个非常常见的示例,您可以使用GNU Make并用此(未经测试!)Makefile替换当前的tasks.json
:
test-sdl: main2.cpp
g++ -g main2.cpp -ID:\\github\\dependencies\\SDL2-2.0.8\\include -LD:\\github\\dependencies\\SDL2-2.0.8\\lib\\x64 -lSDL2main -lSDL2 -lopengl32 -o test-sdl
这告诉make
如何从test-sdl
构建main2.cpp
,即通过运行所示的g++
命令。 (我故意使此Makefile非常简单,因为问题不是关于Makefile的;要注意,一个真正的Makefile会破坏一些东西以便更好地组织,并且反斜杠可能需要调整。)
无论如何,您的tasks.json
可以简化为:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "make", // <-- changed
"args": [] // <-- changed
}
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher":"$gcc"
}
这更好,因为您没有将关键的构建信息锁定在只有VSCode可以理解的文件中。
VSCode具有两个用于理解C ++代码的系统。有较旧的使用browse.path
的“ Tag Parser”和较新的使用includePath
的“ Intellisense”。在这一点上(2019-08-30,VSCode 1.37.1),我的理解基本上是每个人都应该使用更新的Intellisense系统,因为它提供了更准确的信息,并且至少应该成熟。因此,您应该能够忽略 browse.path
。
要确保使用的是Intellisense而不是Tag Parser,请进入“文件”→“偏好设置”→“设置”→“ C / C ++”→“ C_Cpp:Intelli Sense Engine”,并确保它是“默认”而不是“ Tag Parser”。请注意,此设置存储在settings.json
中,而不是c_cpp_properties.json
中。
答案 1 :(得分:1)
我也曾经尝试过使用库,至少现在还行(我在Windows BTW上): 在c_cpp_properties.json中,我引用了include目录:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:\\ProgrammingLibraries\\SDL2-2.0.10\\include\\SDL2",
"${workspaceFolder}\\src\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
在tasks.json中,我有一个编译任务和一个链接程序任务,以及一个同时运行这两个任务的任务:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Compiler",
"type": "shell",
"command": "g++",
"args": [
"-c",
"${workspaceFolder}\\src\\main.cpp",
"-IC:\\ProgrammingLibraries\\SDL2-2.0.10\\include\\SDL2"
]
},
{
"label": "Linker",
"type": "shell",
"command": "g++",
"args": [
"${workspaceFolder}\\main.o",
"-o",
"${workspaceFolder}\\bin\\HelloSDL.exe",
"-LC:\\ProgrammingLibraries\\SDL2-2.0.10\\lib",
"-lmingw32",
"-lSDL2main",
"-lSDL2"
]
},
{
"label": "Build HelloSDL",
"dependsOn": [
"Compiler",
"Linker"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
答案 2 :(得分:0)
Im also new to VS Code, I also never build bigger c++ projects, for now, I solved the buildung like ths...
I ended up doing this in my tasks.json...
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "${workspaceFolder}/buildMysorcery.sh",
"options": {
"cwd": "/usr/bin"
}
},
(seems like your're not able to escape whitespaces within the args property, https://github.com/Microsoft/vscode/issues/36733),
I deleted the args property and set the command property to run a script(buildMysorcery.sh) which simply does
#!/bin/bash
g++ fullpathtodir/hello.cpp -Wall -g $(sdl2-config --cflags --libs) -o fullpathtodir/hello
Replace the fullpathtodir with your paths
答案 3 :(得分:0)
这是我在带有clang的Mac(MacBook Pro 2015,macOS Catalina)上的VS Code中包含OpenGL“ GLWF”和“ glad”库的方式。而且我具有智能,可以构建和调试。
include/glad/glad.h
-要包含的库文件
src / helloworld.cpp-主文件
/* Ask for an OpenGL Core Context */
#define GLFW_INCLUDE_GLCOREARB
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
int main(int argc, char **argv)
{
GLFWwindow *window;
/* Initialize the library */
if (!glfwInit())
{
return -1;
}
#ifdef __APPLE__
/* We need to explicitly ask for a 3.2 context on OS X */
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(1280, 720, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
.vscode / c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/src/", "${workspaceFolder}/include/"],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
.vscode / launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/build/helloworld.out",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
.vscode / tasks.json(您需要包括实现文件include/glad.c
,而不仅是标题)
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-lglfw3",
"--include-directory=include/",
"--include=include/glad.c",
"-framework",
"OpenGL",
"-framework",
"IOKit",
"-framework",
"Cocoa",
"src/helloworld.cpp",
"-o",
"build/helloworld.out",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
答案 4 :(得分:-2)
我不确定您的问题是否是更大问题的子集,因为您的程序无法编译bcs IntelliSense,包括您的外部库也遇到了麻烦。如果那是问题,请阅读我的文章。如果不是,请跳过阅读,详细说明问题所在。
custom library addition in visual studio code
我对您有类似的问题,并通过此链接修复了该问题。
基本上,无论您的编译器路径在哪里,都需要包括头文件库。就我而言,我需要将头文件放入C:\ TDM-GCC-64 \ x84_64-w64-mingw32 \ include。
所以我不知道我的编译器路径是什么,所以这就是我找到它所要做的。在您知道程序可以在其中编译并找到路径的程序中放一个公共头文件。就我而言,我放#include是因为我到处都看到zmouse.h。果然,我的代码接受了包含。然后,我在库上执行了F12键,应该在其中找到标头的某些路径。这就是我找到道路的方式!希望这对您有所帮助并回答您的问题