0

TestViewController.h/TestViewController.mm

HelloWorld.h/HelloWorld.cpp

If I include "HelloWorld.h" into TestViewController.mm it compiles well. When I include "HelloWorld.h" into TestViewController.h it prompts an error: 'iostream' file not found.

My HelloWorld.h code is a simple standard cpp file.

#ifndef __MixedCppTest__HelloWorld__
#define __MixedCppTest__HelloWorld__

#include <iostream>
#include <vector>

class HelloWorld {


public:
    HelloWorld();
    ~HelloWorld();
};
#endif /* defined(__MixedCppTest__HelloWorld__) */
Robert Karl
  • 7,598
  • 6
  • 38
  • 61
Nicholas Xu
  • 43
  • 1
  • 5

3 Answers3

1

you can use macros

#ifdef __cplus
 // TODO Code
#endif

Reference: Link

Community
  • 1
  • 1
abmussani
  • 451
  • 2
  • 10
0

When I include "HelloWorld.h" into TestViewController.h it prompt error: 'iostream' file not found….

Is there any Objective-C files which imports TestViewController.h? At the stage of preprocessing header file becomes a part(along with the source file) of a translation unit, and if you're asking to include such header (with c++ libraries inclusion and c++ class definition) to Objective-C source file - then no, it's impossible. If you searching a way to compile and link .cpp and .m source files to one binary, you could make .mm wrapper unit to wire them together.

bigblackdot
  • 138
  • 1
  • 9
  • The key is if you import cpp file indirectly, you should change all files to objective-c++ compiled mode on the import chain. My problem is AppDelegate.m is not be changed to .mm – Nicholas Xu May 07 '14 at 09:48
  • Are you trying to solve any specific problem or just making a reconnaissance? In the second case it's easy to set yourself an unsolvable task which just do not have to be solved at all. – bigblackdot May 07 '14 at 10:34
0

Thanks https://stackoverflow.com/users/635608/mat 's answer.

It has only one standard solution, that's make compiler knows about specified sources should be compiled as objective-c++.

There are only 3 ways:

  1. change .m to .mm

  2. change Type to Objective-c++ in Utilities panel in xcode editor.

  3. change "Compile sources as" to objective-c++

If you import cpp file indirectly, you should change all files to objective-c++ compiled mode on the import chain.

Community
  • 1
  • 1
Nicholas Xu
  • 43
  • 1
  • 5
  • 1
    So your solution is to compile all your Objective-C files as Objective-C++? That is a poor solution and better solutions are available. – trojanfoe May 07 '14 at 09:43