Under Linux you need to use the cc/gcc (GNU project C and C++ compiler) command to compile a program written in C or C++. When you compile a program it generates an executable file called a.out.
Syntax
The syntax is:
gcc -o output-file program.c
OR
cc -o output-file program.c
OR
make program.c
Sample code
Here is a sample C code called test.c that I am going to compile using GNU C compiler:
/* Purpose: A simple program to get name and information about current kernel * using uname(2) on a Linux. * Author: Vivek Gite <https://www.cyberciti.biz> */ #include <sys/utsname.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/utsname.h> int main(void){ int i; struct utsname myname; i = uname(&myname); /* hold the structure */ if ( i == 0 ){ printf("Operating system name : %s\n",myname.sysname); printf("Node name : %s\n",myname.nodename); printf("Operating system release : %s\n",myname.release); printf("Operating system name : %s\n",myname.version); printf("Hardware identifier : %s\n",myname.machine); } else { fprintf(stderr,"Oh no. uname(2) failed with %s\n", strerror(errno)); exit(1); } return 0; }
Task: Compile program
To compile type the command:
$ gcc test.c
OR
$ cc test.c
Task: Execute program to see output
Above command will create a file called a.out. To see output of test.c program type:
$ ./a.out
Task: Compile to specific executable file
You can specify an execuable file name while compiling program itself:
$ gcc -o test test.c
OR
$ cc test.c -o test
OR
$ make test
Now execute the test program to see output of test.c on screen:
$ ./test
Sample outputs:
- Ubuntu Linux Install GNU GCC Compiler and Development Environment
- Debian Linux Install GNU GCC Compiler and Development Environment
- CentOS / RHEL 7: Install GCC (C and C++ Compiler) and Development Tools
- Download and Install C, C++ Compiler on Red Hat Enterprise Linux 5 (RHEL)
- Mac OS X: Install GCC Compiler with Xcode
- Where is My Linux GNU C or GCC Compilers Are Installed?
- HowTo: Compile And Run a C/C++ Code In Linux
- RHEL / CentOS Linux Install Core Development Tools Automake, Gcc (C/C++), Perl, Python & Debuggers
- HowTo Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD
- How To Install ncurses Library on a Linux
- Linux Find Out What Compilers Are Installed or Available On The System
- Linux Find Out GNU gcc Compiler Version Used To Compile Running Kernel
- Howto see output of C program in Linux or UNIX
🐧 55 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
yaa
you help me a lot
thnx budy
wt is the process to get output in c program
After written the program in c language
To Compile that program in turboc click the ALT+F9
To Run that program in turboc click the CTRL+F9
hey man thanks for the instruction…… i can now compile and run c programs……
this is realy very nice.thanks to help me.
i have installed MinGW now pls tell how to write a program and excuate it
Hi,
from your site i came to know how to compile and see out put of program, but how to write c program in linux????I m a window user,so if anyone give me that ans i will be obliged.
Use just have to do write ur program in gedit and save it with c extension…..
U have to go In file right click then document then click empty document….
Now u can write ur program this is gedit then save it filename.c extension…..
Have…fun…..
i search this site through google and find my answer on the topic … How can i run and compile program on gcc compiler?
Nice post. It helps newbie a lot.
:)
The given commands is work properly…………
it is very helpful
Thanx man!!!
Thanks for this tip
Plz also tell me how to enter the out put of one c program to another one plz send this at my email address.
Thanks for this tip
Superb information , i liked this website very much.
i am executing c program in fedora as gcc temp.c but it says that
bash: gcc: command not found
please tell how to run
Install gcc compiler.
@bhargav:
Update your YUM repo (in Fedora/ RHEL), thereafter
run the following command as root:
yum install gcc
or simply write
sudo yum install gcc
have fun!
I am successfully executing a C prg in Turbo C but i am getting a runtime error in gcc. Please help
@Ivneet:
Please be specific about your program. Paste it anywhere and give us the link.
Awesome man!!!!!\
this really helped………..
i m a new user for linux platform,may any body plz let me know how to code,compile etc a graphics application for c++ under linux platform ,,,,,( explain with example plz)
it is showing that gcc command not found . so what to do help me body.
@vaibhav!
probably gcc is not installed. Install it first depending on your linux.
and try the commands. !
http://susenj.wordpress.com
i hav c++ program in linux how i ll run it in windows c++ because it gives 25 errors
thx buddy it really helps me….
their is some IDE for C++ environment…
@shivam:
yes there is: like KDevelop, Anjuta, Kate etc.
thanx…….
u help me lot……….
THANK U….:)
I use Fedora 12 (live Cd).
i created a c program in it and tried to execute.
it says that no such command..
how to eliminate this problem.
Thanks
Thanks…
how to get o/p in c program pls explain me na
the output command does not do on fedora 13
#./test
i hav c program in linux how i ll run it in windows c because it gives 25 errors
thanks..alot it just helped me out
I am sreedhar,i am doing MCA.I have interest to learn programming languages in linux operating system.so,what should i do.
thank u
ha, of course. Thanks for posting this. Saved me an embarrassing question on some IRC channel or a forum.
Good work dude….proves to be very useful for the begginers
I have typed a hello world program in fedora.. But how should i get back to the terminal.what are the keys on keyboard that i hav t press and want commands to type so that my file is saved.
Create ur file in edit n save the file with .c extension…..
Then compile……then run….through commands
thnx…..for providin this nice solution…
it really helped me thanks
thanks it is very helpful for me
thank you :) !
Is gcc compiler necessary for C++ programming under Linux????
Can we do this without any compiler????
Any language needs a compiler/interpreter.
I can even run Turbo C++ on Linux.
so, answer to your first question = no, but yes, it’s the most preferred compiler for any *nix Operating system.
for second question = No.
Neeraj,
thank you so much for ur help and consideration
thanq nd what abt html programmes
Sorry, There is no programming language called HTML.
What exactly you want to do with HTML under gcc?
hey there, can you tell me how to see the output of a c++ program in unix environment page by page ???
Thank Man…
thanks to this web site
Please show me how to write a program in c that prints a file a screenfull at a time waiting for response from user.
thank u ….
How about if i am making use of code block how do i get to see the output on the screen\