Search This Blog

Wednesday, June 4, 2014

Compiling C++ program in linux


Compiling C++ program in linux is little bit different than Compiling a C program ,

             gcc compiler is the commonly used compiler for c in linux but for compiling c++ program in linux we need to use another compiler called g++ .the syntax for this is

g++ <your_program>

It compiles the program and gives a.out as the output

But there is a problem with this , if any of the modern concepts like range for is used in the program it gives an error like,

15.cpp: In function ‘int main()’:
15.cpp:10:14: error: range-based ‘for’ loops are not allowed in C++98 mode
 for(char c : str){
 
the modern concepts are compiled with C++11 supported compiled. so there is a need to use extra option for compiling this.


i.e.,

g++ -std=c++0x <your program>

Using this flag every time is somewhat difficult so we nee to enable c++11 perminently

To set that first open the terminal

sudo gedit ~/.bashrc

then a file will be opend , now add below line as the last line

alias g++="g++ --std=c++0x" 

then save the file and restart the terminal then compile the program as

g++ <your program>


if you want to compile non c++11 program then you must use
g++ --std=c++98 <your program>




No comments:

Post a Comment