Running C Program in gcc on Linux Platform:
Steps:
1. Create a file demo.c with a C code inside it.
$gedit <filename>
$gedit demo.c
- It will create and open demo.c file in gedit text editor
where you can type your c code, save and close it. e.g.,
#include<stdio.h>
void main()
{
printf("Hello C");
}
2. Compile demo.c#include<stdio.h>
void main()
{
printf("Hello C");
}
$gcc <source file name>
$gcc demo.c
- It will compile C program and create a.out executable binary file.
3. Running demo.c
$./<executable file name>
$./a.out
Note:
If you are dealing with multiple c files at a time, then it is
favorable to specify a name executable which can be done as below during
compile time.
$gcc <Source file name> -o <output file name>
$gcc demo.c -o demoout
now you can run it as
$./demoout
$gcc <Source file name> -o <output file name>
$gcc demo.c -o demoout
now you can run it as
$./demoout
Running C++ Program in gcc on Linux Platform:
Steps:
1. Install the 'build-essential' package.
$ sudo apt-get install build-essential { For Ubuntu }
2. Create a file demo.cpp with a CPP code inside it.
$gedit <filename>
$gedit demo.cpp
- It will create and
open demo.cpp file in gedit text editor where you can type your cpp code,
save and close it. e.g.,
#include <iostream>
using namespace std;
int main()
{
cout << "Hello C++";
return 0;
}
3. Compile demo.cpp#include <iostream>
using namespace std;
int main()
{
cout << "Hello C++";
return 0;
}
$g++ <source file name> -o <output file name>
g++ demo.cpp -o hellocpp
- It will compile C program and create a.out executable binary file.
4. Running demo.cpp
$./<executable file name>
$ ./hellocpp
Debugging C Program in GCC on Linux platform:
- Create C program as below
- $ gedit demo.c
- type your code in this demo.c file
- example:
#include<stdio.h>
void main()
{
int i=0;
while(i<10)
{
printf("\n %d",i);
i++;
}
}
void main()
{
int i=0;
while(i<10)
{
printf("\n %d",i);
i++;
}
}
- Compile this program with -g option so that compiler can embed debugging information in executable.
- $gcc -g demo.c -o demo
- Next to start debugger run below given command.
- $gdb demo
- gdb --- debugger gets loaded.
- Now make us of below commands for various operations.
- Important gdb commands
- help [gdb-command]
- run args
- Run the program till breakpoint / end
- break [function-name | line-number]
- Insert breakpoint at specific line.
- You can have one or more breakpoints.
- delete [bp-number]
- To delete breakpoint.
- next [N]
- Debugger will execute the next line as single instruction.
- next 3, jumps 3 lines.
- step [N]
- Same as next, but does not treats function as a single instruction, instead goes into the function and executes it line by line.
- finish
- run till end.
- continue
- continue until next breakpoint.
- where
- Same as next, but does not treats function as a single instruction, instead goes into the function and executes it line by line.
- quit
- quit debugger.
No comments:
Post a Comment