Is it possible to include a C++ heade inside objective-c header?

Discussion in 'Mac Programming' started by ssawgift, Apr 8, 2012.

  1. macrumors newbie

    Mar 23, 2012
    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?
  2. macrumors 68040


    Jan 10, 2005
    Dallas, TX
    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.

  3. mydogisbox, Apr 9, 2012
    Last edited: Apr 9, 2012

    macrumors member

    Jan 16, 2011
    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:
  4. macrumors G5


    Nov 25, 2005
    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.

Share This Page