Questions about this topic? Sign up to ask in the talk tab.

Difference between revisions of "CPP"

From NetSec
Jump to: navigation, search
(The code)
Line 15: Line 15:
 
<source lang="cpp">
 
<source lang="cpp">
 
  #include <iostream> //preprocessor directive, tells the compiler to 'include' the file "iostream"
 
  #include <iostream> //preprocessor directive, tells the compiler to 'include' the file "iostream"
//This is a comment by the way.
+
//This is a comment.
 
/*
 
/*
 
so is this.
 
so is this.
Line 34: Line 34:
 
</source>
 
</source>
 
}}
 
}}
 +
 
==Compiling the Hello World==
 
==Compiling the Hello World==
 
Assuming you have GCC installed, Save the previous code as "helloworld.cpp".
 
Assuming you have GCC installed, Save the previous code as "helloworld.cpp".

Revision as of 20:15, 22 November 2011

C++ is a compiled low-level programming language. It is an enhancement of the language C.

The name 'C++' comes from the increment operator in C, which is "++" therefore C++

Because C++ is a compiled language, you will need a compiler to compile your code. Most Linux distributions come with the proper tools such as gcc, and gdb installed. If you do not have these installed, you can install through your respective package manager.

 Debian/Ubuntu:   apt get build-essential
 Arch Linux:      pacman -S base-devel

Hello World

Here is a quick example of a "Hello World" application in C++, and how to compile it.

The code

 
 #include <iostream> //preprocessor directive, tells the compiler to 'include' the file "iostream"
//This is a comment.
/*
so is this.
except
it's
a multi-line
comment.
*/
//The compiler ignores comments.
 
//here is our "main" function.
int main(int argc, char *argv[])  //argc and argv stand for "Argument Count" and "Argument Vector"
{
 
    std::cout << "Hello World!" << std::endl; //use cout to print the text "Hello World!" to the console.
   return 0; //return 0 and exit.
}
 

Compiling the Hello World

Assuming you have GCC installed, Save the previous code as "helloworld.cpp". In the same directory that you saved your source file, open a terminal and type the following:

 gcc helloworld.cpp -o helloworld

Now GCC has compiled your source code into an executable file.

execute your hello world application with:

 ./helloworld