Sie sind auf Seite 1von 4

77,000 views Edited 21 weeks ago

Ad
How to Compile a C/C++ Program on
Ubuntu Linux
This article will show you how to compile a C/C++ program on Ubuntu Linux
using the GNU gcc/g++ compiler. Additions were made in order to simplify and
clarify the creation of a C/C++ program on Ubuntu Linux. Hopefully this article
will serve as a guide and tutorial to compiling GNU C/C++ programs on
Ubuntu Linux.
Lenovo Tablet A7-50
lenovo.com/A-750_Tablet
Perfect tablet for all your needs Ultra-portable &
affordable
1
Ad
2
3
Open up a terminal on Ubuntu Linux and install the build-essential package
by typing the following command in the terminal
Type/Copy/Paste: sudo apt-get install build-essential
This will install the necessary C/C++ development libraries for your Ubuntu
Linux system to create C/C++ programs.
Create a directory and a sub directory to hold your C/C++ programs and your
main HelloWorld program.
Type/Copy/Paste: mkdir -p CCPP/HelloWorld
We are using CCPP for the main directory to hold our created C/C++ programs
which stands for C and C+ + programs directory and we are using the sub
directory HelloWorld to hold our main program.
Then we will change into our created directory by issuing the following
command
Type/Copy/Paste: cd CCPP/HelloWorld
Assessment Software
questbase.com
Create Quizzes, Tests, Assessments. Exams.
New Features August 2014.
Steps
How to Compile a C/C++ Program on Ubuntu Linux: 15 ... http://www.wikihow.com/Compile-a-C/C++-Program-on-...
1 of 4 08/04/2014 12:16 PM
4
5
6
7
8
9
10
Next we will use a text editor such as gedit or nano to create our C or C++
source code using the following command.
For example for a C source code file we would issue the following command
Type/Copy/Paste: gedit main.c
or
Type/Copy/Paste: nano main.c
Enter the following C source code below:
Type/Copy/Paste:
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("\nHello World,\nWelcome to my first C program in Ubuntu Linux\n\n");
return(0);
}
Save the file as main.c and exit
For example for a C++ source code file we issue the following command
Type/Copy/Paste: gedit main.cpp
or
Type/Copy/Paste: nano main.cpp
Add the following lines below to create your C++ source code:
Type/Copy/Paste:
#include<iostream>
using namespace std;
int main()
{
cout<<"\nHello World,\nWelcome to my first C ++ program on Ubuntu Linux\n
\n"<<endl;
return(0);
}
Save the file as main.cpp and exit
How to Compile a C/C++ Program on Ubuntu Linux: 15 ... http://www.wikihow.com/Compile-a-C/C++-Program-on-...
2 of 4 08/04/2014 12:16 PM
11
12
13
14
15
Ad
Compiling your C/C++ program
Important: Make sure you are in the CCPP/HelloWorld directory before you
compile your C/C++ programs.
If you are compiling the C program version of Hello World type in the
terminal
Type/Copy/Paste: gcc -Wall -W -Werror main.c -o HelloWorldC
The first line will invoke the GNU C compiler to compile the file main.c and
output (-o) it to an executable called HelloWorldC.
The options -Wall -W and -Werror instruct the compiler to check for warnings.
If you are compiling the C++ program version of Hello World type in the
terminal
Type/Copy/Paste: g++ -Wall -W -Werror main.cpp -o HelloWorldCPP
If you should happen to get permission errors, you need to make the file
executable. You can do this by issuing the following commands below
Type/Copy/Paste: chmod +x HelloWorldC
or
Type/Copy/Paste: chmod +x HelloWorldCPP
In order to execute your program you will have to type in the following
commands.
To execute the C program version of the program:
Type/Copy/Paste: ./HelloWorldC
To execute the the C++ program version of the program:
Type/Copy/Paste: ./HelloWorldCPP
Free REST API Platform
dreamfactory.com
Install open source on any server Connect to SQL or NoSQL data
Make a blog for free
10.15% for Home Loans
How to Compile a C/C++ Program on Ubuntu Linux: 15 ... http://www.wikihow.com/Compile-a-C/C++-Program-on-...
3 of 4 08/04/2014 12:16 PM
If you don't use the -o option the name of the executable will be a.out
(by default).
Thanks to all authors for creating a page that has been read 77,174 times.
Ad
Categories: Ubuntu
Recent edits by: KamiDesu, Entricular, BR
Anti-Aging Secrets
alsearsmd.com
12 Natural Ways to Stimulate Your Cells to
Grow Younger!
Warnings
Article Info
How to Compile a C/C++ Program on Ubuntu Linux: 15 ... http://www.wikihow.com/Compile-a-C/C++-Program-on-...
4 of 4 08/04/2014 12:16 PM

Das könnte Ihnen auch gefallen