How to Install C and C++ Compilers in Ubuntu and testing your first C and C++ Program
Sponsored Link
Install C and C++ Compilers in Ubuntu
sudo aptitude install build-essential
This will install all the required packages for C and C++ compilers
Testing C and C++ Programs
Compiling Your first C Programs
Now you need to open first.c file
sudo gedit first.c
add the following lines save and exit the file
Firstly compile the code using the following command
cc -c first.c
that would produce an object file you may need to add to the library.
then create an executable using the following command
cc -o first first.c
Now run this executable using the following command
./first
Output should show as follows
Hello, world
Compiling your first C++ program
If you want to run c++ program follow this procedure
g++ is the compiler that you must use.
you should use a .cpp file extension rather than a .c one
You need to create a file
sudo gedit first.cpp
add the following lines save and exit the file
Run your C++ Program using the following command
g++ first.cpp -o test
./test
Output should show as follows
Hello World!
@ Pral
Hey in unix system there are no drive concept (like c:\,d:\ etc). all are these declare as a file system. you can find them at /media/(file system name). you want to paste some .h file? but there are no need for this because ubuntu have preinstall these library files.
Thanks for all.
It is useful to install manpages in addition to the compilers. You can do this for example using the following command:
sudo apt-get install build-essential manpages-dev
…or if you have already installed the build-essential package:
sudo apt-get install manpages-dev
Thanks for all
STEPS TO COMPILE C AND C++ ON UBUNTU
For compilation of c code:
1.Open : applications>accessories>terminal
2.Type the following command in the terminal
gedit name of ur file.c
// I HAVE GIVE MY FILE NAME AS main.c
For me i will type gedit main.c inside terminal
3.Write ur code inside the gedit editor:
#include
#include
int main(int argc, char** argv)
{
printf(“NAMAH SHIVAY\n”);
return 0;
}
4.save the code by pressing ctrl+s
5.In the terminal itself goto :File>Open terminal and do as follows in the newly opened terminal
6.Compile the code using the following command :
cc -c main.c
7.This would produce an object file now you may need to add to the library.
Then create an executable using the following command :
cc -o main main.c
8.Now run this executable using the following command :
./main
FOR ANY DOUBTS MAIL ME
[email protected]
sudo aptitude install build-essential
worked like a charm. thanks!
sudo aptitude install build-essential will not work any more.you should use sudo apt-get install build-essential
THanks
“sudo aptitude install build-essential
This will install all the required packages for C and C++ compilers”
For this do u write that code in the terminal to download the compiler
and if so the terminal says that
the sudo:aptitude: command not found
plzz help ty
Guys i had a new idea install wine & get borland c 5.02.then install it with wine. it is most compatible for ubuntu 10.04 & ubuntu 11.04. if you use ubuntu 10.10 you must install borland c 4.5(or)turbo c 4.5.
guys i think it will works for other versions of ubuntu too
Hey Guys, thanks for the help! This worked for me and I ran my C++ program. Once you create the editor file in Ubuntu, how would open the file back up?
Super helpful. Thanks!
At patricia; Just open up (sudo gedit yourfile.cpp) and keep editing. the built (compiled) version of your code is a different animal all together from the cpp file that you are editing.
thanks
try this code:
#include
void main()
{
printf(“hello world!”);
getchar();
}
is the same but you dont have to write “return 0;”
🙂
Hey, when I run the program, I typed as follow:
./test
it shows the error message ” bash: ./MyFirstPointer: No such file or directory”
Help me. What I need to do? Thanks a lot!
sorry, I typed as
./MyFirstPointer
bash: ./MyFirstPointer: No such file or directory
it’s what I got!
That really helpful
now I can develop apps using C and C++
Thanks
thank u dude working fine
Hello-
HELP! Sometime soon, I would like to port some of my old C code projects to the Ubuntu environment, and compile them into executables there. Is there some documentation in one piece somewhere explaining the vagaries of the available standard compiler? Things like how to handle I/O devices other than console, floating point and other numerical precision options, interfacing to assembly language modules (and incidentally assemblers, preferably macro, that are available and compatible with the compiler). These and 100 other questions. Finding answers online might be possible for some things, but is tedious. Sorry my cyber-youth friends, but nothing beats a real book! A solid recommendation of such a book (perhaps one of the O’Reilly series) without a lot of generalized hand waving would be appreciated by this old timer, though Linux newbee.
try,
sudo apt-get install build-essential
The C++ code example shown above won’t compile with g++. To make it work, the vertical bar after the left brace in the fourth line must be deleted.
The “bar” may actually be a screen capture of the text cursor. Why were screen images used in the code examples in this article, instead of actual text?
Here’s the corrected code:
#include
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
(The comment form sorely needs a “preview” feature so that one will know how it will look before posting.)
lot of thanks for helping in installing “build-essential”package now I can make run program in c and c++ from linux
thanx once again
Following is the code that I type in terminal.
I have made hello.c file under /home/rana/C/ using gedit. please help
rana@rana-FS-1050:~$ cc -c hello.c
cc: error: hello.c: No such file or directory
cc: fatal error: no input files
compilation terminated.
rana@rana-FS-1050:~$ cc -c /home/rana/C/hello.c
rana@rana-FS-1050:~$ ./hello
bash: ./hello: No such file or directory
I installed c++ compiler using , sudo apt-get install build-essential
when i type this code i got a message as below, what i must do now
#include
int main()
{
initscr();
move(12,30);
printw(“hello\n”);
getch();
endwin();
return 0;
}
madhura@madhuwa:~$ g++ -omad first.cpp -lncurses
first.cpp:1:21: error: ncurses.h: No such file or directory
first.cpp: In function ‘int main()’:
first.cpp:4: error: ‘initscr’ was not declared in this scope
first.cpp:5: error: ‘move’ was not declared in this scope
first.cpp:6: error: ‘printw’ was not declared in this scope
first.cpp:7: error: ‘getch’ was not declared in this scope
first.cpp:8: error: ‘endwin’ was not declared in this scope
madhura@madhuwa:~$
Hi,
Could someone please help me out in the below error, when I tried to compile the C program in my terminal.
Program:
#include
int main()
{
printf(¨Hello, world¨);
return 0;
}
$ cc -c first.c
first.c: In function ‘main’:
first.c:5: error: stray ‘\302’ in program
first.c:5: error: stray ‘\250’ in program
first.c:5: error: ‘Hello’ undeclared (first use in this function)
first.c:5: error: (Each undeclared identifier is reported only once
first.c:5: error: for each function it appears in.)
first.c:5: error: stray ‘\302’ in program
first.c:5: error: stray ‘\250’ in program
first.c:5: error: ‘world’ undeclared (first use in this function)
@Suresh,
The “quotes” surrounding “Hello, world” in your printf statement aren’t quotation marks. Be sure you’re composing your code on a text-only editor, such as vi, gedit, Notepad, etc., and not with a word processor such as Microsoft Word. The character you want appears on U.S. keyboards just above the apostrophe, on a key that is just to the left of the Enter key.
hey it is very easy to install c++