开发者

Add C/C++ file to Xcode4 iOS project

开发者 https://www.devze.com 2023-03-03 15:32 出处:网络
During my iOS xcode4 project, I plan to add some c or c++ source files such as (.h/.c) and (.h/.cpp) Then met linkage error.

During my iOS xcode4 project, I plan to add some c or c++ source files such as (.h/.c) and (.h/.cpp)

Then met linkage error.

I guess need to add link option to "Other Link ... "

The simply question is what kind of link option need to be added ?

This question may be dupl开发者_StackOverflowicated, I guess.


To include C++ classes in your iOS project, rename the OBJ-C class that's including CPP implementation file to .mm -- this will tell the compiler to compile both Object-C and C++. Example:

SomeCPP.h:

class SomeCPP
{
public:
    int someInt;
    int returnSomeInt(void);
};

SomeCPP.cpp:

#import "SomeCPP.h"

int SomeCPP::returnSomeInt(void)
{
    return this->someInt;
};

MyViewController.mm (OBJ-C++ implementation):

#import "SomeCPP.h"
...
- (void)viewDidLoad
{
    SomeCPP *someCPPObject = new SomeCPP();
    someCPPObject->someInt = 5;
    int someInt = someCPPObject->returnSomeInt();
    ...
    delete someCPPObject;
}


With xcode4, if you want to import a cpp header from an objective-c header you should rename this objective-c header with extension ".hh"

MyObjective-c header :

#import "SomeCPP.hpp"
...
- (void)myFunction
{
  // etc..
}

The ".hpp" extension for cpp header is only for human easy-reading.

0

精彩评论

暂无评论...
验证码 换一张
取 消