Skip to content

Instantly share code, notes, and snippets.

@capocasa
Last active March 7, 2018 21:12
Show Gist options
  • Save capocasa/bd403a3c1b1bf12ab434d2d7154470e6 to your computer and use it in GitHub Desktop.
Save capocasa/bd403a3c1b1bf12ab434d2d7154470e6 to your computer and use it in GitHub Desktop.
nim host and guest
#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;
}
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
#include <iostream>
typedef void (*target_t)();
extern "C" {
void init(target_t target) {
std::cout << "plugin.cpp: init\n";
target();
}
}
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