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 03-01-2005, 04:26 AM   #1
blufire
Member
 
Registered: Jul 2004
Location: Japan
Distribution: Fedora
Posts: 148

Rep: Reputation: 16
C++ libraries and new standard


ok I can write my programs and use the older versions <iostream.h> and stuff and my programs will compile just fine.

Problem.
I started studying a new tutorial where the writer uses <iostream> and using nameplace std; and other headers starting with prefix c and no longer using the suffix .h

I tried it and I can't get this new style to compile.

How can I update the gcc to be able to use the new standard?
 
Old 03-01-2005, 05:11 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can download it from here: http://ftp.gnu.org/gnu/gcc/gcc-3.4.3/
 
Old 03-01-2005, 09:50 AM   #3
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Can you also post a minimal sample that fails to compile and the current version of your compiler (g++ -v)

Fedora Core 2 is not that old so should support the bulk of modern c++. Source code can sometimes require a substantial number of changes.
 
Old 03-01-2005, 10:06 AM   #4
blufire
Member
 
Registered: Jul 2004
Location: Japan
Distribution: Fedora
Posts: 148

Original Poster
Rep: Reputation: 16
ok here is my version and the file that is failing.

gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)


the failure

gcc hello.c hello2
gcc: hello2: No such file or directory
hello.c:1:20: iostream: No such file or directory
hello.c:2: error: syntax error before "namespace"
hello.c:2: warning: data definition has no type or storage class
hello.c: In function `main':
hello.c:6: error: `cout' undeclared (first use in this function)
hello.c:6: error: (Each undeclared identifier is reported only once
hello.c:6: error: for each function it appears in.)
hello.c:6: error: `endl' undeclared (first use in this function)
hello.c:8: error: `cin' undeclared (first use in this function)


the actual file

#include <iostream>
using namespace std;

int main()
{
cout << "Hello, world" << endl;

cin.get();
return 0;
}
 
Old 03-01-2005, 10:19 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
That's because you're using gcc to compile C++ programs instead g++. Also, you need to name C++ programs with .cpp instead of .c. I'm using gcc-2.95.3 and your program works fine for me since I compiled it correctly:
Code:
itsme@dreams:~/CPlusPlus$ g++ --version
2.95.3
itsme@dreams:~/CPlusPlus$ g++ hello.cpp -o hello
itsme@dreams:~/CPlusPlus$ ./hello
Hello, world

itsme@dreams:~/CPlusPlus$
 
Old 03-01-2005, 10:21 AM   #6
blufire
Member
 
Registered: Jul 2004
Location: Japan
Distribution: Fedora
Posts: 148

Original Poster
Rep: Reputation: 16
Thankyou you are totally right. It runs fine now.
 
Old 03-01-2005, 12:11 PM   #7
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
on file naming: does that matter? I've never seen gcc complain about the files being .c instead of <insert your favorite extension here>

However, for header files it does matter in one place: emacs will use c++-mode for .hh-files, but c-mode for .h-files; for symmetry i name my files .cc, but (afaik) that's just a matter of taste.
 
Old 03-01-2005, 12:33 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
gcc really does care about the file extension. Here's a list of file extensions that gcc recognizes and what it recognizes them as: http://www.delorie.com/djgpp/v2faq/faq8_5.html
 
Old 03-01-2005, 02:47 PM   #9
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
gcc does care about the file extension.... however, even with a correct file extension "gcc -o hello hello.cpp" is probably going to fail with linker errors.

You have two options.

"gcc -o hello hello.cpp -lstdc++"
or
"g++ -o hello hello.cpp"

When you envoke g++ it links in libstdc++ by default. Regardless of the file extentions, gcc does not. At least that is the case with the couple different versions I am currently running.

So, to summarize. When coding in c++ use .C, .cpp, .cxx, or .cc as your file extension and either make usre you link in libstdc++ or use g++ instead of gcc to do the compiling.
 
  


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
Use gcc and standard libraries with SUSE 9.1 Personal warrenpatrick Linux - Newbie 3 10-10-2004 07:26 PM
C++ standard library. Thetargos Programming 8 10-09-2003 03:01 PM
rh 8.0 standard kernel 2.5.74 Hybrid Blue Linux - Newbie 1 09-30-2003 10:19 PM
standard vs Expert install? Standard flubs up! Frost Linux - Software 1 03-27-2002 07:55 AM
Mandrake Standard 8.1 Karen Linux - Software 2 03-02-2002 04:28 PM

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

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