简体   繁体   English

如何使用 vscode 编译具有不同选项的 c 语言?

[英]How to compile c language with different options with vscode?

Some c language codes use dynamic libraries, so I need to use the -ldm option.一些 c 语言代码使用动态库,所以我需要使用-ldm选项。 Some include other static libraries and need to use -I to specify the directory.有些包括其他 static 库,需要使用-I来指定目录。 But in vscode, the common configuration is directly gcc sourcefile -o targetfile , so it can't be executed normally?但是在vscode中,常见的配置直接是gcc sourcefile -o targetfile ,所以不能正常执行? Is there a way to make vscode execute according to Makefile?有没有办法让vscode按照Makefile执行? And such a setting can automatically use this method when I use SSH to add a folder on the server to the workspace, without having to reconfigure it every time?而这样的设置在我使用SSH 将服务器上的文件夹添加到工作区时可以自动使用这种方法,而不必每次都重新配置?

Indeed it is possible to have VSCode work with make on linux or msbuild.exe on windows .实际上, makelinuxmsbuild.exe上的 msbuild.exe 上使用windows You can have different configurations within these as well so that you have have a Debug build or a Release build.您也可以在其中拥有不同的配置,以便拥有Debug构建或Release构建。 All you need to do is to have suitable tasks defined in tasks.json .您需要做的就是在tasks.json中定义合适的任务。 For instance, consider:例如,考虑:

        {
            "label": "lindbgbuild",
            "type": "shell",
            "command": "make",
            "args": [
                "CONF=Debug",
                "-C",
                "./.vscode"
            ],
            "group": "build",
            "problemMatcher": []
        },
        {
            "label": "linreleasebuild",
            "type": "shell",
            "command": "make",
            "args": [
                "CONF=Release",
                "-C",
                "./.vscode"
            ],
            "group": "build"
        },
        {
            "label": "winbuilddebug",
            "type": "shell",
            "command": "msbuild",
            "args": [
                ".vscode/windows.vcxproj",
                "/property:GenerateFullPaths=true",
                "/property:Configuration=Debug",
                "/property:Platform=x64",
                "/t:build",
                "/consoleloggerparameters:NoSummary"
            ],
            "group": "build",
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile"
        },
        {
            "label": "winbuildrelease",
            "type": "shell",
            "command": "msbuild",
            "args": [
                ".vscode/windows.vcxproj",
                "/property:GenerateFullPaths=true",
                "/property:Configuration=Release",
                "/property:Platform=x64",
                "/t:build",
                "/consoleloggerparameters:NoSummary"
            ],
            "group": "build",
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile"
        }

There are 4 tasks in the tasks.json . tasks.json中有 4 个任务。 In turn, they are:反过来,它们是:

lindbgbuild which executes command make CONF=Debug -C./.vscode lindbgbuild执行命令make CONF=Debug -C./.vscode

What this command assumes is the existence of Makefile in the ./.vscode subdirectory of the current (project) directory.此命令假定当前(项目)目录的./.vscode子目录中存在Makefile Then, it executes the Debug configuration within it.然后,它在其中执行Debug配置。 The next, linreleasebuild does the same for the Release configuration.接下来, linreleasebuildRelease配置执行相同的操作。 So, what we have accomplished is that we can run an abstract command make and then this command will call the appropriate configuration specified within Makefile .所以,我们已经完成的是我们可以运行一个抽象命令make然后这个命令将调用Makefile中指定的适当配置。 All commands like g++ -c -o -g -I , etc., are configurable outside of VSCode in a separate makefile that VSCode only calls.所有命令,如g++ -c -o -g -I等,都可以在 VSCode之外的一个单独的 makefile 中配置,VSCode 只调用它。

Then, you have a task winbuilddebug .然后,您有一个任务winbuilddebug This assumes the existence of a .vcxproj project configuration file in ./.vscode .这假定./.vscode中存在.vcxproj项目配置文件。 It then executes the appropriate configuration with the options provided (in this case, the configuration is Debug on x64 ).然后它使用提供的选项执行适当的配置(在这种情况下,配置是Debug on x64 )。 Similarly, winbuildrelease for Release configuration under Windows.同样, winbuildrelease下的Release配置的winbuildrelease。 .vcxproj is automatically generated by Visual Studio IDE and makefiles on linux can be created by getting the project built using an IDE such as Netbeans . .vcxproj is automatically generated by Visual Studio IDE and makefiles on linux can be created by getting the project built using an IDE such as Netbeans . Or else, you can modify pre-existing makefiles/.vcxproj from other pre-existing projects to suit your current project.或者,您可以从其他预先存在的项目中修改预先存在的 makefiles/.vcxproj 以适应您当前的项目。 (I suggest against this, and using an IDE for your initial build since that automates without error many settings.) (我建议不要这样做,并使用 IDE 进行初始构建,因为这会自动执行许多设置而不会出错。)

With this given tasks.json , how do you access these tasks?有了这个给定tasks.json ,你如何访问这些任务? On hitting CtrlShiftB , it opens up Select Build Tasks to Run dialog box where you can choose which task you want to run depending on which OS/Platform/compiler you are on.在点击 CtrlShiftB 时,它会打开CtrlShiftB Select Build Tasks to Run对话框,您可以在其中根据您所在的操作系统/平台/编译器选择要运行的任务。

Is there a way to automate this step and launch?有没有办法自动执行此步骤并启动? Indeed, following is an example of launch.json .实际上,以下是launch.json的示例。 Consider the following configuration:考虑以下配置:

        {
            "name": "(Windows)RelLaunch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": ".vscode/x64/Release/windows.exe",
            "args": [],
            "preLaunchTask": "winbuildrelease",
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "internalConsoleOptions": "openOnSessionStart",
        }

This will build then launch the application (assuming there are no build errors).这将build然后启动应用程序(假设没有构建错误)。 Which build configuration does it choose?它选择哪种构建配置? It will choose the winbuildrelease configuration that has been specified.它将选择已指定的winbuildrelease配置。 Like this, if you are debugging and want to step through the code, you will have a different configuration.像这样,如果你在调试并且想要单步调试代码,你会有不同的配置。

How do you choose which configuration to launch?您如何选择要启动的配置? Hit CtrlShiftD .点击CtrlShiftD This opens up the Run sidebar, where on top, you can choose from amongst the configurations you have specified in your launch.json .这将打开Run侧边栏,在顶部,您可以从您在launch.json中指定的配置中进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM