LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ libraries and new standard (https://www.linuxquestions.org/questions/programming-9/c-libraries-and-new-standard-296192/)

blufire 03-01-2005 04:26 AM

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?

itsme86 03-01-2005 05:11 AM

You can download it from here: http://ftp.gnu.org/gnu/gcc/gcc-3.4.3/

dakensta 03-01-2005 09:50 AM

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.

blufire 03-01-2005 10:06 AM

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;
}

itsme86 03-01-2005 10:19 AM

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$


blufire 03-01-2005 10:21 AM

Thankyou you are totally right. It runs fine now.

jonaskoelker 03-01-2005 12:11 PM

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.

itsme86 03-01-2005 12:33 PM

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

jtshaw 03-01-2005 02:47 PM

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 02:45 AM.