I just need to make clear one thing. In University we are learning the C++ programming language and they suggest us to use the GNU C++ Compiler which is part of the GCC.
So on my Mac OS X Mavericks I download the command line tools from the developers.apple.com.
I wrote a simple C++ program and I compile this program using the g++ command like this:
g++ program.cpp
./a.out
And the program runs perfect. But as I know, using a different compiler, means that you have to use the correct syntax/commands/libraries for this spesific compiler, so while in the University we use the "GNU C++ compiler", I just want to make clear that with the g++ command is meant that I use the "GNU C++ Compiler".
Cheers.
Traditionally, gcc and g++ are both components of the GNU C compiler suite. gcc is the C compiler, and g++ is the C++ compiler.
On current versions of Mac OS X, the commands gcc and g++ are both treated as alternate names for clang and clang++, which are components of the Clang C compiler. However, this compiler is almost entirely compatible with GCC — the few differences that do exist will almost certainly not come up in the coursework you're doing.
(The most significant difference is that Clang's diagnostics are much better: it will point out exactly where a syntax error occurs in a line, rather than just what line it's on, and it can often identify potential typos or subtle mistakes in situations where GCC would just give you a cryptic error message. If you're just learning C, you will appreciate this a lot.)
As per #duskwuff, I would prefer to use clang++, however if you must be compatible, then you can installi the real GNU compiler via macports.
After installing macports (which includes a xcode-select step), simply do:
$ sudo port selfupdate
$ sudo port install gcc46
(or gcc47, etc.)
The compiler will be in your $PATH (if you set-up macports correctly), but explicitly, it will be /opt/local/bin/gcc46 (see sudo port select gcc).
I use ubuntu 12.04 and the default gcc is 4.6.3. It is not accepting c++11 commands and is giving me output saying the command is not c++98 compatible. I checked online and have seen people advising to not change default compilers on operating system as it becomes unstable. Can anybody suggest a fix or a safe way of downloading a gcc compiler that is c++11 compliant.
As others have suggested, you need to enter the std commandline option. Let us make it easy for you
Open terminal by pressing Ctrl+Alt+T
sudo gedit ~/.bashrc
Enter the following line as the last line
alias g++="g++ --std=c++0x"
Save and close the file and close the terminal.
Now open terminal again and compile your c++ 11 programs simply by g++ filename.cpp
Thats it. By default it will compile for c++11 standard.
NOTE: If you follow the above mentioned option, to compile non-c++ 11 programs, you have to use
g++ --std=c++98 filename.cpp
gcc 4.6.3 supports many c++11 features. However, they are disabled by default. To enable them, use the following flag:
g++ -std=c++0x ...
This flag also disables GNU extensions; to keep them enabled, use -std=gnu++0x flag.
I've been trying to compile a multithread hello-world program under Cygwin using the newly introduced C++ 11 std::thread feature without success. I compiled and installed GCC 4.7.2 by myself, and the same code works without any problems under Linux with the same version of GCC. The first error I got was that the compiler did not recognize the -pthread flag. After researching on it for a while I noticed someone said on Cygwin this flag should be -lthread. I made the change and that error was gone, but another series of errors occur telling me thread is not member of std. I wonder if it's caused by the wrong configuration of the compiler during installation, or std::thread is simply not supported under Cygwin?
This looks like you did not compile the program with the appropriate standard library flag. If you want to compile for C++11 you should use:
g++ --std=c++0x -o ...
The --std flag sets the appropriate language compatibility level. If this does not help, please post the error messages you got as a source listing.
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
I just found on my Ubuntu, there are two different C++ compilers: /usr/bin/g++ and /usr/bin/c++. I am not familiar with the latter, but man c++ just jumps to the manpage of gcc. I wonder what is their difference as C++ compilers?
This is typical Ubuntu symlink mayhem.
If you ls -l /usr/bin/c++, you will see it is actually a symbolic link. to:
/etc/alternatives/c++
Which in turn, is also a symbolic link to:
/usr/bin/g++
So, on Ubuntu systems, c++ is g++. The reasoning behind the link indirection is that there are multiple packages that could provide a c++ compiler (such as different versions of g++). You'll see this a lot on Ubuntu. For example, qmake is a link to a file in /etc/alternatives, which is (on my system) a link back to /usr/bin/qmake-qt3.
c++ is a standard name of a C++ compiler on a system.
On a GNU system you almost surely have GCC (GNU compiler collection) installed, which includes a C++ compiler named g++ ('g' for GNU). But to be POSIX-compatible, they install this compiler as c++ also, sometimes c++ is a symbolic link to g++ sometimes it's a hard link, sometimes it's just the same file installed twice.
This can be not the case for other systems like FreeBSD or NetBSD. It's possible that those systems don't have GCC (and other GNU stuff) installed.
On my system these two files are just identical:
% diff `which c++` `which g++`
% echo $?
0
This means that c++ at least invokes the same compiler, but theoretically it can interpret some command line options differently or have some different defaults. Someone with more knowledge is free to extend the answer in this regard.
On my machine, c++ is a link:
$ readlink /usr/bin/c++
/etc/alternatives/c++
$ readlink /etc/alternatives/c++
/usr/bin/g++
So c++ is just a link to g++.
g++ is the gnu c++ compiler where c++ is the system c++ compiler, in the case of ubuntu C++ is a link to g++ however in another system it could very well be a link to a non gcc compiler. as someone else said vi vs vim. just because a link to vi exists on the system doesn't mean that it's vim could be any vi clone.