LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 07-13-2006, 07:06 PM   #1
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Rep: Reputation: 15
C++ hello world program error


Hi,

I am using Ubuntu 6.06 LTS .


Linux version 2.6.15-25-386 (buildd@terranova) (gcc version 4.0.3 (Ubuntu 4.0.3-1ubuntu5))


here is my hello world program
#include <stdio.h>

int main()
{
printf("Hello world");
return 0;
}


when i compile the above program as a c program it works

but when i try to compile the above program as a .cpp program it gives an error

junaid@Dev-jsahibzada:~$ gcc -o client client.cpp
/tmp/ccEBL7jl.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status



can any one tell me what the problem is?

Last edited by bahadur; 07-13-2006 at 07:09 PM.
 
Old 07-13-2006, 07:25 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,627
Blog Entries: 10

Rep: Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776Reputation: 776
Fist of all that's a C program that is just called cpp :}


Secondly you seem to be missing some library.
Try renaming it to .c instead of .cpp, and compiling it
again with its new name.


Cheers,
Tink
 
Old 07-13-2006, 07:34 PM   #3
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
hi TInk,

Thanks for the reply.

actually all c programs are working. only cpp ones are giving me that error

for example the following program

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

int main()
{
cout<<"Hello world\n";
return 0;
}


is giving me the error


junaid@Dev-jsahibzada:~$ gcc -o client client.cpp
In file included from /usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/backward/iostream.h:31,
from client.cpp:2:
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
/tmp/cco9I2s4.o: In function `main':client.cpp.text+0x27): undefined reference to `std::cout'
:client.cpp.text+0x2c): 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/cco9I2s4.o: In function `__tcf_0':client.cpp.text+0x46): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cco9I2s4.o: In function `__static_initialization_and_destruction_0(int, int)':client.cpp.text+0x6f): undefined reference to `std::ios_base::Init::Init()'
/tmp/cco9I2s4.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
 
Old 07-13-2006, 07:39 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,538

Rep: Reputation: 58
Quote:
#include <stdio.h>
#include <iostream.h>
for C++ programs they may use different headers, and youll need to leave out the '.h'.

Quote:
junaid@Dev-jsahibzada:~$ gcc -o client client.cpp
to compile C++ programs you use g++ not gcc.
 
Old 07-13-2006, 08:02 PM   #5
Gethyn
Member
 
Registered: Aug 2003
Location: UK
Distribution: (X)Ubuntu 10.04/10.10, Debian 5, CentOS 5
Posts: 900

Rep: Reputation: 31
Quote:
Originally Posted by nadroj
for C++ programs they may use different headers, and youll need to leave out the '.h'.
This isn't quite true. Calling headers as file.h is deprecated. For c++, you want to include <iostream> and <cstdlib>.

Last edited by Gethyn; 07-13-2006 at 08:03 PM.
 
Old 07-13-2006, 08:11 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,538

Rep: Reputation: 58
Quote:
Originally Posted by nadroj
for C++ programs they may use different headers
i wasnt saying specifically for the two mentioned.. i was just saying in general, there MAY be different versions of specific ones as a note to the thread starter in the future.
 
Old 07-13-2006, 08:13 PM   #7
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
bahadur -

Tinkster's right: GCC is treating your "C" program as though it were C++ (which is apparently what you want), and the linker isn't finding the right C++ runtime binaries.

SOLUTION:
Use "g++" instead of "gcc"
... OR ...
Add "-lstdc++" to the end of your command

EXAMPLE:
Quote:
gcc -o client client.cpp -lstdc++
BETTER:
Quote:
g++ -o client client.cpp

Last edited by paulsm4; 07-13-2006 at 08:15 PM.
 
Old 07-13-2006, 08:38 PM   #8
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
ok thanks for the help.

i used g++ instead of gcc and it worked but it still gives me the deprecated warning.

some how i have old libraries on my system.

my code looks like this now


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

int main()
{
  cout<<"Hello world\n";
  return 0;
}

and here is what i get when i compile the program

junaid@Dev-jsahibzada:~$ g++ -o client client.cpp
In file included from /usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/backward/iostream.h:31,
from client.cpp:2:
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.

when i use -Wno-deprecated then it works fine.

last of all if i remove the .h from the iostream.h the program simply cant find any include files.
 
Old 07-13-2006, 08:41 PM   #9
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,538

