LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-13-2003, 08:10 AM   #1
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Rep: Reputation: 31
Compiling C++ GCC


Hello

I write a simple c++ program:

#include<iostream.h>

int main()
{
cout << "HI" << endl;
return 1;
}

try to compile under Linux RedHat 9.0 (it does have gcc installed)

gcc me.cpp -o me

and it will keep saying:

cout not declared (each undeclared function is displayed only once)
endl not declared bla bla

what happens

Thanks
 
Old 10-13-2003, 08:38 AM   #2
maroonbaboon
Senior Member
 
Registered: Aug 2003
Location: Sydney
Distribution: debian
Posts: 1,495

Rep: Reputation: 48
How about

g++ me.cpp -o me

instead?
 
Old 10-13-2003, 09:00 AM   #3
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
won't work the thing is that it won't recognize iostream.h, cout, endl and stuff............ is the library ok?
 
Old 10-13-2003, 09:10 AM   #4
ksd
Member
 
Registered: Sep 2003
Location: lost in Eastern Kansas,USA
Distribution: FC3,Slackware ,ubuntu
Posts: 130

Rep: Reputation: 15
hey buddy...
try doing this for compiling

g++ compile.cpp

now the output is stored in a.out...this is standard file..

so for execution..ie just execute the file

./a.out

this is one way of doing it.

the other way is

for compiling

g++ compile.cpp -o xxx.out


and while executing...just call

./xxx.out

this means the output is stored in xxx.out instead of hte default a.out
 
Old 10-13-2003, 09:29 AM   #5
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
Hello:

that's not the problem. The thing is that it says : file iostream.h doesn't exist.

I tried to compile another file without any including:

int main()
{
int a = 0;
for(int i = 0; i<2; i++);
return 0;
}

and it says: for initialized outside cycle 99

however, it does create a.out I try to run it and say bash file doesn't exists.

I'm kinda desperated......... it's there such thing as c++ for dummies like DevC++ for linux? hehe
 
Old 10-13-2003, 09:34 AM   #6
ksd
Member
 
Registered: Sep 2003
Location: lost in Eastern Kansas,USA
Distribution: FC3,Slackware ,ubuntu
Posts: 130

Rep: Reputation: 15
if iostream.h is not detected then there s some thing wrong with your package.try to load new version from the slackware site...thats all i can say....good luck
 
Old 10-13-2003, 09:41 AM   #7
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
yeah I believe it will work.......... where can I download it?
 
Old 10-13-2003, 09:42 AM   #8
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
sorry about it guys I'm completely new at linux
 
Old 10-13-2003, 10:50 AM   #9
RandomLinuxNewb
Member
 
Registered: Oct 2003
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
Lightbulb

The first problem I see is that your using the old header files. The new standard header files do not include the .h at the end. The next error I see is that your not declaring the namespace for the functions cout and endl. Try this code out.
PHP Code:
#include <iostream> // notice how it does not have the .h
using namespace std;  // this is how you use a namespace
/*
     you could also do this:
     using std::cout;
     using std::endl;

     This is the better way to do as your not using the entire std namespace.
*/

int main()
{
     
cout << "Hi" << endl;
     return 
0;     // returning anything besides a 0 indicates an error.

 
Old 10-13-2003, 10:20 PM   #10
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
it does compile now.......... I write:
g++ test.c -o me.out
and it will create me.out
but I type me or me.out and it keeps saying:
bash: me.out: command not found
but the file is there I see it with ls
 
Old 10-13-2003, 11:24 PM   #11
tuxfood
Member
 
Registered: Aug 2003
Location: kerala , India
Distribution: RH9 , FC1 ,
Posts: 141

Rep: Reputation: 15
hi,
as the compiling went on fine,half the work is over.
i think you should try running by using the "./" infront of the executable file.

bye,
tuxfood
 
Old 10-14-2003, 06:12 AM   #12
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
HI
It just keeps saying file doesn't exist even when I use

g++ test.c -o me.out

it creates me.out and I try:

me.out./ as you said and it won't work.
If I compile using g++ it does create the out. but if I try cc or gcc it says iostream doesn't exist and cout, endl and namespace should be declared first.

Is it possible I didn't install Red Hat correctly? I see everyone compiles just fine
 
Old 10-14-2003, 06:19 AM   #13
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
I can compile now!
thanks a lot to everyone.......... why do I have to use a ./ in front of the file?
I'm used to C++ under Windows..... but I don't know whats about *.out files...... if I try to write something a little more complicated (ie I use several .h and .cpp files) how can I join them into one exe?
thanks thanks thanks a lot I really appreciate it.
 
Old 10-14-2003, 01:05 PM   #14
tuxfood
Member
 
Registered: Aug 2003
Location: kerala , India
Distribution: RH9 , FC1 ,
Posts: 141

Rep: Reputation: 15
use ./

hi,
i would suggest that u try using ./
first of all,try to get the object file as it is,i.e compile using the basic command
g++ file.cpp
no g++ -o..... etc.
if the compiling went fine(as u have told)
u should be able to find a file a.out in your directory.
now type ./a.out to run the program.
now why to use ./ in front............
from what i have heard,object files are executed using the ./ command in front of the name of the file.(try to get output atleast usiong this)

now to run several cpp files together,from what i have heard
it is enough to compile them together.
i.e
g++ file1.cpp file2.cpp .................
(please reconfirm on this and post the answer)

good luck

bye,
tuxfood
 
Old 10-14-2003, 04:23 PM   #15
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
Hi

yeah I've finally managed to compile and run succesfully. Just a simple program. I'll try to compile several files later tonight. thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
compiling problems with gcc 3.4 1337 Twinkie Linux - Software 1 05-13-2005 03:51 PM
Compiling using gcc Damitha Mandriva 1 12-17-2004 05:11 AM
Kernel compiling: gcc-3.3 is 586, should be gcc-3.3 386 Erik Plaggenmar Linux - Software 0 10-01-2004 11:38 AM
Various Compiling Errors (GCC compiling Openal, GUIlib, xmms-wma) gregorya Linux - Software 2 08-27-2004 05:03 AM
Compiling gcc CuteBug Linux - Software 2 02-22-2004 09:19 AM


All times are GMT -5. The time now is 01:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration