114

I know that everyone uses an IDE nowadays, but I just find it simpler to write my code in notepad++, compile it using a command prompt command, and run it from there too. At least that works for Java and Python. I've tried to get my head around how to do that with C++, and haven't been able to find anything good. Is there any compiler (like Java's JDK) that I can stick into my path and use the C++ equivalent of javac and java to run and compile my code from CMD?

Note: please don't post answers and comments about how IDEs are better - I know they are. I'm just used to doing it the old way :D

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Bluefire
  • 13,519
  • 24
  • 74
  • 118
  • 4
    Sure, what operating system are you using? – cha0site Jul 06 '12 at 16:08
  • Probably windows ... install the free version of visual studio and you can use the "cl" command. – nisah Jul 06 '12 at 16:10
  • 5
    @BLuefire: also, what C++ compilers do you have installed on your system? – Mooing Duck Jul 06 '12 at 16:16
  • 1
    You have to set the environment variable to run a c++ file anywhere. follow this video instructions on Youtube [How To Compile C & C++ Files On Windows 10 2018](https://www.youtube.com/watch?v=40MWdFAGTzg) – Pushpa Kumara Oct 05 '19 at 16:55

14 Answers14

62

Steps to perform the task:

  1. First, download and install the compiler.

  2. Then, type the C/C++ program and save it.

  3. Then, open the command line and change directory to the particular one where the source file is stored, using cd like so:

    cd C:\Documents and Settings\...
    
  4. Then, to compile, type in the command prompt:

    gcc sourcefile_name.c -o outputfile.exe
    
  5. Finally, to run the code, type:

    outputfile.exe
    
ib.
  • 27,830
  • 11
  • 80
  • 100
codeDEXTER
  • 1,181
  • 7
  • 14
  • 11
    If you mention `cd`, you should mention that he should probably put gcc on the path first. – Mooing Duck Jul 06 '12 at 16:24
  • @MooingDuck MinGW installer do it for you – Gigi Jul 06 '12 at 16:25
  • 2
    @Gigi: I normally use mingw-w64 which doesn't touch the path, alright then. – Mooing Duck Jul 06 '12 at 16:35
  • 3
    @MooingDuck @Gigi I'm using mingw-w64-install.exe on Windows. In CMD, I get `'g++' is not recognized as an internal or external command, operable program or batch file.`. What do I do? – Leo Jul 13 '19 at 17:45
  • 2
    Add the directory containing `g++` to the windows path. https://docs.alfresco.com/4.2/tasks/fot-addpath.html – Mooing Duck Jul 14 '19 at 04:58
  • you probably will need to restart your PC to use new PATH, you can just refer your compiler manually, mine was installed like so: "C:\MinGW\bin\gcc" – Kevin Oct 01 '19 at 07:23
  • 3
    Just want to mention (since it took me some time to figure it out): I think these commands are for running c files. If you are running C++ files, you should change the second command to g++ sourcefile_name.cpp -o outputfile.exe – user56202 Jun 23 '21 at 19:23
46

If you're running Windows then make use of this:

g++ -o program program.cpp

g++ is the name of the compiler and -o is the option needed for creating a .o file. Program (without .cpp suffix) is the exe file and program.cpp is your source file that you want to compile.

g++ -o program program.cpp&program.exe

Use this shortcut to run the .exe file of the program. This might run in Linux but you may have to use .out suffix instead of .exe. Use this handy batch script to execute your programs on Windows:

@echo off&&cls
set /p pathName=Enter The Path where the file is located:%=%
cd %pathName%
REM set /p exec=Enter The Name of the executable you want to make:%=%
set /p file=Enter The Name of the file you want to compile:%=%
g++ -o %file% %file%.cpp
%file%.exe

save it as cppExecutor.bat

Also you could use the following commands on Unix (Linux and Mac) OS:

CC program.cc

If you want to use gcc:

gcc -o program program.cpp

With the shortcut:

gcc -o program program.cpp&program.exe
JedaiCoder
  • 646
  • 7
  • 12
  • What would be the first `program` after `-o`? – Memmo Apr 30 '20 at 08:37
  • @Memmo sorry for the late reply, the first `program` after `-o` will be the name of your code file (or whatever else you want to name your program (the `.exe` etc.) – JedaiCoder Jun 06 '21 at 07:45
41

It depends on what compiler you're using.

For example, if you are using Visual C++ .NET 2010 Express, run Visual C++ 2010 Express Command Prompt from the start menu, and you can simply compile and run the code.

> cl /EHsc mycode.cpp
> mycode.exe

or from the regular command line, you can run vcvars32.bat first to set up the environment. Alternatively search for setvcvars.cmd (part of a FLOSS project) and use that to even locate the installed VS and have it call vcvars32.bat for you.

Please check your compiler's manual for command lines.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
Rango
  • 1,087
  • 7
  • 5
11

Sure, it's how most compilers got started. GCC is probably the most popular (comes with most flavors of *nix). Syntax is just gcc my_source_code.cpp, or gcc -o my_executable.exe my_source_code.cpp. It gets more complicated, of course, when you have multiple source files (as in implementation; anything #included works automatically as long as GCC can find it).

MinGW appears to be a version of GCC for Windows, if that's what you're using. I haven't tried it though.

Pretty sure most IDEs also include a command line interface. I know Visual Studio does, though I have never used it.

KRyan
  • 7,308
  • 2
  • 40
  • 68
  • 1
    Agree, I use it once in a while. In my case (Windows 10 64 bit) it was installed under: `C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools`. You can also get to it via the start menu (from there navigate to the "Visual Studio" folder/section). It will appear as: `Developer Command Prompt for VS 2017` (in the case of VS 2017). – Guy Avraham Dec 19 '18 at 07:23
10

I really don't see what your problem is, the question is rather unspecific. Given Notepad++ I assume you use Windows.

You have so many options here, from the MinGW (using the GCC tool chain and GNU make) to using a modern MSVC. You can use the WDK (ddkbuild.bat/.cmd or plain build.exe), the Windows SDK (nmake.exe), other tools such as premake and CMake, or msbuild that comes with MSVC and the Windows SDK.

I mean the compiler names will differ, cl.exe for MSVC and the WDK and Windows SDK, gcc.exe for MinGW, but even from the console it is customary to organize your project in some way. This is what make and friends were invented for after all.

So to know the command line switches of your particular compiler consult the manual of that very compiler. To find ways to automate your build (i.e. the ability to run a simple command instead of a complex command line), you could sift through the list on Wikipedia or pick one of the tools I mentioned above and go with that.

Side-note: it isn't necessary to ask people not to mention IDEs. Most professional developers have automated their builds to run from a command line and not from within the IDE (as during the development cycle for example), because there are so many advantages to that approach.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
  • Does the Windows SDK come preinstalled with the platform? – Bluefire Jul 06 '12 at 16:15
  • 1
    @Bluefire: no, but the current versions of it can be downloaded for free and still contain a compiler (though not an optimizing one, unless you have a qualifying product such as MSVC Pro or higher). However, MS announced that they want to drop the compiler from WDK and SDK starting with Windows 8. – 0xC0000022L Jul 06 '12 at 16:16
8
  1. Download MinGW form : https://sourceforge.net/projects/mingw-w64/
  2. use notepad++ to write the C++ source code.
  3. using command line change the directory/folder where the source code is saved(using notepad++)
  4. compile: g++ file_name.cpp -o file_name.exe
  5. run the executable: file_name.exe
Deepak Singh
  • 81
  • 1
  • 1
  • 1
    After installation `'g++' is not recognized as an internal or external command`. – Vitaly Zdanevich Apr 26 '19 at 08:10
  • @VitalyZdanevich you should add g++ to the PATH variable. – Muktadir Khan Oct 16 '19 at 09:07
  • I just tried it and it worked; I installed cygwin64. A bit weird that one has to download from the internet (I'd like a single .exe file actually), but after I did so, I just did a "g++ hello_world.cpp -o hello_world.exe" and after doing a "hello_world" it just worked. \o/ Now I can use ruby to autogenerate tons of C++ code actually. :D – shevy May 06 '21 at 02:59
7
  • first Command is :

g++ -o program file_name.cpp

  • Second command is :

.\program.exe

Let us Check this image

SCouto
  • 7,808
  • 5
  • 32
  • 49
harshit14366
  • 81
  • 1
  • 5
3

A better alternative to MinGW is bash for powershell. You can install bash for Windows 10 using the steps given here

After you've installed bash, all you've got to do is run the bash command on your terminal.

PS F:\cpp> bash
user@HP:/mnt/f/cpp$ g++ program.cpp -o program
user@HP:/mnt/f/cpp$ ./program
1

Open cmd and go In Directory where file is saved. Then, For compile, g++ FileName. cpp Or gcc FileName. cpp

For Run, FileName. exe

This Is For Compile & Run Program.

Make sure, gcc compiler installed in PC or Laptop. And also path variable must be set.

1

This is what I used on MAC.

Use your preferred compiler.

Compile with gcc.

gcc -lstdc++ filename.cpp -o outputName

Or Compile with clang.

clang++ filename.cpp -o outputName

After done compiling. You can run it with.

./outputFile
Joe
  • 811
  • 1
  • 8
  • 14
0

have MinGW compiler bin directory added to path.

use mingw32-g++ -s -c source_file_name.cpp -o output_file_name.o to compile

then mingw32-g++ -o executable_file_name.exe output_file_name.o to build exe

finally, you run with executable_file_name.exe

emma-ea
  • 324
  • 2
  • 5
0

[Working 100%] from a Windows user. Open the terminal(powershell) where your file.cpp is created.

  1. g++ file.cpp //it will compile the file into a.exe
  2. .\a.exe //this will run the program.
mkbhru
  • 29
  • 4
-1

There are few ways:

Using GNU Compiler Collection (GCC):

gcc -lstdc++ filename.cpp -o outputName

Using g++ command:

g++ -o outputName filename.cpp

Using clang++:

clang++ filename.cpp -o outputName
Yadab Sd
  • 591
  • 4
  • 9
-1

You can run your code by just typing

To Compile g++ file_name.cpp

To Run: a

only this you have to do to run c++ code in cmd which is written in notepad++

enter image description here

enter image description here

  • Almost every answer tells to use gcc. This answer does not contribute anything different. Recommend deleting. [From review](https://stackoverflow.com/review/low-quality-posts/33526389) – Burak Dec 30 '22 at 14:38