Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing Objective-C and C++

I'm trying to mix Objective-C with C++. When I compile the code, I get several errors.

A.h

#import <Cocoa/Cocoa.h> #include "B.h"  @interface A : NSView {     B *b; }  -(void) setB: (B *) theB;  @end 

A.m

#import "A.h"  @implementation A  - (id)initWithFrame:(NSRect)frame {     self = [super initWithFrame:frame];     if (self) {         // Initialization code here.     }     return self; }  - (void)drawRect:(NSRect)dirtyRect {     // Drawing code here. }  -(void) setB: (B *) theB {     b = theB; }  @end 

B.h

#include <iostream>  class B {      B() {         std::cout << "Hello from C++";     }  }; 

Here are the errors:

/Users/helixed/Desktop/Example/B.h:1:0 /Users/helixed/Desktop/Example/B.h:1:20: error: iostream: No such file or directory /Users/helixed/Desktop/Example/B.h:3:0 /Users/helixed/Desktop/Example/B.h:3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'B' /Users/helixed/Desktop/Example/A.h:5:0 /Users/helixed/Desktop/Example/A.h:5: error: expected specifier-qualifier-list before 'B' /Users/helixed/Desktop/Example/A.h:8:0 /Users/helixed/Desktop/Example/A.h:8: error: expected ')' before 'B' /Users/helixed/Desktop/Example/A.m:26:0 /Users/helixed/Desktop/Example/A.m:26: error: expected ')' before 'B' /Users/helixed/Desktop/Example/A.m:27:0 /Users/helixed/Desktop/Example/A.m:27: error: 'b' undeclared (first use in this function) 
like image 897
LandonSchropp Avatar asked Apr 26 '10 00:04

LandonSchropp


People also ask

Can I use C in Objective-C?

You really can't use C in Objective-C, since Objective-C is C. The term is usually applied when you write code that uses C structures and calls C functions directly, instead of using Objective-C objects and messages.

Is Objective-C as fast as C?

Objective-C is slightly slower than straight C function calls because of the lookups involved in its dynamic nature.

Can you mix C and C++ code?

The C++ language provides mechanisms for mixing code that is compiled by compatible C and C++ compilers in the same program. You can experience varying degrees of success as you port such code to different platforms and compilers.

Is Objective-C and C++ same?

While they are both rooted in C, they are two completely different languages. A major difference is that Objective-C is focused on runtime-decisions for dispatching and heavily depends on its runtime library to handle inheritance and polymorphism, while in C++ the focus usually lies on static, compile time, decisions.


2 Answers

You need to name your .m files .mm. And you will be able to compile C++ code with Objective-C.

So, following your example, your AView.m file should be named AView.mm. It's simple as that. It works very well. I use a lot of std containers (std::vector, std::queue, etc) and legacy C++ code in iPhone projects without any complications.

like image 88
Pablo Santa Cruz Avatar answered Sep 25 '22 10:09

Pablo Santa Cruz


Never mind, I feel stupid. All you have to do is rename AView.m to AView.mm so the compiler knows it's Objective-C++, and it compiles without a problem.

like image 42
LandonSchropp Avatar answered Sep 23 '22 10:09

LandonSchropp