c中调用c++

// my.h
#ifndef MY_H
#define MY_H
extern "C" {
    int my_mul(int x,int y);
}
#endif

// my.cpp
#include "my.h"

int my_mul(int x,int y)
{
    return x*y;
}

// test.c
#include <stdio.h>
extern int my_mul(int x,int y);
int main() {
    printf("%d\n",my_mul(3,5));
    return 0;
}

g++ -c my.cpp
 gcc test.c my.o -o test

猜你喜欢

转载自www.cnblogs.com/beiyala/p/13368082.html