您的位置:首页 > 编程语言 > C语言/C++

Compile c/c++ program on ubuntu

2013-07-24 20:47 555 查看
1.Open up a terminal on Ubuntu Linux and install the build-essential package by typing the following command in the terminal

sudo apt-get install build-essential

2.Create
a directory and a sub directory to hold your C/C++ programs and your main HelloWorld program.

mkdir -p CCPP/HelloWorld

3.Then
we will change into our created directory by issuing the following command

cd CCPP/HelloWorld

4.Next
we will use a text editor such as gedit or nano to create our C or C++ source code using the following command.

5.For
example for a C source code file we would issue the following command

gedit main.c

6.Enter the following
C source code below:

#include<stdio.h>

#include<stdlib.h>

int main()

{

printf("\nHello World,\nWelcome to my first C program in Ubuntu Linux\n\n");

return(0);

}
7.Save the file as main.c and
exit
8.For
example for a C++ source code file we issue the following command

gedit main.cpp
9.Add
the following lines below to create your C++ source code:

#include<iostream>
using namespace std;

int main()

{

cout<<"\nHello World,\nWelcome to my first C ++ program on Ubuntu Linux\n\n"<<endl;

return(0);

}
10.Save the file as main.cpp
and exit
11.Compiling
your C/C++ program

Important: Make sure you are in the CCPP/HelloWorld directory before you compile your C/C++ programs.

12.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.

13
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

14
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

15
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

From :http://www.wikihow.com/Compile-a-C/C%2B%2B-Program-on-Ubuntu-Linux
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: