PDA

View Full Version : Is it possible to include a C++ heade inside objective-c header?




ssawgift
Apr 8, 2012, 11:59 PM
I know I can include standard C++ header inside a .mm source file.

But now I need to include a C++ header file inside an objc header file. Is it supported to do so?



lee1210
Apr 9, 2012, 01:04 AM
If you do you need to be sure that this file is only #imported into .mm files that will be compiled as Objective-C++. #ifdef __cplusplus could be used to conditionally include C++ portions if the file may be imported in a .m.

-Lee

mydogisbox
Apr 9, 2012, 09:25 AM
If you do you need to be sure that this file is only #imported into .mm files that will be compiled as Objective-C++. #ifdef __cplusplus could be used to conditionally include C++ portions if the file may be imported in a .m.

You can also import them into C files if the header contains C++ that is compatible with C.

A blog post on how to do this:
http://www.philjordan.eu/article/strategies-for-using-c++-in-objective-c-projects

gnasher729
Apr 9, 2012, 06:09 PM
I know I can include standard C++ header inside a .mm source file.

But now I need to include a C++ header file inside an objc header file. Is it supported to do so?

The problem is not including a C++ header from an Objective-C header, the problem is including a C++ header directly or indirectly from a C or Objective-C source file. C and Objective-C source files are exactly the same in this respect.

You can't include class definitions from C. If you include function declarations without precautions, then C and Objective-C will interpret them as C functions, while C++ and Objective-C++ will interpret them as C++ functions, so they need to be declared as "extern "C"" when included from C++.

So unless the author of the header file was planning for this, it won't work.