4

I'm trying to compile the simplest program on MacOS 10.6 like:

$ g++ -o hello hello.cpp

the following source:

#include <iostream>

int main (int argc, char * const argv[]) {
    std::cout << "Hello, World!\n";
    return 0;
}

I'm getting the error:

hello.cpp:1:20: error: iostream: No such file or directory
hello.cpp: In function ‘int main(int, char* const*)’:
hello.cpp:4: error: ‘cout’ is not a member of ‘std’

So obviously I have to add the include path somewhere. My question is where can I find the include directories and how can add them globally (I don't want to provide the include path whenever I want to compile).

I just installed the XCode 3.1.4 and managed to compile it via Xcode, but not via command line. I found some header files in this directory:

/Xcode3.1.4/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers

and tried to add it to the HEADER_SEARCH_PATHS after reading this question, but no luck.

I'm developing on Linux and everything is working fine there, but I want to continue doing that on MacOS. Any help?

Community
  • 1
  • 1
Lipis
  • 21,388
  • 20
  • 94
  • 121
  • This works as expected on my system, no problems finding the file to include. I don't think I have anything special installed other than the regular developer tools package... – Asher Dunn Jan 22 '10 at 01:12
  • well.. I just realized that I downloaded the version 3.1.4 of Xcode, but today I found that there is a 3.2.1 out there.. and I'm pretty sure that entered the same query on Google :) So let's see if that will help... – Lipis Jan 22 '10 at 01:25

2 Answers2

2

On my Mac, that include file is in /usr/include/c++/4.0.0/iostream . Are you sure you have all the command-line development tools installed? They might not be by default; I'm pretty sure I had to install it manually when I first set up my Mac. There should be a "developer tools" package somewhere on your OS X installation media.

Or, if you want to make sure you're getting the latest version, you can download it from: http://developer.apple.com/technology/xcode.html

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • I have only ppc_intrinsics.h and stdint.h under my /usr/include/gcc/darwin/4.2 folder. – Lipis Jan 22 '10 at 01:00
  • Same here (except I have 4.0), but g++ works for me. Do you have a /usr/include/c++ directory? – Jim Lewis Jan 22 '10 at 01:06
  • Sounds like it's not fully installed, then. There should be a "developer tools" package in your OS X installation media, maybe give that a try? I'm pretty sure I had to do this manually when I set up my Mac. – Jim Lewis Jan 22 '10 at 01:14
  • Just edit your answer and tell me to update to the latest version from here http://developer.apple.com/technology/xcode.html and I'll accept this answer :D – Lipis Jan 22 '10 at 01:53
-4
$ g++ -o example.bin example.cpp //to compile
$ ./example.bin //to run

It's code:

#include <iostream>
using namespace std;
int main () {
    cout << "Hello, World!\n";
    return 0;
}
thkala
  • 84,049
  • 23
  • 157
  • 201