在Xcode中使用C++与Objective-C混编

本文介绍了如何在Xcode项目中使用C++与Objective-C混编,通过修改文件后缀为.mm,确保C++代码能够正确编译并运行。示例展示了在ViewController中调用C++类实现输出""Hello, Cpp.""的过程。" 112037439,10539402,Python算法效率:大O表示法与时间复杂度解析,"['Python', '算法', '时间复杂度', '数据结构', '大O表示法']
摘要由CSDN通过智能技术生成

有时候,出于性能或可移植性的考虑,需要在iOS项目中使用到C++。

假设我们用C++写了下面的People类:

//
//  People.h
//  MixedWithCppDemo
//
//  Created by Jason Lee on 12-8-18.
//  Copyright (c) 2012年 Jason Lee. All rights reserved.
//

#ifndef __MixedWithCppDemo__People__
#define __MixedWithCppDemo__People__

#include <iostream>

class People
{
public:
    void say(const char *words);
};

#endif /* defined(__MixedWithCppDemo__People__) */

//
//  People.cpp
//  MixedWithCppDemo
//
//  Created by Jason Lee on 12-8-18.
//  Copyright (c) 2012年 Jason Lee. All rights reserved.
//

#include "People.h"

void People::say(const char *words)
{
    std::cout << words << std::endl;
}

然后,我们用Objective-C封装一下,这时候文件后缀需要为.mm,以告诉编译器这是和C++混编的代码:

//
//  PeopleWrapper.h
// 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值