Skip to content

Instantly share code, notes, and snippets.

@iffy

iffy/Makefile Secret

Created January 25, 2019 00:19
Show Gist options
  • Save iffy/c0e5e962bf3f14acf41dc6969f322747 to your computer and use it in GitHub Desktop.
Save iffy/c0e5e962bf3f14acf41dc6969f322747 to your computer and use it in GitHub Desktop.
import strformat
import strutils
import sequtils
echo "clib initialized"
echo GC_getStatistics()
proc hello_echo*(message:cstring):cstring {.exportc.} =
## Echo a message back
echo GC_getStatistics()
var s_message:string = $message
result = &"{s_message} echo"
echo &"Nim echo result: {result}"
NIMBASE = ~/lib/Nim/lib/nimbase.h
.PHONY: run
run: a.out
./a.out
a.out: test.cpp csrc/clib.cpp csrc/nimbase.h
g++ -Icsrc test.cpp
csrc/clib.cpp: clib.nim
nim cpp --checks:on -d:traceGC --memTracker:on -d:memProfiler -d:lineDir -d:leakDetector -d:useSysAssert -d:useGcAssert --header --nimcache:csrc --compileOnly ./clib.nim
csrc/nimbase.h: $(NIMBASE) csrc/clib.cpp
cp $(NIMBASE) csrc/nimbase.h
#include <iostream>
#include "csrc/clib.h"
using namespace std;
N_CDECL(void, NimMain)(void);
std::string echo(std::string arg) {
const char* message = arg.c_str();
cout << "C++ passing message: " << message << "\n";
cout << "C++ message addr: " << (void*)message << "\n";
char* retval = hello_echo(
(char *)(message)
);
cout << "C++ retval addr : " << (void*)retval << "\n";
return std::string(retval);
}
int main(void) {
NimMain();
cout << "Hello world\n";
int i = 0;
for (i = 0; i < 10; i++) {
std::string msg = std::string("Hello");
std::string ret = echo(msg);
cout << "Got reply: " << ret << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment