c调用c++函数

简介: c调用c++普通函数    cpp_test/cpp.h#ifndef CPP_H#define CPP_H#include "extern_cpp.h"int add(int a, int b);char add(char a, char b);#endif // CPP_H    cpp_test/extern_cpp.
  • c调用c++普通函数
    cpp_test/cpp.h

#ifndef CPP_H
#define CPP_H

#include "extern_cpp.h"

int add(int a, int b);
char add(char a, char b);

#endif // CPP_H

    cpp_test/extern_cpp.h

#ifndef EXTERN_CPP_H
#define EXTERN_CPP_H

#ifdef __cplusplus
extern "C"
{
#endif

int add_int(int a, int b);
char add_char(char a, char b);

#ifdef __cplusplus
}
#endif

#endif // EXTERN_CPP_H

    cpp_test/cpp.cpp

#include "cpp.h"

#include <iostream>

int add(int a, int b)
{
    std::cout << "int a+b=" << a+b << std::endl;
    return a+b;
}

char add(char a, char b)
{
    std::cout << "char a+b=" << a+b << std::endl;
    return a+b;
}

int add_int(int a, int b)
{
    return add(a,b);
}

char add_char(char a, char b)
{
    return add(a,b);
}

    c_test/main.c

#include <stdio.h>

#include "../cpp_test/extern_cpp.h"

int main(int argc, char *argv[], char *env[])
{
    printf("%d\n", add_int(2,3));
    printf("%c\n", add_char(20, 30));

    return 0;
}

编译 g++ -c cpp.cpp       
    gcc main.c ../cpp_test/cpp.o -lstdc++

  • c调用c++类函数
     cpp_test/cpp.h

#ifndef CPP_H
#define CPP_H

#include "extern_cpp.h"

struct example
{
public:
    example(void);
    example(int i, int j);
    ~example(void);
    int add(void);
    int a,b;
};

#endif // CPP_H

    cpp_test/extern_cpp.h

#ifndef EXTERN_CPP_H
#define EXTERN_CPP_H

#ifdef __cplusplus
extern "C" {
#endif

typedef struct example example;
example* exmaple_create(int a, int b);
void example_delete(example* e);
int example_add(example* e);

#ifdef __cplusplus
}
#endif

#endif // EXTERN_CPP_H

    cpp_test/cpp.cpp

#include "cpp.h"
#include <iostream>
example::example(void){}
example::example(int i, int j):a(i),b(j){}
example::~example(void){}

int example::add(void)
{
    std::cout << "a+b=" << a+b << std::endl;
    return a+b;
}

example* exmaple_create(int a, int b)
{
    return new example(a, b);
}

void example_delete(example* e)
{
    delete e;
}

int example_add(example* e)
{
    return e->add();
}

    c_test/main.c
#include <stdio.h>
#include "../cpp_test/extern_cpp.h"

int main(int argc, char *argv[], char *env[])
{
    example *e = exmaple_create(2, 3);
    printf("%d\n", example_add(e));
    example_delete(e);

    return 0;
}
编译 g++ -c cpp.cpp

        gcc main.c ../cpp_test/cpp.o -lstdc++


目录
相关文章
|
14天前
|
C++ 容器
【C++】拷贝构造函数、拷贝赋值函数与析构函数
【C++】拷贝构造函数、拷贝赋值函数与析构函数
65 6
|
10天前
|
编译器 程序员 语音技术
C++的超20种函数类型分享
C++超20种函数类型:编程语言规定规则,编译器实现预定规则
|
10天前
|
C++
C++函数的返回数据写法的思路
C++函数使用尾置返回类型、decltype、类型别名返回一个数组引用
|
11天前
|
关系型数据库 MySQL 测试技术
技术分享:深入C++时间操作函数的应用与实践
技术分享:深入C++时间操作函数的应用与实践
14 1
|
14天前
|
安全 C++ 开发者
C++一分钟之-函数参数传递:值传递与引用传递
【6月更文挑战第19天】C++中函数参数传递涉及值传递和引用传递。值传递传递实参副本,安全但可能效率低,适合不变对象;引用传递传递实参引用,允许修改,用于高效修改或返回多值。值传递示例显示交换不生效,而引用传递示例实现交换。常量引用则防止意外修改。选择传递方式需考虑效率与安全性。
26 2
|
22天前
|
C++
C++中函数模版与类模版
C++中函数模版与类模版
24 4
|
20天前
|
编译器 C++
c++primer plus 6 读书笔记 第八章 函数探幽0
c++primer plus 6 读书笔记 第八章 函数探幽0
|
20天前
|
编译器 vr&ar C++
c++primer plus 6 读书笔记 第七章 函数--C++的编程模块
c++primer plus 6 读书笔记 第七章 函数--C++的编程模块
|
20天前
|
编译器 C++ 存储
【C++语言】类和对象--默认成员函数 (中)
【C++语言】类和对象--默认成员函数 (中)
【C++语言】类和对象--默认成员函数 (中)