C++
1. файл в test_c++.cpp
#include <iostream>
extern "C" {
int main ();
}
int main ()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
компилятор
g++ -shared -fpic test_c++.cpp -o test_c++.so
===========
C
2. файл в test_c++toc.c
#include <stdio.h>
#include <dlfcn.h>
int main (){
void *ptr;
ptr = dlopen("./test_c++.so", RTLD_NOW | RTLD_LOCAL);
if (ptr == 0){
fputs (dlerror(),stderr);
puts ("");
return 1;
}
int (*main_cpp) (void) = 0;
main_cpp = dlsym (ptr,"main");
if (main_cpp == 0){
fprintf (stderr,"%s\n",dlerror());
return 1;
}
main_cpp ();
dlclose (ptr);
return 0;
}
gcc test_c++toc.c -o test_c++toc -ldl
./test_c++toc