1

I've made a struct which does cached file manipulation for my application. I built and tested to in a separate project before putting it into my current one.

Ever since I've moved it over, Xcode refuses to build it. Except when I don't include the file from any Objective-C based header file.

I get one error when I try to include iostream: enter image description here

And more when I comment it out: enter image description here

Its file extension is .mm, however I have tried it with .cpp and .hpp, but all of them refuse to build unless I don't #include it from the Objective-C header file.

I've also tried #import from iostream and the file itself in the Objective-C header file.

Any clues as to why this is happening?

Jack Greenhill
  • 10,240
  • 12
  • 38
  • 70
  • 1
    Side note: Since you're using C++, get rid of the C-ism `typedef struct {...} mytype;` and replace it with `struct mytype {...};`. Also, I see way to many naked pointers. Modern resource management looks different in C++. – sellibitze Dec 12 '11 at 13:12

3 Answers3

1

Take a look here and here. You need to tell the compiler to include libstdc++. When mixing Objective-C and C++ all you're files need to have the ".mm" extension, as stated in the second link.

Community
  • 1
  • 1
Sebastian
  • 8,046
  • 2
  • 34
  • 58
1

As a matter of principle, you cannot include a C++ header file from an objective-C source file.

After all, #including (or #importing) a file only means that the preprocessor replaces the #include directive by the contents of the #included file, before passing the result on to the "actual" compiler. The file extension of the header file is a matter of convention, only, it has no actual meaning.

The error messages your are seeing are clearly the result of the file being compiled as [Objective-]C rather than [Objective-]C++.

Solution: All the source files that include your C++ header file have to be either C++ (.cpp or .cc or a few other extensions) or Objective-C++ (.mm). All source files that include a header file that includes your C++ header file, also have to be C++ or Objective-C++.

EDIT: I just saw that you are defining non-inline, non-template functions in your C++ file that you want to include. This is an unrelated problem, but it will lead to "multiple definition" errors sooner or later. Those function definitions belong in a .cpp, which shouldn't get #included anywhere, only the struct/class definition belongs in a header.

wolfgang
  • 4,883
  • 22
  • 27
  • Thanks for your comments, and changed all of the file extensions to Objective-C++ and made the functions inline. It works perfectly now. – Jack Greenhill Dec 12 '11 at 13:48
0

I suspect the error is occurring when you compile a .m or .c file that includes the same header.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365