Re: very basic gcc question



As Anthony said,  using the C++ compiler instead of the C compiler fixes
the problem.  Brian,  it would be better for you to use 'endl' instead of
'\n' to print a newline.  Using the endl manipulator flushes the stream,
otherwise you may not get the output.

ex:

cout << "Hello World." << endl ;



Deviation in discussion:

However,  looking at the gcc web site and man pages, it appears that 'gcc'
no longer stands for GNU C Compiler, but instead stands for GNU Compiler
Collection.  gcc also accepts an argument (-x) to specify the language to
use.  Here are some samples using Brian's code:

$ gcc -x c++ hello.cc 
/tmp/ccq8aI9Q.o: In function `main':
/tmp/ccq8aI9Q.o(.text+0x17): undefined reference to `cout'
/tmp/ccq8aI9Q.o(.text+0x1c): undefined reference to
`ostream::operator<<(char const *)'
/tmp/ccq8aI9Q.o(.text+0x2a): undefined reference to
`ostream::operator<<(ostream &(*)(ostream &))'
collect2: ld returned 1 exit status

$ g++ -x c++ hello.cc 

$ g++ -x c hello.cc 
hello.cc:2:22: iostream.h: No such file or directory

Similarly, if I write the corresponding Hello World in java:
$ gcj --main=Hello Hello.java 

$ gcj -xjava --main=Hello Hello.java 

$ gcc -xjava --main=Hello Hello.java 
jc1: Unrecognized option `-fmain=Hello'

$ gcc -xjava -C Hello.java  
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o: In function
`_start':
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.text+0x18):
undefined reference to `main'
/tmp/ccQ2q5v6.o: In function `Hello::Hello(void)':
/tmp/ccQ2q5v6.o(.text+0xe): undefined reference to
`java::lang::Object::Object(void)'
 [other link errors snipped]

$ gcc -xjava -c Hello.java 

[ this gave no errors,  however a C/C++ .o object file was created, rather
than a .class file ]



It seems that the -x option tells gcc which parser to use, but it has no
effect on the remainder of available options or the linker.

What is the intended purpose of redefining "GCC" and the -x option?  Am I
using it in the wrong way?

Thanks,

-D



On Tue, 07 Nov 2000 23:45:57 Antony Stace wrote:
 | try using "g++" instead of "gcc"
 | 
 | "Brian J. Rohan" wrote:
 | > 
 | >   I am new to C++, and realize this may not be the best group to post
 | > C++ questions in (what group would be best?).  I have tried to compile
a
 | > very basic C++ program as follows:
 | > 
 | > #include <iostream.h>
 | > 
 | > int main()
 | > {
 | >   cout <<"Hello World!\n";
 | >   return 0;
 | > }
 | > 
 | > I then go to the directory contaiing the .cc file above, and type gcc
 | > ch1proj1.cc (the name of the file)
 | > I then get the following error messages:
 | > 
 | > /tmp/cc2y1g3w.o: IN function `main':
 | > /tmp/cc2y1g3w.o(.text+0xf): undefined reference to `cout'
 | > /tmp/cc2y1g3w.0(.text+0xf): undefined reference to
 | > `ostream::operator<<(char const *)'
 | > collect2: ld returned 1 exit status
 | > 
 | 
 | > What is wrong?? i am using RedHat7.0, I recently upgraded form 6.2
 | > 
 | > Thank you very much
 | > 
 | > Brian
 | > 





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]