ProgrammingThis 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.
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.
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?
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)
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:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.