LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-14-2004, 08:58 AM   #1
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Rep: Reputation: 15
Question how to compile c++ programs with gcc


Hi

I have a stupid question: I tried to compile the 'hello world' program to check if my c++ compiler works.

My code:
#include <iostream>

int main(){
cout << ''Hello World" << endl;

return 0;
};

konsole in/output:
gcc -o test test.cpp

test.cpp: in function `int main()'
test.cpp:4: `cout' undeclared (first use this function)
test.cpp:4: `endl' undeclared (first use this function)

I have already check the path to the iostream library (should be fine).
Where is the problem?
 
Old 05-14-2004, 09:52 AM   #2
fsbooks
Member
 
Registered: Jan 2002
Location: Missoula. Montana, USA
Distribution: Slackware (various)
Posts: 449

Rep: Reputation: 31
How about trying g++ instead of gcc? :-)
 
Old 05-14-2004, 10:11 AM   #3
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Original Poster
Rep: Reputation: 15
I've already tried with g++.... exactly the same result...

First I thought, the problem lies in the fact I'am running the machine with a smp kernel. So I started my computer with the normal single cpu kernel, with the same results...

by the way: installed
gcc: 3.2.2
kernel: 2.4.20-8.smp/2.4.20-8

in the iostream file is a line
#include <bits/c++config.h>
but in the directory 'bits' is no file called 'c++config.h' , instead i found the 'c++config.h' in the directiory 'i386-redhat-linux/bits/c++config.h'

might this be a problem?
 
Old 05-14-2004, 11:00 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Try this instead:

Code:
#include <iostream>

using namespace std;

int main(){

cout << ''Hello World" << endl;

return 0;
};
Or:

Code:
#include <iostream>

int main(){
std::cout << ''Hello World" << std::endl;

return 0;
};
The <iostream> header and all of the standard C++ headers that don't have the .h add everything to a namespace named std. Adding "using namespace std;" basically gives everything in the std namespace a global scope so that it can be used without explicitly referencing it.

Last edited by deiussum; 05-14-2004 at 11:02 AM.
 
Old 05-14-2004, 11:16 AM   #5
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Original Poster
Rep: Reputation: 15
Thanks a lot for your input, of course i forget the namespace, but it didn't solved the problem....

/tmp/ccxS4t1k.o(.text+0x14): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/tmp/ccxS4t1k.o(.text+0x21): In function `main':
: undefined reference to `std::cout'
/tmp/ccxS4t1k.o(.text+0x26): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccxS4t1k.o(.text+0x2f): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >:perator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccxS4t1k.o(.text+0x5c): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccxS4t1k.o(.text+0x8b): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccxS4t1k.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

this output reminds me at the output, where i tried in the old fashioned way with #include <iostream.h> (Once I've learned like this ;-))
 
Old 05-14-2004, 11:33 AM   #6
fatah
LQ Newbie
 
Registered: May 2004
Location: Shah Alam
Posts: 2

Rep: Reputation: 0
so, this error also print when using g++, eh?
 
Old 05-17-2004, 03:05 AM   #7
nimra
LQ Newbie
 
Registered: May 2004
Posts: 28

Original Poster
Rep: Reputation: 15
Talking

You're right, now it's running!!!!

Thank's a lot for your advice!

What's the difference between g++ and gcc then? I always thought, that it is the same program. Apparently,it is not.

 
Old 05-17-2004, 08:08 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,043

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
gcc -> c compiler
g++ -> c++ compiler
 
  


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
can't compile simple C++ programs on Suse 9.1 after installing gcc cyclebain Linux - Software 9 07-04-2007 09:04 AM
Windows programs in gcc velan Programming 2 03-21-2005 09:26 AM
gcc can't compile noir911 Programming 1 09-25-2004 11:35 AM
gcc can't compile zionz Linux - Newbie 2 09-03-2004 12:34 AM
How to compile GCC if you don't have GCC? khermans Linux - Software 13 05-12-2004 07:42 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration