Victor Stone
Victor Stone

Reputation: 528

How can I build an existing Windows-only Visual Studio project from Bash using WSL

I have an existing C++ project that I've configured and built in Visual Studio. This project's only target is Windows, no other platforms. I'm using Bash in WSL to launch the executable.

I prefer to develop in Visual Code (not Visual Studio). I prefer to build and launch applications through Bash (strong Linux background).

Right now, my development workflow is:

  1. Edit code in VS Code
  2. Switch to Visual studio and click the build button
  3. Switch to Bash and execute the built program

Since I only keep Visual Studio open for building, I would much prefer to build by command line through Bash.

My naive approach was to use an open source tool to convert the Visual Studio project file into a CMake file. Then cmake & make from Bash, but I stopped when I started encountering errors looking for windows.h (maybe I just need to add some windows include paths to my include_path).

I'm not sure what the best way to go about this would be. Any suggestions would be appreciated!

Upvotes: 0

Views: 1851

Answers (2)

Victor Stone
Victor Stone

Reputation: 528

MSBuild.exe is provided with my installation of Microsoft Visual Studio. From within WSL bash, I can invoke MSBuild.exe and give the .sln file of my project as the first and only argument.

The compilation output is written to the terminal.

Upvotes: 1

Ross Douglas
Ross Douglas

Reputation: 120

If the project is entirely C++, there should be no reason to leave WSL. Building and launching the application can be easily handled right there! You can absolutely build by the command line in bash by using

g++ -o <outputfile> <inputfiles>

However, the easiest way to run the program is to create a build configuration in Visual Code. You will need 2 files: launch.json and tasks.json To create the launch file, hit F1 (or open your command pallet) and select Tasks: Configure Default Build Task. It should look something like this.

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}", //input files
                "-o",
                "${fileDirname}/a.out" //output file
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

To create launch.json, go to the 'debug' tab and select 'create a launch.json file'. It should look something like this

{
    // 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": "g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/a.out", //output file
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

with both of these files in place, all you have to do is hit the run button like in Visual Studio.

Upvotes: 1

Related Questions