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

    #1
    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

    lee1210

    #2
    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
     
  3. mydogisbox, Apr 9, 2012
    Last edited: Apr 9, 2012

    macrumors member

    #3
    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
     
  4. macrumors G5

    gnasher729

    #4
    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