c++ - 当我有 Visual C++ 构建工具(不是 g++)时,如何为 VSCODE 设置包含路径库目录和链接器

标签 c++ visual-studio-code

我已经将 VS code 设置为我的开发环境并使用 MSVC 构建工具(cl.exe 编译器)而不是 g++。我尝试为我的环境设置 SFML。我想知道如何设置 SFML 包含路径和库路径。此外,如何使用 cl.exe 执行静态链接。注意:我只使用 VS 代码而不是 Visual Studio 进行编程。下面是我用过的一些文件。 Tasks.jsonLaunch.jsonC_cpp_properties.json

任务.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe:",
                "${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe",
                "${workspaceFolder}\\*.cpp"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

启动.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "cl.exe - Build and debug active file",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal",
            "preLaunchTask": "C/C++: cl.exe build active file"
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${default}"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.30.30705/bin/Hostx64/x64/cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

最佳答案

如评论中所述,您需要使用 C++ 构建系统来管理依赖项和构建项目。 VSCode 不像 Visual Studio 那样带有任何内置构建系统。

VSCode 任务允许您指定一个命令行,然后可以在 IDE 中轻松调用它。显示的任务只是一个“构建事件文件”任务,它只对没有依赖关系的普通程序真正有用。它调用 cl.exe在当前源文件上(并传递一些其他参数)。

您可以通过添加到任务中的“args”数组来指定包含目录并将参数传递给链接器,例如:

"/I", "D:\\Code Libraries\\boost_1_77_0",
"/link", "/LIBPATH:\"D:\\Code Libraries\\boost_1_77_0\\stage\\lib\"",

假设 boost 头文件和(静态构建的)库位于指定位置。

您可能会通过添加带有 VSCode 任务的命令行来弄清楚如何构建整个项目,但使用构建系统(即使该系统是 CMake)可能更容易。

关于c++ - 当我有 Visual C++ 构建工具(不是 g++)时,如何为 VSCODE 设置包含路径库目录和链接器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70695086/

相关文章:

C++——最快的整数类型?

c++ - 制作对象的优先队列

php - 算法实现对比c++和php

python - VS Code - 使用 Conda/Pip 安装 autopep8 时出错

c++ - 'g++' 不是内部或外部命令,也不是可运行的程序或批处理文件。

visual-studio-code - 如何在 Visual Studio 代码中默认为 "*.*"而不是 "Plain Text"?

css - 如何在 vscode 中从 css lint 中排除文件

c++ - 如何在 MFC View 上显示 OpenCV Mat

c++ - 如何打印数组元素的索引号?

python - 如何在VScode中设置jupyter的运行文件路径?