0

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?

Wagner Patriota
  • 5,494
  • 26
  • 49
  • 3
    I have seen very little that won't work but there are things that are different. For instance you can't have anything named `id` or `SEL` in your code because that would resolve to something special in Objective-C++. Can you give more specifics (e.g. the exact contents of the line that triggers the "non-const static data member" error)? – Kevin Grant Jul 03 '12 at 22:11
  • Have you tried to set the file type of the library to C++ source file? I had problems before because of this. You nee to select that file in XCode , go to the File Inspector (alt + cmd + 1) and select the file type. – George Jul 03 '12 at 22:41
  • George, the File inspector recognizes my file .mm as Objective-C++... so it's right. Thank you – Wagner Patriota Jul 03 '12 at 22:51
  • Kevin, the line: `typedef struct __tag_LIGHT_CONTEXT { char data[__CB_DECL( LIGHT_CONTEXT_LEN )]; } LIGHT_CONTEXT; throws the error... being #define __CB_DECL(x) (x) and #define LIGHT_CONTEXT_LEN MAX( LEFT_LIGHT_LEN, RIGHT_LIGHT_LEN )` – Wagner Patriota Jul 03 '12 at 23:48
  • 1
    I'd bet the `MAX` is the problem. Objective-C/Cocoa defines this macro (`NSObjCRuntime.h` in `Foundation.framework`). – Kevin Grant Jul 04 '12 at 04:01

2 Answers2

2

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.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

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.

Wagner Patriota
  • 5,494
  • 26
  • 49