C调用C++函数

来源:互联网 发布:网络办公管理v3.1系统 编辑:程序博客网 时间:2024/06/07 00:23

1.C++代码

#include <iostream>using namespace std;extern "C" void func(void){    cout << "This is a C++ code" << endl;}

2.C代码

#include <stdio.h>extern void func(void);void f(void){    printf("being used within C code\n");}int main(int argc, char *argv[]){    func();    f();    return 0;}
3.编译

#!/bin/bashg++ -c -Wall -Werror -fPIC cpp_source.cppg++ -shared -o libcpp_source.so cpp_source.oexport LD_LIBRARY_PATH=/home/csdn/workspace/test:$LD_LIBRARY_PATHgcc -L/home/csdn/workspace/test -Wall main.c -o main -lcpp_source
4.运行
export LD_LIBRARY_PATH=/home/csdn/workspace/test:$LD_LIBRARY_PATHThis is a C++ codebeing used within C code







0 0
原创粉丝点击