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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-01-2005, 04:26 AM
|
#1
|
Member
Registered: Jul 2004
Location: Japan
Distribution: Fedora
Posts: 148
Rep:
|
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?
|
|
|
03-01-2005, 05:11 AM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
|
|
|
03-01-2005, 09:50 AM
|
#3
|
Member
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194
Rep:
|
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.
|
|
|
03-01-2005, 10:06 AM
|
#4
|
Member
Registered: Jul 2004
Location: Japan
Distribution: Fedora
Posts: 148
Original Poster
Rep:
|
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;
}
|
|
|
03-01-2005, 10:19 AM
|
#5
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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$
|
|
|
03-01-2005, 10:21 AM
|
#6
|
Member
Registered: Jul 2004
Location: Japan
Distribution: Fedora
Posts: 148
Original Poster
Rep:
|
Thankyou you are totally right. It runs fine now.
|
|
|
03-01-2005, 12:11 PM
|
#7
|
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524
Rep:
|
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.
|
|
|
03-01-2005, 12:33 PM
|
#8
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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
|
|
|
03-01-2005, 02:47 PM
|
#9
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 07:51 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|