Are you interested in writing C or C++ programs on Ubuntu Linux instead of MS-Windows? Using the bash Terminal application you can compile a C or C++ program on any Linux distro, like Ubuntu, Red Hat, Fedora, Debian and others. You just need to install GNU C and C++ compiler collection, development tools, development libraries and IDE or text editor to write programs. Let's discuss the process step-wise:
Step #3: How to Compile and Run C/C++ program on Linux:
Create a file called demo.c using a text editor such as vi, emacs or joe:
Step #4: How to Compile the program on Linux?
Use any one of the following syntax to compile the program called demo.c:
cc program-source-code.c -o executable-file-name
OR
OR ## assuming that executable-file-name.c exists ##
OR
## assuming demo.c exists in the current directory ##
If your code or C program has no error, then an executable file called demo will be successfully created by the compiler in the current directory, otherwise the code can be fixed. To verify this, type:
Step #5:How to run or execute the program called demo on Linux?
Simply type the the program name:
Compiling and running a simple C++ program
Create a program called demo2.C as follows:
To run this program, type:
Step #6: How to generate symbolic information for gdb and warning messages?
The syntax is as follows C compiler:
Step #7: How to generate optimized code on a Linux machine?
The syntax is as follows C compiler:
The syntax is as follows when need pass the -lm option with gcc to link with the math libraries:
Step #9: How to compile a C++ program that uses Xlib graphics functions?
The syntax is as follows when need pass the -lX11 option with gcc to link with the Xlib libraries:
Step #10: How to compile a program with multiple source files?
The syntax is as follows if the source code is in several files (such as light.c, sky.c, fireworks.c):
Step #1: Install C/C++ compiler and related tools:
Use the following yum command to install GNU C/C++ compiler, on Fedora, Red Hat, CentOS or Scientific Linux:
# yum groupinstall 'Development Tools'
In case you are using Debian or Ubuntu Linux then you need the following apt-get command to install the same:
$ sudo apt-get update $ sudo apt-get install build-essential manpages-dev
Step #2: Verify installation:
Type the following command to display the version number and location of the compiler on Linux:
$ whereis gcc $ which gcc $ gcc –version
Here is a sample of the output:
Step #3: How to Compile and Run C/C++ program on Linux:
Create a file called demo.c using a text editor such as vi, emacs or joe:
#include /* demo.c: My first C program on a Linux */ int main(void) { printf("Hello! This is a test prgoram.\n"); return 0;
Step #4: How to Compile the program on Linux?
Use any one of the following syntax to compile the program called demo.c:
cc program-source-code.c -o executable-file-name
OR
gcc program-source-code.c -o executable-file-name
OR ## assuming that executable-file-name.c exists ##
cc demo.c -o demo
OR
## assuming demo.c exists in the current directory ##
make demo
If your code or C program has no error, then an executable file called demo will be successfully created by the compiler in the current directory, otherwise the code can be fixed. To verify this, type:
$ ls -l demo*
Step #5:How to run or execute the program called demo on Linux?
Simply type the the program name:
$ ./demoOR
$ /path/to/demo
Compiling and running a simple C++ program
Create a program called demo2.C as follows:
#include "iostream" // demo2.C - Sample C++ prgoram int main(void) { std::cout << "Hello! This is a C++ program.\n"; return 0; }To compile this program, enter:
make demo2
To run this program, type:
./demo2
Step #6: How to generate symbolic information for gdb and warning messages?
The syntax is as follows C compiler:
cc -g -Wall input.c -o executableThe syntax is as follows C++ compiler:
g++ -g -Wall input.C -o executable
Step #7: How to generate optimized code on a Linux machine?
The syntax is as follows C compiler:
cc -O input.c -o executableThe syntax is as follows C++ compiler:
g++ -O -Wall input.C -o executableStep #8: How to compile a C program that uses math functions?
The syntax is as follows when need pass the -lm option with gcc to link with the math libraries:
cc myth1.c -o executable -lm
Step #9: How to compile a C++ program that uses Xlib graphics functions?
The syntax is as follows when need pass the -lX11 option with gcc to link with the Xlib libraries:
g++ fireworks.C -o executable -lX11
Step #10: How to compile a program with multiple source files?
The syntax is as follows if the source code is in several files (such as light.c, sky.c, fireworks.c):
cc light.c sky.c fireworks.c -o executableC++ syntax is as follows if the source code is in several files:
g++ ac.C bc.C file3.C -o my-program-name
0 comments: