Run C++ in command prompt - Windows - c++

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:
First, download and install the compiler.
Then, type the C/C++ program and save it.
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\...
Then, to compile, type in the command prompt:
gcc sourcefile_name.c -o outputfile.exe
Finally, to run the code, type:
outputfile.exe

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

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.

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.

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.

Download MinGW form : https://sourceforge.net/projects/mingw-w64/
use notepad++ to write the C++ source code.
using command line change the directory/folder where the source code is saved(using notepad++)
compile: g++ file_name.cpp -o file_name.exe
run the executable: file_name.exe

first Command is :
g++ -o program file_name.cpp
Second command is :
.\program.exe
Let us Check this image

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

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

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.

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

[Working 100%] from a Windows user.
Open the terminal(powershell) where your file.cpp is created.
g++ file.cpp //it will compile the file into a.exe
.\a.exe //this will run the program.

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

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

Related

How to know the command line arugments to build a project that was coded in eclipse

I have a project that I coded in Eclipse, now I need to compile it and run using the terminal. The project has some additional libraries that were added to the linker.
e.g.
g++ then what?
How can I know that command line arguments that I need to run it through the terminal?
The project was coded in c++ using eclipse Luna on a linux machine.
Thanks
You could always try
g++ -std=c++0x your_file_name.cpp -o desired_output_name

create and compile "hello world" application in Linux using Visual Studio Code

I am new to Linux, but developed C/C++ in windows for some times now.
I installed a Linux Ubuntu 16.4 and Visual Studio Code.
I create a folder and inside that folder I created a file called main.cpp and inside that file I wrote:
#include <iostream>
void main()
{
std::cout << "Hello World" << std::endl;
}
Now I want to compile and run it and possibly debug it (step by step to see how I can debug a simple application).
How can I do this?
Any tutorial on to setup a development system in Linux using Visual Studio Code?
I installed "C/C++ for Visual Studio Code" but I am still not able to compile and run the sample application.
Edit1
I already installed compiler and can compile my code using
g++ main.cpp
and getting a.out
How can I configure VSC to automate this processor and generate dependency and if there is an error, open the file with error on editor and show me the line that generate error. Also during debug show me the source code when I am stepping the code.
These are some basic requirement that I have from a development system, otherwise I call it an editor and not a development system.
First, you have to install a compiler, I recommend GCC (Ubuntu usually doesn't come with one even though it's mostly written in C/C++)
Second, compile your program, here's how
To compile the program, open the terminal and move on to the target directory type the command – (where gcc implies compiler name, then it asks for the file name of the source program while -o option specifies the file name of the output program)
gcc hello.c -o hello1
If there is no syntax/semantic error in you program then the compiler will successfully generate an executable file, otherwise fix the problem in your code.
However, that will only work for C, here's how to do it for C++ (only if the extension is .cpp)
The steps are almost same as above but you need to install g++ compiler, the file extension should be .cpp and in compilation phase replace gcc with g++. To install G++ compiler, execute the command -
sudo apt-get install g++
The compilation command now is:
g++ hello.cpp -o hello1
It goes without saying that you should replace "hello.cpp" with your files name and "hello1" with the name you want your "exe" file to have

command to compile c++ program on live Fedora

I'm using Fedora 20 live with the help of a DVD instead of installing it. I have used the following command to compile c++ program:
g++ programname.cpp
But it displayed this:
bash: g++: command not found...
Is there any other alternative command?
Why g++ command is not working for me?
Is using Live Fedora the reason??
You can also try c++, which is usually a symlink to the currently installed C++ compiler. If you don't have that, try clang++.
The g++ command is part of the GNU C/C++ compiler gcc. Using the g++ command specifies that you want to compile a C++ program, but it's actually just a link to the gcc command. Try using gcc instead of g++. You may have problems with that because it will try to compile it as a C program, but I think there is a compiler flag that will force it to compile it as a C++ program. If none of this works, then you probably don't have a compiler installed. I am not familiar with Fedora's package manager, but the package you want will probably be called either gcc or g++.

Using system() cross platform. Compiling with cygwin and g++

I'm using cygwin and g++ under Windows 7 to compile my project. I've made a makefile to compile the project under Ubuntu. This is all working so far.
A function calls the Libre Office to convert some files. For this purpose i pass a command string to system().
Symptom
When I use the executable generated with cygwin a error occurs:
sh: C:\..\LibreOffice 3.6\program\soffice --headless --convert-to png:'draw_png_Export' add1.fodg : command not found
What irritates me is the fact, that it seems the command is passed to sh and not cmd
How can I make sure the executable build for Windows uses no sh?
I think you have two options.
First, stick with sh used by system() and use the path to LibreOffice in POSIX format.
C:\..\LibreOffice 3.6\program\soffice
will become
/cygdrive/c/../LibreOffice 3.6/program/soffice
You can also use the cygpath utility for this, as well.
The other option is to call LibreOffice via cmd:
cmd /c C:\..\LibreOffice 3.6\program\soffice args...

New MinGW gcc doesn't do anything

I'm trying to compile a simple helloworld program with MinGW on Windows and nothing happens. No output, no executable, nothing. I've just installed the latest MinGW with their mingw-get-inst-20120421.exe installer. When I use an older version of MinGW that came with Code::Blocks, I am able to compile the program. I'm out of ideas and my googling has been in vain. C:\MinGW\bin is on my path and I'm using MSYS.
Command line parameters in MSYS:
gcc helloworld.c -o helloworld
Execute the console from the Start menu, Start->MingW->MinGW Shell or from filesystem:
C:\MinGW\msys\1.0\msys.bat
Execute gcc in this shell.
Otherwise you will have to add the minGW/bin directory to your PATH environment variable.
Go to the directory where you have you gcc executable. In my case it's:
C:\MingW\bin
From there execute gcc:
gcc myFile.c
If it compiles, than something must screwed up your $PATH