简体   繁体   中英

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. Some include other static libraries and need to use -I to specify the directory. But in vscode, the common configuration is directly gcc sourcefile -o targetfile , so it can't be executed normally? Is there a way to make vscode execute according to 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?

Indeed it is possible to have VSCode work with make on linux or msbuild.exe on windows . You can have different configurations within these as well so that you have have a Debug build or a Release build. All you need to do is to have suitable tasks defined in 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 . In turn, they are:

lindbgbuild which executes command make CONF=Debug -C./.vscode

What this command assumes is the existence of Makefile in the ./.vscode subdirectory of the current (project) directory. Then, it executes the Debug configuration within it. The next, linreleasebuild does the same for the Release configuration. 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 . All commands like g++ -c -o -g -I , etc., are configurable outside of VSCode in a separate makefile that VSCode only calls.

Then, you have a task winbuilddebug . This assumes the existence of a .vcxproj project configuration file in ./.vscode . It then executes the appropriate configuration with the options provided (in this case, the configuration is Debug on x64 ). Similarly, winbuildrelease for Release configuration under Windows. .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. (I suggest against this, and using an IDE for your initial build since that automates without error many settings.)

With this given tasks.json , how do you access these tasks? 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.

Is there a way to automate this step and launch? Indeed, following is an example of 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). Which build configuration does it choose? It will choose the winbuildrelease configuration that has been specified. 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 . This opens up the Run sidebar, where on top, you can choose from amongst the configurations you have specified in your launch.json .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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