Last active
March 7, 2018 21:12
-
-
Save capocasa/bd403a3c1b1bf12ab434d2d7154470e6 to your computer and use it in GitHub Desktop.
nim host and guest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <dlfcn.h> | |
typedef void (*target_t)(); | |
void target() | |
{ | |
std::cout << "host: target\n"; | |
} | |
int main() { | |
void *handle; | |
typedef void (*init_t)(target_t); | |
init_t init; | |
const char *err; | |
handle = dlopen("plugin.cpp.so", RTLD_NOW); | |
init = (init_t) dlsym(handle, "init"); | |
err = dlerror(); | |
if (err) { | |
std::cerr << "dlsym failed: " << err << '\n'; | |
return 1; | |
} | |
init(&target); | |
dlclose(handle); | |
handle = dlopen("plugin.nim.so", RTLD_NOW); | |
init = (init_t) dlsym(handle, "init"); | |
err = dlerror(); | |
if (err) { | |
std::cerr << "dlsym failed: " << err << '\n'; | |
return 1; | |
} | |
init(&target); | |
dlclose(handle); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: host plugin.cpp.so plugin.nim.so | |
host: host.cpp | |
g++ host.cpp -ldl -o host | |
plugin.cpp.so: plugin.cpp | |
g++ -fpic -c plugin.cpp -o plugin.o | |
g++ -shared -o plugin.cpp.so plugin.o | |
plugin.nim.so: plugin.nim | |
nim cpp --app=lib -o:plugin.nim.so plugin.nim | |
clean: | |
rm -f plugin.cpp.so plugin.nim.so plugin.o host nimcache/* | |
rmdir nimcache | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
typedef void (*target_t)(); | |
extern "C" { | |
void init(target_t target) { | |
std::cout << "plugin.cpp: init\n"; | |
target(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
target_t* = proc () | |
proc init(target: target_t) {.cdecl,exportc,dynlib.} = | |
echo "plugin.nim: init" | |
target() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment