Created
January 24, 2017 18:20
-
-
Save arigo/147a09e81c6b5a3580496a5100b5675e to your computer and use it in GitHub Desktop.
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
arigo@catsnake:/tmp/x$ python x_build.py | |
generating ./StrategyBasic.c | |
running build_ext | |
building 'StrategyBasic' extension | |
gcc -pthread -fno-strict-aliasing -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -DNDEBUG -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fPIC -I/usr/include/python2.7 -c StrategyBasic.c -o ./StrategyBasic.o | |
gcc -pthread -shared -Wl,-O1,--sort-common,--as-needed,-z,relro ./StrategyBasic.o -L/usr/lib -lpython2.7 -lpython2.7 -o ./libStrategyBasic.so | |
arigo@catsnake:/tmp/x$ gcc main.c -L. -lStrategyBasic | |
arigo@catsnake:/tmp/x$ LD_LIBRARY_PATH=. a.out | |
init() called! |
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
struct _AlgoServer { | |
void (*init)(void); | |
void (*destroy)(void); | |
void (*getAccountInfo)(char*); | |
}; | |
extern struct _AlgoServer AlgoServer; | |
extern void runStrategyBasic(struct _AlgoServer *); |
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 <stdio.h> | |
#include "algoserver.h" | |
void init() { | |
printf("init() called!\n"); | |
} | |
void destroy() { | |
} | |
void getAccountInfo(char *result) { | |
result = "SUCCESSFUL ACCOUNT INFO RETURNED\0"; | |
} | |
struct _AlgoServer AlgoServer = { | |
.init = init, | |
.destroy = destroy, | |
.getAccountInfo = getAccountInfo | |
}; | |
extern void runStrategyBasic(struct _AlgoServer *); | |
int main(void) | |
{ | |
runStrategyBasic(&AlgoServer); | |
} |
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
import cffi | |
ffibuilder = cffi.FFI() | |
with open('algoserver.h') as f: | |
ffibuilder.embedding_api(f.read()) | |
ffibuilder.set_source("StrategyBasic", r''' | |
#include "algoserver.h" | |
''') | |
ffibuilder.embedding_init_code(""" | |
from StrategyBasic import ffi | |
@ffi.def_extern() | |
def runStrategyBasic(server): | |
server.init() | |
""") | |
ffibuilder.compile(target="libStrategyBasic.*", verbose=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment