object-c 混編 調用C,C++接口


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;
}

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM