• c c++混合调用


    //extern_cpp.h

    #pragma once
    //extern_cpp.h
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    int add_int(int a, int b);
    char add_char(char a, char b);
    
    typedef struct Rectangle Rectangle;
    Rectangle* RectangleCreate(int width, int height);
    void RectangleDestroy(Rectangle* rec);
    int RectangleArea(Rectangle* rec);
    
    #ifdef __cplusplus
    }
    #endif

    //cppfunc.h

    #pragma once
    
    //cppfunc.h
    #include "extern_cpp.h"
    
    struct Rectangle {
        int _width;
        int _height;
        Rectangle(int width = 0, int height = 0); 
        int Area();
    };
    
    
    int add(int a, int b);
    
    char add(char a, char b);

    //cppfunc.cpp

    //cppfunc.cpp
    #include "cppfunc.h"
    
    Rectangle::Rectangle(int width, int height):
        _width(width), _height(height)
    {
    }
    
    int Rectangle::Area()
    {
        return _width * _height; 
    }
    
    int add(int a, int b)
    {
        return a + b;
    }
    
    char add(char a, char b)
    {
        return a + b;
    }
    
    // c call
    
    int add_int(int a, int b)
    {
        return a + b;
    }
    
    char add_char(char a, char b)
    {
        return a + b;
    }
    
    Rectangle* RectangleCreate(int width, int height)
    {
        return new Rectangle(width, height);
    }
    
    void RectangleDestroy(Rectangle* rec)
    {
        delete rec;
    }
    
    int RectangleArea(Rectangle* rec)
    {
        return rec->Area();
    }

    //mainc.c

    //mainc.c
    #include <stdio.h>
    #include "extern_cpp.h"
    
    int main()
    {
        Rectangle* rec = RectangleCreate(4, 12);
        int area = RectangleArea(rec);
        RectangleDestroy(rec);
    
        int c = add_int(12, 23);
        char d = add_char(33, 10);
        
        printf("c = %d
    ", c);
        printf("d = %d
    ", d);
        printf("area = %d
    ", area);
        return 0;
    }

    //maincpp.cpp

    //maincpp.cpp
    
    #include <iostream>
    #include <cstdio>
    
    #include "cppfunc.h"
    
    using std::cout;
    using std::endl;
    
    int main()
    {
        int a = add(1000, 2000);
        char c = add('A', (char)2);
        Rectangle rec(2, 3);
        int area = rec.Area();
        cout << "a = " << a << ", c = " << c << endl;
        cout << "area = " << area << endl;
        return 0;
    }
  • 相关阅读:
    Java 多线程(一) 基础知识与概念
    hashMap和treeMap
    转:Java IO流学习总结
    hibernate缓存
    java aio nio bio
    java1.8新特性
    LeetCode Contiguous Array
    LeetCode Sort Characters By Frequency
    LeetCode Subarray Sum Equals K
    LeetCode Group Anagrams
  • 原文地址:https://www.cnblogs.com/endenvor/p/11686945.html
Copyright © 2020-2023  润新知