Compile C code gcc Linux

Compile C/C++ code GCC Linux

GCC stands for GNU Compiler Collection. GCC is an integrated collection of compilers for several major programming languages, which as of this writing are C, C++, Objective-C, Java, FORTRAN, and Ada. The GNU compilers all generate machine code, not higher-level language code which is then translated via another compiler.

How to Compile C/C++ code with GCC

// This will compile program.cpp file to executable file program.o
g++ program.cpp -o program
// g++ will place the executable file named a.out in your current directory
g++ myprogram.cpp
// -g option we enable debug mode(gdb debugger)
g++ -g program.cpp -o program
// compiler to generate some warning about you code when syntax is correct
g++ -Wall program.cpp -o program
//Generate optimize code
g++ -O program.cpp -o program
// Compile program with multiplier source code files
g++ program1.cpp program2.cpp -o program

Write my first program

/*
 * My first program
*/

#include <iostream>
int main() {
std::cout << "Hello, World from MyCodingLab!" << std::endl;
return0;
}//end of main

Execute the program after you compile it

admin@mycodinglab:/home/mycodinglab# g++ -g -Wall hello.cpp -o hello
admin@mycodinglab:/home/mycodinglab# ./hello
Hello, World from MyCodingLab!

Components of a C++ program

#include

All the lines starting with # singh are directives for the preprocessor. This tells to include the iostream standard file. In this example iostream includes the standard input-output library in C++.

// my first program in C++
This is a comment line. All lines beginning with // signs are considered as comments and they don’t have any effect on the program.
/* this is a comment */ /* This is a multiline comment */ You cannot nest comments.
/*nested */ comment */do NOT work */

using namespace std;
All C++ libraries are decralered in sertan namespace. The default namesapce is std.

int main () {// some code}
In the main function is where all C++ programs start their execution, independently of the location within the source code. All C++ programs must have main function.

cout <<“Hello, World from MyCodingLab!” << endl;
cout is the name of the standard output stream in C++, and It will print the text in semicolon character area.

endl;
Endl is indication end of line and print the message on a separate line.

return 0;
A return code 0 indicate the program run succssefully with no errors.

Have fun compiling! :)

You must be logged in to post a comment.

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com
%d bloggers like this: