• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: C++ Compiling with OSX
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ Compiling with OSX


  • Subject: Re: C++ Compiling with OSX
  • From: Timothy Knox <email@hidden>
  • Date: Wed, 14 Nov 2007 11:52:15 -0800
  • Mail-followup-to: X11 list <email@hidden>

Somewhere on Shadow Earth, at Wed, Nov 14, 2007 at 05:47:09PM +0100, Maurizio Martinelli wrote:
> Hi all,
> I am sorry to ask a question probably simple, but I can't solve this
> problem.
> I am new with C++ and I tried to run the first simple program to get
> started.
>
> The program is "Hello World":
>
> #include <iostream.h>
> // This program prints "Hello, World."
> main()
> {
>   cout << "Hello, World.\n";
>   return 0;
> }
>
> I called it primo.c, then I tried to run it:

That was your first problem. You should call it primo.cpp to help clue the
compiler in to the fact that this is a C++ source file.

> $ gcc primo.c

This is your second problem. You should use:

$ g++ primo.cpp

Your third problem will be the compiler warning you that <iostream.h> is
deprecated. To fix it, rewrite your program in one of two ways:

#include <iostream>
// This program prints "Hello, World."
main()
{
  std::cout << "Hello, World.\n";
  return 0;
}

or

#include <iostream>
using namespace std;
// This program prints "Hello, World."
main()
{
  std::cout << "Hello, World.\n";
  return 0;
}

Of the two, I prefer the first, because you are dumping less crud into your
top-level namespace, but there is much argument both ways.

Hope this helps. :-)

> primo.c:1:22: error: iostream.h: No such file or directory
> primo.c: In function 'main':
> primo.c:5: error: 'cout' undeclared (first use in this function)
> primo.c:5: error: (Each undeclared identifier is reported only once
> primo.c:5: error: for each function it appears in.)
>
> I then tried to link the library iostream.h:
>
> $ gcc -Wall -I/usr/include/gcc/darwin/3.3/c++/backward primo.c
>
> In file included from primo.c:1:
> /usr/include/gcc/darwin/3.3/c++/backward/iostream.h:32:20: error:
> iostream: No such file or directory
> In file included from primo.c:1:
> /usr/include/gcc/darwin/3.3/c++/backward/iostream.h:34: error: parse
> error before 'std'
> primo.c:4: warning: return type defaults to 'int'
> primo.c: In function 'main':
> primo.c:5: error: 'cout' undeclared (first use in this function)
> primo.c:5: error: (Each undeclared identifier is reported only once
> primo.c:5: error: for each function it appears in.)
>
> In the second output line, the compiler seems to tell me that it
> cannot find iostream.h, but if i execute the ls command iostream.h is
> there:
>
> $ ls /usr/include/gcc/darwin/3.3/c++/backward/iostream.h
> /usr/include/gcc/darwin/3.3/c++/backward/iostream.h
>
> I also tried to run primo.c with iostream.h in the same directory but
> it gave me the same output of  `$ gcc primo.c'
>
> I know that this is not exactly a X11 problem, but I hope you can
> help me too.
> I work on a iBook 12'' PPC G4 1.33GHz, Tiger 10.4.10. I have
> installed XCode 2.4.1.
> Bye
>
> Maurizio

>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> X11-users mailing list      (email@hidden)
>
> This email sent to email@hidden

--
Timothy Knox <mailto:email@hidden>
90% of everything is crud. And for software the other 10% is worse.
    -- Nicholas Clark
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
X11-users mailing list      (email@hidden)

This email sent to email@hidden

References: 
 >C++ Compiling with OSX (From: Maurizio Martinelli <email@hidden>)

  • Prev by Date: Re: Leopard X11 focus loss issue
  • Next by Date: Re: Leopard X11 focus loss issue
  • Previous by thread: Re: C++ Compiling with OSX
  • Next by thread: Re: C++ Compiling with OSX
  • Index(es):
    • Date
    • Thread