1

My ultimate goal is to able to use Visual Studio Code to write and run simple C++ programs. I have installed Visual Studio Code and MSBuild (Visual Studio Build Tools 2017) on my machine.
I do not have Visual Studio IDE installed on my machine. So, I have no easy way to create *.vcproj and *.sln for the code that I write.

So, here is what I do: I write a simple C++ program and save the file as test.cpp. And then, even before creating a build task in VS code, I wanted to verify if MSBuild works. So, I ran the following command.
msbuild.exe /t:ClCompile /p:SelectedFiles="test.cpp"
and it returns the following error:
error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.

Is it possible to use MSBuild to compile a single C++ file?

Reference: Using MSBuild to compile a single cpp file - This page suggest that I should be able to run MSBuild to compile a single C++ file since VS 2010.

Rakesh Agarwal
  • 3,009
  • 9
  • 33
  • 40
  • 1
    You could use `CMake` to generate a project file given a very simple / basic `CMakeLists.txt`. Although I question why you need / want to use msbuild in the first place. – drescherjm Jan 01 '19 at 16:52
  • ok sure, I will try that. Thanks. What else could I use instead of msbuild? I just want a C++ compiler on windows and could be used with vs code. Sorry, if that sounds too naive, but could I use gcc on windows? – Rakesh Agarwal Jan 01 '19 at 17:23
  • 2
    The microsoft `c++` compiler is `cl.exe` msbuild.exe is for building projects or solutions. – drescherjm Jan 01 '19 at 21:46
  • Is it possible to install just cl.exe on my machine without having to install msbuild? – Rakesh Agarwal Jan 02 '19 at 03:47
  • 1
    Yes, this has been asked/answere before. There are multiple ways to do it, basically a matter of setting up task.json (see linked duplicate question for one possible way to write that) and making sure the compiler is available (see e.g. https://stackoverflow.com/questions/47115546/setup-vscode-for-c-on-windows-msvc which has the easiest answer: first run the batch file which sets up the environment variables etc for the build tools, then start VSCode so it can just call cl.exe etc.) – stijn Jan 02 '19 at 09:49

2 Answers2

0

You need to read the page you link to more carefully. MSBuild requires a project file (or a solution file referencing one or more project files) to do anything at all and that in turn is created by the IDE.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
0

I believe that what you are looking for is CL (compiler) and not MSBuild (project tool,...).

I wrote a page with my notes for vscode and c++. There you can also get the task for debugging.

These are the tasks I use for building a single file:

        {
            "label": "MSVC C++03",
            "type": "shell",
            "command": "cl.exe",
            "args": [
                "/EHsc",
                "/FC",
                "/Od",
                "/permissive-",
                "/W4",
                "/Z7",
                "/Fdout/${fileBasenameNoExtension}.pdb",
                "/Feout/${fileBasenameNoExtension}.exe",
                "/Foout/${fileBasenameNoExtension}.obj",
                "${fileBasename}",
            ],
            "group": "build",
            "presentation": {
                "reveal":"always"
            },
            "problemMatcher": "$msCompile"
        },
        {
            "label": "MSVC C++17",
            "type": "shell",
            "command": "cl.exe",
            "args": [
                "/EHsc",
                "/FC",
                "/Od",
                "/permissive-",
                "/std:c++17",
                "/W4",
                "/Z7",
                "/Fdout/${fileBasenameNoExtension}.pdb",
                "/Feout/${fileBasenameNoExtension}.exe",
                "/Foout/${fileBasenameNoExtension}.obj",
                "${fileBasename}"
            ],
            "group": "build",
            "presentation": {
                "reveal":"always"
            },
            "problemMatcher": "$msCompile"
        }
Janosimas
  • 570
  • 7
  • 17