xcode 支持 object-c 混編,在object-c 中調用c,c++接口
第一步 定義c語言 接口(File.c)
#include <stdio.h> void printsByC(){ printf("調用C語言。"); }
第二步 定義c++ 接口
student.h文件
#ifndef __test_hun__student__ #define __test_hun__student__ #include <iostream> #endif /* defined(__test_hun__student__) */
student.cpp文件
#include "student.h" using namespace std; class Student{ public: void getWeight(){ cout<<"Object C與C++混合編程。體重為:"<<weight<<"kg"; printf("調用C++語言。getWeight"); } void setWeight(int x){ weight = x; printf("調用C++語言。setWeigth"); } private: int weight; };
第三步 將 object-c implementation文件名 .m 改稱.mm 告訴編譯器 混編
下面的例子是 object-c 調用接口
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. printf("調用C語言。"); Human human; human.setWeight(26); human.getWeight(); Student *student=new Student(); student->getWeight(); delete student; }