Dan Ashby
Dan Ashby

Reputation: 41

vscode building of Visual Studio Projects

I am trying to build a collection of .vcxproj generated by Premake within VS Code. What I don't get is why it works for executing the premake generation but not the .vcxproj builds as the old batch files are the same except they call msbuild.exe with a full path.



** Visual Studio 2019 Developer Command Prompt v16.11.17 ** Copyright (c) 2021 Microsoft Corporation


Building configurations... Running action 'vs2019'... Done (160ms).

'C:/Program' is not recognized as an internal or external command, operable program or batch file.


This is my tasks.json there is more but it's just rinse and repeat.

{
    "version": "2.0.0",
    "windows": {
        "options": {
          "shell": {
            "executable": "cmd.exe",
            "args": [
                "/C",
                // The path to VsDevCmd.bat depends on the version of Visual Studio you have installed.
                "\"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/VsDevCmd.bat\"",
                "&&"
            ]
          }
        }
      },
    "tasks": [
        {
            "type": "shell",
            "label": "Generate Project",
            "command": "${workspaceFolder}/packages/premake/premake5.exe",
            "args": [ "vs2019" ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
        },
        {
            "type": "shell",
            "label": "Build SimClient (Debug)",
            "dependsOn": ["Generate Project"],
            "command": "msbuild.exe" ,
            "args": [
                "SimClient.vcxproj",
                "&&",
                "/p:configuration=Debug Static",
                "&&",
                "/p:platform=x64"
            ],
            "problemMatcher": [ "$msCompile" ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
        },
        ...
        ],
    }
}

Upvotes: 2

Views: 4451

Answers (2)

Dan Ashby
Dan Ashby

Reputation: 41

I managed to find a solution to this problem while trying to start a process in an external terminal. By replacing "command":"msbuild.exe" with "command":"cmd.exe" and moving the "msbuild.exe" into the argument list preceded by "/C" fixes the failing path string.

{
    "version": "2.0.0",
    "windows": {
        "options": {
          "shell": {
            "executable": "cmd.exe",
            "args": [
                "/C",
                "\"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/VsDevCmd.bat\"",
                "&&"
            ]
          }
        }
      },
    "tasks": [
        {
            "label": "Generate Project",
            "type": "shell",
            "command": "${workspaceFolder}/packages/premake/premake5.exe",
            "args": [ "vs2019" ],
            "group": {
                "kind": "build",
                "isDefault": false
            },
        },
        {
            "type":"shell",
            "label": "Build (Debug)",
            "command":"cmd.exe",
            "args": [
                "/C",
                "msbuild.exe",
                "SimClient.sln",
                "/p:configuration=Debug;platform=x64"
            ],
            "problemMatcher": [ "$msCompile" ],
            "group": {
                "kind": "build",
                "isDefault": false
            },
        },

    ]
}

Upvotes: 2

Bowman Zhu
Bowman Zhu

Reputation: 7241

VS2019 is an inherited development tool, but VSCode is actually just an text editor.

If you want to use VSCode to build and run C++ code, you will need multiple extensions and configure everything by yourself.

Here is how I make it work:

1, install gcc and configure it to the environment variables of the system.

2, install C/C++, C/C++ Extension Pack, CMake, CMake Tools.

3, Close VSCode and reopen it, this step is to make sure the VSCode load the environment variables and extension features.

4, Write a CMakeLists.txt to activate the CMake extension:

cmake_minimum_required(VERSION 3.5)

project(MyProject)

set(CMAKE_CXX_STANDARD 11)

add_executable(cpp-compiler Cpp_Proj.cpp)

This is my project structure(Created from Visual Studio):

enter image description here

When you configure the file and save, CMake extension should auto build the project.

If it didn't build, just go this place and click the button:

enter image description here

After that, configure everything needed and click run:

enter image description here

5, Result:

enter image description here

This is my code(a console app):

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
    //input
    std::cout << "Enter a number: ";
    int x = 0;
    std::cin >> x;
}

For more details about more complicated situation, you can do a research on configurations in CMakeLists.txt.

Upvotes: 0

Related Questions