- Compile C Programs Online
- Compile C Program Terminal
- Compile C Program Fedora Terminal
- C++ Compile And Run
I am new to Linux. I am using Ubuntu 11.04 and do not know how to compile and execute C++ program in it. I need to know the commands to Compile and Execute a C++ program in Linux.
Kris HarperJoin Date Jan 2010 Location India Beans 8 Distro Ubuntu 9.10 Karmic Koala. >> How to compile & execute C programs under Linux (Absolute basics). This would create problems when you compile many programs in one directory. So you override this with the -o option followed by the name of the executable. This is the absolute basics of compiling C programs under Linux.
5 Answers
To compile your c++ code, use:
g++ foo.cpp
foo.cpp in the example is the name of the program to be compiled.
This will produce an executable in the same directory called a.out
which you can run by typing this in your terminal:
./a.out
g++ should already be in your $PATH, so you don't need to call /usr/bin/g++
explicitly, but you can use the latter in any case.
foo.cpp
should be in the same directory you're running the command from. If there is any doubt, you can make sure you are in the same directory by typing ls foo.cpp
or head foo.cpp
(if you need to verify you're working with the correct foo
.)
As noted by @con-f-use, the compiler will usually make this file executable, but if not, you can do this yourself (so the command to execute, ./a.out
or equivalent, will work):
To specify the name of the compiled output file, so that it is not named a.out, use
-o` with your g++ command.
E.g.
g++ -o output foo.cpp
This will compile foo.cpp
to the binary file named output
, and you can type ./output
to run the compiled code.
I'm making two assumptions here:
- You already have a C++ source file/program ready to build
- You have set up a build system on your computer
The simplest way to compile a C++ program on Ubuntu, or any other Linux distro for that matter, is to type
- g++ is the invocation of the C++ component of GCC, the defacto compiler for C/C++ and whole host of other languages on the Linux platform. It's currently the only compiler capable of compiling the Linux kernel.
- main.cpp is the c++ source file you wish to compile.
- -o main specifies the name of the output file you wish to create once the source is compiled. The target source file and the target output file can be inverted if you wish, so
g++ -o main main.cpp
is equally valid. - To then execute that program, you need to do ./main in the terminal.
The above commands assume you are already in the location of the source files, but both the source file and target output file may also be specified as a directory. For example
will compile a C++ source file located on your desktop and place the executable binary in a Projects
folder in your home directory. To run this executable, run ./Projects/main
.
This is how I like to compile with g++.
$g++ -W -Wall -pedantic -o programName -p sourceFile.cpp
You need g++, for gcc may not compile cpp file easily.
You also need to learn vim or emacs to write C code.
Just try this on your terminal:
Type a test program and save it:
Compile hello.cc
with g++:
Execute it:
Here the ./
means the exe file is under the current dir.
g++
is a front-end to gcc
(GNU Compiler Collection) with some predefined c++ macros and different default options/flags.
compiling c++ code with gcc
is handy when g++
is not available for any number of reasons ,in fact it's just a matter of linking to the c++ library -lstdc++
and informing gcc to treat the input as c++ code (either by using a .C
extension , or forcing the language with -x
)
other valid c++ file name suffixes : .cc
, .cp
, .cxx
, .cpp
, .CPP
, .c++
example :
gcc cpp_code.C -lstdc++
the uppercase extension (.C
) is important for gcc to know it's a c++ file.
Compile C Programs Online
or explicitly specifying the input language :
gcc -x c++ cpp_code.txt -lstdc++
extension can be anything, or even nothing
by default the result (after a successful compilation) is an a.out
file which can be run with ./a.out
protected by A.B.Jul 11 '15 at 8:52
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged command-lineprogrammingc++ or ask your own question.
Ubuntu 11.10 (Oneiric Ocelot) is one of the most popular operating system for programming because there are lot of great open source applications, tools, compilers, debuggers, IDEs are available free of cost. Some of them are - GCC – The greatest compiler for C language (from FSF (‘Free Software Foundation’ by Stallman); Linux Torwalds used GCC while developing Linux Kernel), Eclipse IDE (The most popular ‘Integrated Development Environment’ for Java programmers), Netbeans, KDevelop, Codelite etc.
C/C++ language is a high level programming language (although the term high and low is used in relative sense e.g C is a high level language as compared to Assembly but if we compare it with java then C is a low level programming language; the term high or low basically describes the closeness with hardware). Most of the operating systems has been written in C language. This post has been written for beginners who just started learning C/C++ or the programmers who have migrated from Windows to Ubuntu (although the commands are almost same for all Linux based operating system).
Compiling and Executing C program in Ubuntu 11.10
1. Write and save the program
Compile C Program Terminal
Open a simple text editor (e.g gedit), IDE (Eclipse) or command line code editor (Nano or Vim). I’ll be using gedit as it is very simple to use and it’s recommended for beginner programmers. Right Click on Desktop or any directory (Browse File using Nautilus) and select create new File – hello.c (.c extension is used to indicate that it’s a c program). Then write a simple program like this (and save the program press Ctrl+S)
2. Compile the program
GCC (GNU Compiler Collection) is installed by default, in Ubuntu. 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)
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.
3. Execute the program
To execute the program, you need to run -
Compile C Program Fedora Terminal
Compiling and Executing C++ program
C++ Compile And Run
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 -
If you have any problems then share it through comments. One more thing, This video might help you in running your first C program in Ubuntu 11.10(I’ve recorded it in Gnome Shell interface) -