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/bash g++ -c -Wall -Werror -fPIC cpp_source.cpp g++ -shared -o libcpp_source.so cpp_source.o export LD_LIBRARY_PATH=/home/csdn/workspace/test:$LD_LIBRARY_PATH gcc -L/home/csdn/workspace/test -Wall main.c -o main -lcpp_source
4.运行
export LD_LIBRARY_PATH=/home/csdn/workspace/test:$LD_LIBRARY_PATH This is a C++ code being used within C code