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
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
相关文章推荐
- How to Compile and Run C/C++ program on Ubuntu 11.10
- Compile my helloworld cpp program on Ubuntu linux
- How to compile Android 2.3 on Ubuntu 10.04 32bit
- emacs as the c++ ide on the Ubuntu
- How to compile OpenJDK on Ubuntu
- How to Compile Android on Ubuntu (12.04)
- How to compile mplayer with vdpau support on Ubuntu
- [C++] Build Shared libraries with GCC on Linux ---gcc is actually for compile C
- Compile Linux Kernel on Ubuntu 12.10
- Matlab R2013a: C++ MEX on Ubuntu 14.04 64-bit
- linux kernel stable compile on ubuntu
- Compile FFmpeg on Ubuntu
- How to fix “System program problem detected” error on Ubuntu
- compile rtplib3.9.1 on linux(ubuntu 12.10)
- Matlab R2013a: C++ MEX on Ubuntu 14.04 64-bit
- Compile Bitcoin Core from Source on Ubuntu
- Compile ASOP Froyo for Dream on Ubuntu 10.4
- maven bug之Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project acSpaceCommon: Fatal error compiling: tools.jar not found: C:\Program Files\J
- Compile Linux Kernel on Ubuntu 12.04 LTS (Detailed)
- Compile the latest Kernel(linux-3.1-rc4) On Ubuntu Plateform