LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-20-2006, 09:04 PM   #1
pengu
Member
 
Registered: Dec 2005
Location: Canton, GA, USA
Distribution: Ubuntu 7.10, FreeBSD, Debian
Posts: 207

Rep: Reputation: 30
Can't compile c++ program using gcc


Hi, I've been using linux for some time now, but am new to programming on it. When trying to compile a simple c++ program the compilation fails.

Program:
Code:
#include <iostream>
using namespace std;

// C++ Program

int main()
{
cout << "Hello World";
return 0;
}


Error:
Code:
/tmp/cc4geWt3.o: In function `main':
c++.cpp:(.text+0x25): undefined reference to `std::cout'
c++.cpp:(.text+0x2a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/cc4geWt3.o: In function `__tcf_0':
c++.cpp:(.text+0x47): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc4geWt3.o: In function `__static_initialization_and_destruction_0(int, int)':
c++.cpp:(.text+0x74): undefined reference to `std::ios_base::Init::Init()'
/tmp/cc4geWt3.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Thanks for the help,
 
Old 02-20-2006, 09:08 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
unless im missing something, it looks fine to me.

can u copy and paste the command u used to compile it
 
Old 02-20-2006, 09:52 PM   #3
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Rep: Reputation: 49
Quote:
#include <iostream>

I think it has to be iostream.h (You have iostream)
 
Old 02-20-2006, 09:58 PM   #4
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
Hey, the same thing happens on my compiler as well!

#include <iostream.h> works but not

#include <iostream>

But interestingly enough it warns me that <iostream.h> is deprecated and I should replace it with <iostream>.
I thought <iostream> was STANDARD, though.
 
Old 02-20-2006, 10:01 PM   #5
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
The problem here is that you are using gcc and not g++. The correct way of compiling would be something like,
Code:
$g++ -o program program.cpp
Quote:
Originally Posted by duffmckagan
I think it has to be iostream.h (You have iostream)
The code is alright. C++ header files are imported differently to C header files.
 
Old 02-21-2006, 07:14 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
if you use make it will g++ insted of gcc if your file has a .cpp extension.

you can use the inbuilt rules:
Code:
primadA$ touch 1.cpp
primadA$ make 1
g++     1.cpp   -o 1
(you don't need a makefile for simple files.
 
Old 02-21-2006, 09:35 AM   #7
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Rep: Reputation: 49
Quote:
The code is alright. C++ header files are imported differently to C header files.
Yeah. I am still learning C. :P
 
Old 02-21-2006, 07:03 PM   #8
pengu
Member
 
Registered: Dec 2005
Location: Canton, GA, USA
Distribution: Ubuntu 7.10, FreeBSD, Debian
Posts: 207

Original Poster
Rep: Reputation: 30
hi, thanx for the replies. redazzz was right, using "g++ -o ... ..." solved the problem. I would like to say thanks to redazz because i see him post really helpfull comments all the time. Unfourtanatly i just broke my arm today, so i wont be awnsering many posts for a while. Typing with one hand is hard
 
Old 02-22-2006, 02:10 AM   #9
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Quote:
Originally Posted by pengu
hi, thanx for the replies. redazzz was right, using "g++ -o ... ..." solved the problem. I would like to say thanks to redazz because i see him post really helpfull comments all the time. Unfourtanatly i just broke my arm today, so i wont be awnsering many posts for a while. Typing with one hand is hard
I'm also learning C++ so some of the stuff is still fresh in my mind.
 
Old 02-22-2006, 01:59 PM   #10
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Quote:
Originally Posted by pengu
hi, thanx for the replies. redazzz was right, using "g++ -o ... ..." solved the problem. I would like to say thanks to redazz because i see him post really helpfull comments all the time. Unfourtanatly i just broke my arm today, so i wont be awnsering many posts for a while. Typing with one hand is hard
Owwhhh... Get better soon! And good luck with the learning!
 
  


Reply



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
Compile GCC 3.3 mullins Linux - Software 1 08-04-2005 06:44 PM
gcc 3.4.1 want not to compile, but 3.2.2 does BorisB Linux - Software 3 07-27-2004 06:53 PM
How to compile GCC if you don't have GCC? khermans Linux - Software 13 05-12-2004 06:42 PM
Can't compile GCC Mikessu Linux - Software 2 05-05-2004 03:59 PM
how to compile with gcc 2.95 mu664life Debian 2 04-14-2004 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 AM.

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
Open Source Consulting | Domain Registration