Compiling C code inside a C++ code inside a Objective-C++ code
-
18-06-2021 - |
문제
My problem is a little complex:
Library: I have a library written in C and C++. It compiles perfecly in XCode with Apple LLVM. The work of this library itself is done by the C code while the C++ part is just a C++ interface because I prefer C++. In theory I need to use only C++ to comunicate with it.
The application: I have an application in Objective-C that uses the library above. Of couse, as the communication with my library is done via C++ I need to have a .mm file in order to call my library from "Objective-C++".
During the compilation... several issued of "C" languages happen:
- error: non-const static data member must be initialized out of line
- error: statement expression not allowed at file scope
- etc...
It only happens with Objective-C++
- Objective-C++ (app) -> C++ headers (lib) -> C headers (lib, with extern "C") -> NOT OK!!! WHY?
If I make another C++ application for test of library, it goes ok
- C++ (app) -> C++ (lib) -> C header (lib, with extern "C") -> OK
Detail: I am always using the Apple LLVM compiler here
The question: How do I compile C code from C++ from Objective-C++ code? Why it's different than compile from a regular C++ code?
해결책 2
After a long period of tries and Google I finally could figure out. Before I call my C headers, I should undefine MAX and MIN macros. For some reason they were previous defined. The compiler doesn't show the error with precision. That's why it was difficult. The C framework I am using has it's own declaration of MAX and MIN macros...
Another very interesting thing that might be useful for others is that before we need to do the same thing: undefine max and min (lower case) if we are using some C framework that implements it's own max and min.
It solved both problems.
다른 팁
Don't forget to do
#ifdef __cplusplus
extern "C" {
#endif
<Some C method declaration>
#ifdef __cplusplus
}
#endif
in your header files that contain C method declarations.