public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* compiler problems pt2
@ 2009-01-02 17:02 robert watson
  2009-01-02 19:27 ` me22
  2009-01-02 20:03 ` Tim Prince
  0 siblings, 2 replies; 4+ messages in thread
From: robert watson @ 2009-01-02 17:02 UTC (permalink / raw)
  To: gcc-help

Hi Gnu folks,

Thanks for the replies. Compiling my code using: 

cd Desktop
g++ /home/robert/Desktop/test.c  # uses c++ compiler 

Instead of:

cd Desktop
gcc /home/robert/Desktop/test.c  # uses c++ compiler 

Produced no errors, and an output file called 'a.out', so I guess that means g++ keeps tabs on the libraries better. 

I would never have tried that myself, so some sort of explanation for all this stuff in the manual would be really useful to others. After all 'HelloWorld' programs are supposed to be easy!!

BTW How do I run the code? Double clicking doesn't work. I mean it's like clicking the desktop - absolutely nothing happens.

Thanks again,

Rob

My code:

/* my second program in C++
   with more comments */

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  cout << "Hello World! ";     // prints Hello World!
  cout << "I'm a C++ program"; // prints I'm a C++ program
  return 0;
}

The errors I got with g++:

robert@robert-laptop:~$ gcc /home/robert/Desktop/test.c # uses c++ compiler 
/home/robert/Desktop/test.c:4:20: error: iostream: No such file or directory 
/home/robert/Desktop/test.c:5:18: error: string: No such file or directory 
/home/robert/Desktop/test.c:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘namespace’ 
/home/robert/Desktop/test.c: In function ‘main’: 
/home/robert/Desktop/test.c:10: error: ‘cout’ undeclared (first use in this function) 
/home/robert/Desktop/test.c:10: error: (Each undeclared identifier is reported only once 
/home/robert/Desktop/test.c:10: error: for each function it appears in.) 





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: compiler problems pt2
  2009-01-02 17:02 compiler problems pt2 robert watson
@ 2009-01-02 19:27 ` me22
  2009-01-02 19:39   ` Andreas Tobler
  2009-01-02 20:03 ` Tim Prince
  1 sibling, 1 reply; 4+ messages in thread
From: me22 @ 2009-01-02 19:27 UTC (permalink / raw)
  To: reswatson; +Cc: gcc-help

On Fri, Jan 2, 2009 at 12:01, robert watson <reswatson@yahoo.com> wrote:
>
> Thanks for the replies. Compiling my code using:
>
> cd Desktop
> g++ /home/robert/Desktop/test.c  # uses c++ compiler
>
> Instead of:
>
> cd Desktop
> gcc /home/robert/Desktop/test.c  # uses c++ compiler
>
> Produced no errors, and an output file called 'a.out', so I guess that means g++ keeps tabs on the libraries better.
>

g++ links to the C++ library; gcc does not.

I think you problem might be that you have C++ code in a .c file.
(IIRC, gcc/g++ chooses the frontend based on the file extension.)  Try
.cpp .cc .C or .cxx instead.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: compiler problems pt2
  2009-01-02 19:27 ` me22
@ 2009-01-02 19:39   ` Andreas Tobler
  0 siblings, 0 replies; 4+ messages in thread
From: Andreas Tobler @ 2009-01-02 19:39 UTC (permalink / raw)
  To: me22, reswatson; +Cc: gcc-help

me22 wrote:
> On Fri, Jan 2, 2009 at 12:01, robert watson <reswatson@yahoo.com> wrote:
>> Thanks for the replies. Compiling my code using:
>>
>> cd Desktop
>> g++ /home/robert/Desktop/test.c  # uses c++ compiler
>>
>> Instead of:
>>
>> cd Desktop
>> gcc /home/robert/Desktop/test.c  # uses c++ compiler
>>
>> Produced no errors, and an output file called 'a.out', so I guess that means g++ keeps tabs on the libraries better.
>>
> 
> g++ links to the C++ library; gcc does not.
> 
> I think you problem might be that you have C++ code in a .c file.
> (IIRC, gcc/g++ chooses the frontend based on the file extension.)  Try
> .cpp .cc .C or .cxx instead.

doesn't matter, at least for c vs. C, cpp:

[wolfram:~] andreast% cat hello2.c
#include <iostream>
#include <string>
using namespace std;

int main ()
{
   cout << "Hello World!\n";     // prints Hello World!
   cout << "I'm a C++ program\n"; // prints I'm a C++ program
   return 0;
}

[wolfram:~] andreast% g++ hello2.c
[wolfram:~] andreast% ./a.out
Hello World!
I'm a C++ program

better would be:

[wolfram:~] andreast% g++ -Wextra -Wall -o hello2 hello2.c
[wolfram:~] andreast% ./hello2
Hello World!
I'm a C++ program

Andreas


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: compiler problems pt2
  2009-01-02 17:02 compiler problems pt2 robert watson
  2009-01-02 19:27 ` me22
@ 2009-01-02 20:03 ` Tim Prince
  1 sibling, 0 replies; 4+ messages in thread
From: Tim Prince @ 2009-01-02 20:03 UTC (permalink / raw)
  To: reswatson; +Cc: gcc-help

robert watson wrote:

> 
> I would never have tried that myself, so some sort of explanation for all this stuff in the manual would be really useful to others. After all 'HelloWorld' programs are supposed to be easy!!

Nowhere in 'info gcc' or 'info g++' nor in the on-line manuals?
> BTW How do I run the code? Double clicking doesn't work. I mean it's like clicking the desktop - absolutely nothing happens.
> 

If you want to learn more about your OS and desktop utility (whatever it
may be), this isn't the best place.  Under sh family command lines, the
usually way is to type ./a.out  None of those are part of gcc.

> 
> robert@robert-laptop:~$ gcc /home/robert/Desktop/test.c # uses c++ compiler 
No, both gcc and the .c extension ask for a C compiler.
> /home/robert/Desktop/test.c:4:20: error: iostream: No such file or directory 
> /home/robert/Desktop/test.c:5:18: error: string: No such file or directory 
> /home/robert/Desktop/test.c:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘namespace’ 
> /home/robert/Desktop/test.c: In function ‘main’: 
> /home/robert/Desktop/test.c:10: error: ‘cout’ undeclared (first use in this function) 

All of those messages should be clues that you told the compiler you had a
C program, not implementing C++ headers.
It's not possible to make a combination C and C++ compiler without C and
C++ modes.  Even MSVC has a C89 mode, (default for .c source files), where
C++ name mangling is turned off, along with command line options to accept
C++ in a .C source file. C99 is a useful programming language for many
people; nearly all compilers (excepting Microsoft) support a useful part
of it.

Yahoo mail (accessed via tbird) puts you in the Spam box. Just in case you
may be interested.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-01-02 20:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-02 17:02 compiler problems pt2 robert watson
2009-01-02 19:27 ` me22
2009-01-02 19:39   ` Andreas Tobler
2009-01-02 20:03 ` Tim Prince

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).