1.C代码
#include <stdio.h> void f(void) { printf("This is a C code\n"); }
2.C++代码
#include <iostream> using namespace std; extern "C"{ void f(); } void func(void) { cout << "begin used within C++ code" << endl; } int main(int argc, char *argv[]) { f(); func(); return 0; }
3.编译
#!/bin/bash gcc -c -Wall -Werror -fPIC c_source.c gcc -shared -o libc_source.so c_source.o export LD_LIBRARY_PATH=/home/csdn/workspace/test:$LD_LIBRARY_PATH g++ -L/home/csdn/workspace/test -Wall main.cpp -o main -lc_source
4.运行结果
export LD_LIBRARY_PATH=/home/csdn/workspace/test:$LD_LIBRARY_PATH This is a C code begin used within C++ code