Skip to content

Instantly share code, notes, and snippets.

@jinliangwei
Created March 25, 2017 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinliangwei/441483e62934f7cf00bd2da37a8c3ac8 to your computer and use it in GitHub Desktop.
Save jinliangwei/441483e62934f7cf00bd2da37a8c3ac8 to your computer and use it in GitHub Desktop.
After "make all", running "julia helloworld.jl" causes abort due to "attempt to free invalid pointer". However, running "./test_main" works.
# replace the string below with the absolute path to libtest.so
ccall((:helloworld, "[abosolute path to the libtest.so]"), Void, ())
all: libtest.so test_main
CXX = g++ -std=c++14
test.pb: test.proto
protoc --cpp_out=. test.proto
test.pb.h: test.pb
test.pb.cc : test.pb
test.o: test.c test.pb.h
$(CXX) -fPIC $(CFLAGS) -c $< -o $@
test_main: test_main.c
gcc $< -ldl -o $@
test.pb.o: test.pb.cc
$(CXX) -fPIC $(CFLAGS) -c $< -o $@
libtest.so: test.o test.pb.o
gcc -shared -Wl,-soname,libtest.so.0 -o libtest.so.0.0 \
$^ -lprotobuf -ltcmalloc
ln -sf libtest.so.0.0 $@
clean:
rm -f *.o test.pb.h test.pb.cc *.so *.so.* test_main
#include <iostream>
extern "C" {
void helloworld() {
std::cout << "hello world!" << std::endl;
}
}
package test;
message Test {
required int32 index = 1;
required string value = 2;
}
#include <dlfcn.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
dlerror();
void *lib = dlopen("/home/ubuntu/test_protobuf/libtest.so", RTLD_LAZY | RTLD_GLOBAL | RTLD_DEEPBIND);
if (lib == NULL) {
const char* err_msg = dlerror();
printf("something wrong: %s\n", err_msg);
return 1;
}
void *void_fptr = dlsym(lib, "helloworld");
void (*fptr)(void);
fptr = (void (*)(void)) void_fptr;
if (void_fptr == NULL) {
const char* err_msg = dlerror();
printf("something wrong: %s\n", err_msg);
return 1;
}
fptr();
dlclose(lib);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment