2

I have an Xcode project for iOS in Xcode 7.0 that contains Objective-C, C++, and C code. Xcode tries to compile the C++ header files with the extension .h (not .hpp) as C header files which generates errors.

How do I fix this? There should be a setting for which files should be compiled as which language.

18
  • 1
    You can change the type of an individual file under the "File inspector". Commented Sep 28, 2015 at 16:17
  • 1
    Changing them to .hpp should also work. Commented Sep 28, 2015 at 16:22
  • 5
    Headers aren't compiled in isolation. How they're interpreted depends on where you include them. Commented Sep 28, 2015 at 16:29
  • 1
    @jBot-42 After changing to .hpp did you check the file inspector. It should say that the type is "Default - C++ Header". Also, since you've been playing around with the build settings, you might want to start a new project, so that all the build settings are back to their defaults. Commented Sep 28, 2015 at 16:37
  • 1
    Does XCode have a facility for building and relying upon "precompiled headers"? If so, try turning it off. Especially do so if any of the same headers are included both in C source files and in C++ source files. Otherwise, I think we've exhausted the alternatives we can suggest without you presenting example code with which the failure can be reproduced, and the actual error messages that lead you to conclude that files are being compiled for the wrong language. Commented Sep 28, 2015 at 17:33

1 Answer 1

4

Individually header files doesn't compiled. Compiled source files where they included. If you include them in .c they will be compiled like C, if in .m - like Objective-c, if in .cpp - like C++, if in .mm - like Objective-C++.

It doesn't matter how you name them .h or .hpp.

If you have some portion of C++ code in h-file- you should put it in

#ifdef __cplusplus
#endif

If you have some portion of Objective-C code in h-file you should put it in

#ifdef __OBJC__
#endif

This will make code inside this statement invisible for other languages.

You can find this statement in Xcode's generated pch-file.

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't answer the question. The asker asked how to force compiling header file with specified language instead of how to port codes between different situations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.