Rep: Reputation: 58
Quote:
To disable this warning use -Wno-deprecated.

when i use -Wno-deprecated then it works fine.
you mean it doesnt show you any warnings

Quote:
last of all if i remove the .h from the iostream.h the program simply cant find any include files.
can you show us the command you run and the output with the removed .h's, and the code i guess.
 
Old 07-13-2006, 09:00 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 9,919

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338
To not use -Wno-deprecated you need to change your code. c++ in recent years add some new features. You can google for more info but here is the basic change.

Code:
#include <iostream>
using namespace std;
     .
     .
     .
rest of your code
 
Old 07-13-2006, 09:04 PM   #11
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
ok folks here is what the problem was.

long time ago i had installed a wrong set of headers

so now i again resintalled the latest headers

i have the following headers on my system now

junaid@Dev-jsahibzada:~$ sudo find /usr -name '*linux-headers*' -print
/usr/share/doc/linux-headers-2.6.15-25-386
/usr/share/doc/linux-headers-2.6.15-25
/usr/share/doc/linux-headers-2.6.15-26
/usr/share/doc/linux-headers-2.6.15-26-686
/usr/src/linux-headers-2.6.15-25-386
/usr/src/linux-headers-2.6.15-25
/usr/src/linux-headers-2.6.15-25/linux-headers.revision
/usr/src/linux-headers-2.6.15-26-686
/usr/src/linux-headers-2.6.15-26
/usr/src/linux-headers-2.6.15-26/linux-headers.revision


i just installed the 686 ones.

i also added the line using name space std to the code.

and now its not giving me the warning.

i think the problem is fixed.

one thing i want to know is that how do i foce my compiler to use a specific set of headers and to ignore the others.?
 
Old 07-14-2006, 03:06 AM   #12
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 46
Quote:
Originally Posted by bahadur
ok folks here is what the problem was.

long time ago i had installed a wrong set of headers

so now i again resintalled the latest headers

i have the following headers on my system now

junaid@Dev-jsahibzada:~$ sudo find /usr -name '*linux-headers*' -print
/usr/share/doc/linux-headers-2.6.15-25-386
/usr/share/doc/linux-headers-2.6.15-25
/usr/share/doc/linux-headers-2.6.15-26
/usr/share/doc/linux-headers-2.6.15-26-686
/usr/src/linux-headers-2.6.15-25-386
/usr/src/linux-headers-2.6.15-25
/usr/src/linux-headers-2.6.15-25/linux-headers.revision
/usr/src/linux-headers-2.6.15-26-686
/usr/src/linux-headers-2.6.15-26
/usr/src/linux-headers-2.6.15-26/linux-headers.revision


i just installed the 686 ones.

i also added the line using name space std to the code.

and now its not giving me the warning.

i think the problem is fixed.

one thing i want to know is that how do i foce my compiler to use a specific set of headers and to ignore the others.?
Umm... these look like the headers for your kernel. Which is completely unrelated.
 
Old 07-14-2006, 05:45 AM   #13
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
well that was the thing i messed up with earlier.

and i just reinstalled the new ones and every thing worked.
 
Old 07-14-2006, 01:37 PM   #14
mhykgyver
Member
 
Registered: Aug 2004
Distribution: Gentoo, Fedora, OpenSuSE,Slackware,DSL
Posts: 38

Rep: Reputation: 16
uhmmm, Hi guys...I know this seems a bit embarrassing but after you compiled a cpp program, how do you run it?
 
Old 07-14-2006, 01:43 PM   #15
mhykgyver
Member
 
Registered: Aug 2004
Distribution: Gentoo, Fedora, OpenSuSE,Slackware,DSL
Posts: 38

Rep: Reputation: 16
ummm, my apologies...researched it in the internet and found out how it works :

./Hello

thanx again...
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in building world: warning: operand 1 missing mode? henkelarsson12 *BSD 0 04-14-2006 02:14 PM
Sample "Hello World" Xwindows C++ program dogpatch Programming 1 12-02-2005 08:55 AM
World of Warcraft Patching Error Maelstrom Linux - Games 1 03-29-2005 08:33 PM
32bit program in a 64bit program's world Jaster Debian 0 02-22-2005 12:33 PM
help for a "hello world" program lhaozheng Programming 5 04-19-2002 01:12 PM


All times are GMT -5. The time now is 03:07 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
Open Source Consulting | Domain Registration