VSCode c ++ task.json包含路径和库

问题描述 投票:2回答:2

IntelliSense使用c_cpp_properties.json >> includePath来查找自动完成的标头,但我注意到我仍然需要在task.json >> tasks >> args中指定include路径来构建。

我在文档中发现includePath与我在“-I”中指定的路径几乎相同:

为此设置指定的路径与通过-I开关发送到编译器的路径相同。解析源文件时,IntelliSense引擎会在尝试解析这些路径时将这些路径添加到#include指令指定的文件中。不会递归搜索这些路径。*

link

  1. 我是通过指定构建的args中的所有库和包含目录来正确设置VSCode吗?或者它应该以不同的方式完成?
  2. 有人可以解释使用不同的单词includePath和browse之间的区别是什么?解释链接对我来说并不完全清楚

这是我的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的新手(对不起)。

c++ build visual-studio-code g++
2个回答
0
投票

我也是VS Code的新手,我也从不构建更大的c ++项目,现在,我解决了像这样的构建......

我最终在我的tasks.json中做了这个...

 "tasks": [
    {
        "type": "shell",
        "label": "g++ build active file",
        "command": "${workspaceFolder}/buildMysorcery.sh",
        "options": {
            "cwd": "/usr/bin"
        }
    },

(好像你无法逃脱args属性中的空格,https://github.com/Microsoft/vscode/issues/36733),

我删除了args属性并设置了command属性来运行一个脚本(buildMysorcery.sh)

#!/bin/bash

g++ fullpathtodir/hello.cpp -Wall -g $(sdl2-config --cflags --libs) -o fullpathtodir/hello

用路径替换fullpathtodir


-2
投票

我不确定你的问题是否是一个更大问题的一个子集,那就是你的程序无法编译bcs IntelliSense包含你的外部库有问题。如果这是问题,请阅读我的帖子。如果没有,请跳过阅读本文并详细说明您的问题。

custom library addition in visual studio code

我遇到了类似的问题并通过此链接修复了它。

基本上,您需要在编译器路径的任何位置包含头库。在我的情况下,我需要将我的头文件放在C:\ TDM-GCC-64 \ x84_64-w64-mingw32 \ include中。

所以我不知道我的编译器路径是什么,所以这就是我找到它的方法。在程序中放置一个公共头文件,您知道它将编译并找到路径。在我的情况下,我把#include,因为我一直看到zmouse.h。果然,我的代码接受了包含。然后我在库上做了F12,它应该是找到标题的某些路径。这就是我如何找到路径!我希望这有助于回答你的问题

© www.soinside.com 2019 - 2024. All rights reserved.