0

Referred Question: Problem when #import C++ Header File in iPhone/iPad Project

Well, my project is relatively huge and cannot solve that either changing ALL or PART OF my project files .m to .mm or changing the ALL or PART OF file types in File Inspector.

That is because both my colleagues and library authors DOES NOT follow the C++ standard strictly, such as assigning void* to int* and other various violations. Objective C would allow these and just gives warnings.

The C++ header file I want to #import is a library. It uses keyword namespace in its header, while its implementation is in an .a assembly file. That means it is nearly impossible to hack them. Besides that, my project also includes other libraries that are compatible only with Objective C.

Does anyone know how to solve this?

The ways I could imagine is as follows:

  1. Find an alternative for namespace, but still I want to write codes like QCAR::Renderer in my project.

  2. Tell the compiler to recognized C++ header(Well, that might not be possible)

EDIT

#ifdef __cplusplus
# include MyCPPHeader.h
#endif

If I use that, would MyCPPHeader.h really get included in an Objective-C environment? I guess not. And that's against the principles of not hacking libraries.

EDIT

Even I changed these .mm files to include that C++ Header, i would get an link error saying Undefined symbols for architecture armv7:. This happens when my .mm files including .h headers in other libraries.

Community
  • 1
  • 1
Chao Zhang
  • 1,476
  • 2
  • 14
  • 25
  • Naming the source files as `.mm` should work without issue (the Mac app I am developing uses a large C++ library and most of my app implementation files use the `.mm` file extension). Perhaps you are including the C++ headers in a `.m` file (either directly or indirectly) by mistake? – trojanfoe Jan 31 '13 at 09:41
  • How does your problem manifest itself? as a compiler error, if yes, which? as a linker error, if yes, which? as a runtime error, if yes, which? – PlasmaHH Jan 31 '13 at 09:48
  • Because I am not the author of those `.m` files. If I change `.m` suffix to `.mm`, I would modify their codes. – Chao Zhang Jan 31 '13 at 09:51
  • are you writing the c++library or the project including the c++library? You have to be able to change at least one part to get help. – Jonathan Cichon Jan 31 '13 at 09:52
  • @ComboZhc you do not have to change the c++library file suffix to `.mm`, you have to change the suffix of all your project files which import the `MyCPPHeader.h` to `.mm`. You can not import the `MyCPPHeader.h` in other header files which are imported in normal `.m` files. – Jonathan Cichon Jan 31 '13 at 10:02
  • @ComboZhc This question is getting somewhat confusing. Please be more specific than "those `.m` files". If you follow the principle that `.m` files should never see C++ then your problem will be solved. I am voting to close as "too localized" as I cannot see this issue ever being solved. – trojanfoe Jan 31 '13 at 10:02
  • @JonathanCichon as I said. I cannot change all my files to `.mm`. Because they are hundreds of them and they cannot get compiled correctly under C++ standard – Chao Zhang Jan 31 '13 at 10:03
  • @ComboZhc you have to change only the files that use the c++Library. If ALL Files use the Library, you will have to change all. If this is to much work, you could wrapp the functionality of the lib in one single objective-c class with a `.mm` file and import the header of this class in the other files in your project. – Jonathan Cichon Jan 31 '13 at 10:08
  • @JonathanCichon even if I changed the `.mm` files that uses the C++ Library, it would raises an link error. – Chao Zhang Jan 31 '13 at 10:12
  • @ComboZhc you have to add the link error to your question. – Jonathan Cichon Jan 31 '13 at 10:16
  • 1
    @ComboZhc seams like your problem is not c++ after all. Looks like the lib is not compiled with armv7. – Jonathan Cichon Jan 31 '13 at 10:32
  • @JonathanCichon which library do you refer to. is it the library that triggered link error? – Chao Zhang Jan 31 '13 at 10:38
  • @ComboZhc what symbol is not defined? – Jonathan Cichon Jan 31 '13 at 10:49
  • @JonathanCichon It is trivial. I could assume that every reference from MY codes to the functions of OTHER libraries would raise a link error. I changed a file which is referenced in OTHER library to .mm and this reference are freed from link error. – Chao Zhang Jan 31 '13 at 11:05

1 Answers1

0

I have solved this a year ago, but for other people who are looking for an answer, here is the solution.

Say the C++ style header is named cpp.h. In my project, I never #import "cpp.h". I wrote a C style header, named c.h, as a wrapper header for cpp.h. Then I wrote a C++ implementation source file, named c.mm to implement the function define in c.h by calling functions in cpp.h. If I want to call functions in cpp.h, I #import c.h and use the wrapper function instead.

Below is the explanation in a possibly clearer way:

cpp.h C++ style header file using the namespace, its implementation is in assembly code so it is hard to change

c.h C style header as the wrapper for cpp.h

c.mm C++ implementation implements functions in c.h by calling functions in cpp.h

#import "c.h" to use the wrapper functions

Chao Zhang
  • 1,476
  • 2
  • 14
  • 25
  • Hey could you elaborate on how to create C style header as a wrapper for cpp.h files? I have not worked on cpp, so this concept is fairly new to me. Any example would help me a lot. Thanks. – Ananth Kamath Feb 21 '23 at 06:19