compiling and running procedure for c++ programs

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.

compiling and running procedure for c++ programs

Postby raza » Thu Jul 21, 2005 3:59 pm

Hi dears ,
can anybody help k how to compile and run a C++ program in red hat 9 with small example of hello world program. I think there is something to take care about header extension or any thing else.

following is the program i tried but getting error messages only


[root@linux root]# g++ -c -g -O seeplusplus.cc


seeplusplus.cc:1: `iostream' was not declared in this scope
seeplusplus.cc:2: syntax error before `using'
seeplusplus.cc: In function `int main()':
seeplusplus.cc:6: `cout' undeclared (first use this function)
seeplusplus.cc:6: (Each undeclared identifier is reported only once for each
function it appears in.)

how to solve the problem

Regards

Raza
raza
Lance Naik
 
Posts: 21
Joined: Tue Jul 12, 2005 2:01 pm

always paste code :)

Postby AsadRasheed » Thu Jul 21, 2005 5:24 pm

include <iostream.h>
#include <stdio.h>

main()
{

cout << "raza is g++ master \n";

}

always paste code when u ask question regarding some codeing error.

$g++ -o test test.cc
$./test
raza is g++ master
$
AsadRasheed
Battalion Quarter Master Havaldaar
 
Posts: 228
Joined: Fri Jan 28, 2005 6:23 pm
Location: Karachi

Re: always paste code :)

Postby jargon » Thu Jul 21, 2005 5:49 pm

AsadRasheed wrote:include <iostream.h>
#include <stdio.h>

main()
{

cout << "raza is g++ master \n";

}

always paste code when u ask question regarding some codeing error.

$g++ -o test test.cc
$./test
raza is g++ master
$



WHAT!!!

if it's C++ the standard is

no need for *.h unless it's a C library
you need. Using stdio.h is redundant
if you're using iostream.

#include <iostream>
using namespace std;

int
main(int argc, char *argv[])
{

// code

return(0);
}


or instead of using the 'using' directive
you can place a 'std::' in front of every 'cout'
jargon
jargon
Lieutenant Colonel
 
Posts: 691
Joined: Mon Oct 13, 2003 9:40 am

Postby Anjum Butt » Fri Jul 22, 2005 4:53 am

Well said jargon

People shouldn't mix 'C' code with 'C++'.
Make a good habit of keeping C++ code clean from legacy C code.

also, when in C++ try avoiding C header files
and in an unavoidable stiutaion, do like this:

extern C {
#include <legacyheader.h>
}

this will prompt the compiler to use C style linking for encapsulated code, rather than C++ style linking.
More will follow :)

Anjum Butt
Registered Linux User #314299
Anjum Butt
Company Havaldaar Major
 
Posts: 165
Joined: Mon Jun 09, 2003 9:02 am
Location: Karachi


Return to “%s” Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron