LinuxQuestions.org
Help answer threads with 0 replies.
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 01-12-2012, 06:10 AM   #1
gaurav.rustagi
LQ Newbie
 
Registered: Jan 2012
Location: Mumbai, India
Distribution: Red Hat
Posts: 23

Rep: Reputation: Disabled
g++ compiler gives error in a simple hello world program


//Program snippet:

#include <iostream>

using namespace std;

int
main (int argc, char *argv[])
{
cout << "Hello world" << endl;
return 0;
}

Filename : test.cpp
Compilation string:
g++ -o test -Wall -pedantic test.cpp

Compilation Output:
test.cpp:1:20: iostream: No such file or directory
test.cpp: In function `int main(int, char**)':
test.cpp:8: error: `cout' was not declared in this scope
test.cpp:8: error: `endl' was not declared in this scope
test.cpp:8: warning: unused variable 'cout'
test.cpp:8: warning: unused variable 'endl'
w

gcc version:Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)

Observations:
the above mentioned program works fine with gcc version 4.1.2.

Why does it not get compiled with gcc 3.4.6 ? Any help is highly appreciated.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-12-2012, 06:30 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
It looks like the C++ standard library for 3.4.6 isn't installed. How did you get this version of g++ - have you compiled it yourself, or is it from RPM?

Also, could you run g++ with the '-v' option and post the section that starts with '#include "..." search starts here:' and ends with 'End of search list'.
 
2 members found this post helpful.
Old 01-12-2012, 06:40 AM   #3
gaurav.rustagi
LQ Newbie
 
Registered: Jan 2012
Location: Mumbai, India
Distribution: Red Hat
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thanks for replying.
I have not installed gcc myself. But i would like to find out the cause behind this.
I ran the program again as you suggested. Please find the below output.

g++ -v -Wall -pedantic test.cpp
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)
/usr/libexec/gcc/i386-redhat-linux/3.4.6/cc1plus -quiet -v -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -auxbase test -Wall -pedantic -version -o /tmp/ccZrqH7Y.s
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6"
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/i386-redhat-linux"
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/backward"
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc/i386-redhat-linux/3.4.6/include
/usr/include
End of search list.
GNU C++ version 3.4.6 20060404 (Red Hat 3.4.6-3) (i386-redhat-linux)
compiled by GNU C version 3.4.6 20060404 (Red Hat 3.4.6-3).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
test.cpp:1:20: iostream: No such file or directory
test.cpp: In function `int main(int, char**)':
test.cpp:8: error: `cout' was not declared in this scope
test.cpp:8: error: `endl' was not declared in this scope
test.cpp:8: warning: unused variable 'cout'
test.cpp:8: warning: unused variable 'endl'
 
Old 01-12-2012, 07:08 AM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
With a complete EL5 gcc34 / g++34 : compat-gcc-34-c++-3.4.6-4.1 /
compat-gcc-34-3.4.6-4.1 / compat-libstdc++-33-3.2.3

1) g++34 -v -Wall -pedantic test.cc -o hello
2) ./hello : No errors, replies : Hello world


Same conclusion as @JohnGraham : Install the right libstdc++.
Which is automatically installed by : # yum install compat-gcc-34-c++

.

Last edited by knudfl; 01-12-2012 at 07:10 AM.
 
1 members found this post helpful.
Old 01-12-2012, 07:22 AM   #5
gaurav.rustagi
LQ Newbie
 
Registered: Jan 2012
Location: Mumbai, India
Distribution: Red Hat
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thanks for your help. I am trying to understand what is happening here. I agree that C++ standard library for 3.4.6 isn't installed. I have checked it manually by going to that directory. There is no directory 3.4.6 in c++ folder.

What i have done now is that i compiled the above program on a server where C++ is installed and it works. However, when i move the binary ( or execuatable ) to this server ( where 3.4.6 is not installed ), and run it, it fails with "Floating point exception".

Is the program compiled with higher version of gcc(4.1.1), does not work with older version of gcc(3.4.6 [C++ std library is not installed] ) ?

Any light on this matter ?
 
Old 01-12-2012, 08:45 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
These problems might be unrelated -- try and debug to floating point exception with gdb
 
Old 01-14-2012, 01:14 AM   #7
jtso8
Member
 
Registered: Apr 2011
Posts: 38

Rep: Reputation: 0
Try installing build essentials.
 
  


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
error in creating rpm package for a simple "Hello World" c program jayasekar Linux - Newbie 8 12-02-2009 12:53 AM
Error in Creating rpm package for a simple "Hello World" Program jayasekar Linux - Software 2 12-01-2009 08:26 AM
Trouble compiling simple hello world program knightstreet Linux - Newbie 6 10-19-2009 07:03 PM
cannot compile simple hello world C++ program in Emacs Rafael.Pascual Linux - Newbie 1 05-30-2009 01:02 PM

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

All times are GMT -5. The time now is 12:46 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