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
Steps to perform the task:
Yes, first install a compiler: Download from here
Then type the C/C++ program, save it.
Then open the command line and change directory, using
cd
to the particular directory where the source file is stored.like:
cd C:\Documents and Settings\...
Then to compile/run type in the command prompt,
gcc sourcefile_name.c
orgcc -o outputfile.exe
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.
or from the regular command line, you can run
vcvars32.bat
first to set up the environment. Alternatively search forsetvcvars.cmd
(part of a FLOSS project) and use that to even locate the installed VS and have it callvcvars32.bat
for you.Please check your compiler's manual for command lines.
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
If you're running Windows then make use of this:
g++
is the name of the compiler and-o
is the option needed for creating a .o file. Program (without .cpp suffix) is theexe
file andprogram.cpp
is your source file that you want to compile.Use this shortcut to run the
.exe
file of the program. This may run in Ubuntu but you may have to use.out
suffix instead of.exe
. Use this handy batch script I made to execute your programs on Windows:save it as
cppExecutor.bat
Also you could use the following commands on Unix (Linux and Mac) OS:
If you want to use
gcc
:With the shortcut:
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 plainbuild.exe
), the Windows SDK (nmake.exe
), other tools such as premake and CMake, ormsbuild
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 whatmake
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.