Skip to content

Instantly share code, notes, and snippets.

@zhmz90
Last active August 28, 2016 15:20
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 zhmz90/00297564d3c048eaa7dccb2279a237c6 to your computer and use it in GitHub Desktop.
Save zhmz90/00297564d3c048eaa7dccb2279a237c6 to your computer and use it in GitHub Desktop.
#include "add2.h"
extern "C" {
int add2(int a , int b){
return a+b;
}
}
#ifdef __cplusplus
extern "C" {
#endif
int add2(int, int);
#ifdef __cplusplus
}
#endif
#include <stdio.h>
#include "add2.h"
int add3(int a, int b, int c){
return a+b+c;
}
int main(){
printf("test %d", add2(1,2));
}
add.so:*.c *.cpp *.h
gcc main.c add2.h add2.cpp -fpic -shared -o add.so
result = ccall((:add2,"/home/guo/Github/cpp/add.so"),Cint,(Cint,Cint),1,2)
println(result)
@zhmz90
Copy link
Author

zhmz90 commented Aug 28, 2016

使用 make 编译好shared library。

在Julia REPL中测试:

julia> ccall((:add2,"/home/guo/Github/cpp/add.so"),Cint,(Cint,Cint),1,2)
3

或者执行 julia test_shared_library_in_julia.jl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment