Skip to content

Instantly share code, notes, and snippets.

@mtvee
Created October 3, 2022 02:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtvee/696e74294b8967586e56201a4d114aee to your computer and use it in GitHub Desktop.
Save mtvee/696e74294b8967586e56201a4d114aee to your computer and use it in GitHub Desktop.
c++ from command line in windows

How to build c++ from the command line in windows 10

The rest of all this is done from the shell and any editor

  • make a junk directory somewhere and put these files into it

main.cpp

#include <iostream>
                                                                                                                                                                                                                             
int main(int argc, char **argv)
{
    std::cout << "hello world!\n";
    return 0;
}  

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(hello)
add_executable(hello main.cpp)  

vscmd.bat

%comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\Tools\VsDevCmd.bat"

  • run vscmd.bat

this sets up the visual studio tools paths and god knows what all. Don't read the batch file that gets called from this batch file, you'll go blind. Just make sure the path to it is correct

  • type mkdir build

this is where we are going to put the build files so we don't pollute our sources

  • type cd build

go there

  • type cmake -G"NMake MakeFiles" ..

this tells cmake to generate the files that the tools need to actually build stuff. The two dots (..) tell cmake to look for the CMakeLists.txt file one directory up Now you see a whole bunch of stuff that cmake generated, one of which is a file called Makefile. This is what we need.

  • type make

this is running a VS tool called make that reads the files that cmake generated and finally builds the thing You should see hello.exe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment