diff --git a/examples/ch2-07/hello-py/Makefile b/examples/ch2-07/hello-py/Makefile new file mode 100644 index 0000000..69a2cc0 --- /dev/null +++ b/examples/ch2-07/hello-py/Makefile @@ -0,0 +1,11 @@ +default: + go build -buildmode=c-shared -o gopkg.so main.go + python3 -c 'import gopkg; print(gopkg.system("time"))' + +clean: + -rm *.so + +# PKG_CONFIG=python3-config go build -buildmode=c-shared -o say-hello.so main.go +# python3-config --ldflags +# python3-config --include + diff --git a/examples/ch2-07/hello-py/main.go b/examples/ch2-07/hello-py/main.go new file mode 100644 index 0000000..46f2977 --- /dev/null +++ b/examples/ch2-07/hello-py/main.go @@ -0,0 +1,51 @@ +package main + +/* +// macOS: +// python3-config --cflags +// python3-config --ldflags +#cgo darwin CFLAGS: -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -fno-strict-aliasing -Wsign-compare -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -g +#cgo darwin LDFLAGS: -L/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin -lpython3.6m -ldl -framework CoreFoundation + +// linux +#cgo linux pkg-config: python3 + +// windows +// should generate libpython3.a from python3.lib + +#define Py_LIMITED_API +#include + +static PyObject * +spam_system(PyObject *self, PyObject *args) { + const char *command; + if (!PyArg_ParseTuple(args, "s", &command)) { + return NULL; + } + + int status = system(command); + return PyLong_FromLong(status); +} + +static PyMethodDef modMethods[] = { + {"system", spam_system, METH_VARARGS, "Execute a shell command."}, + {NULL, NULL, 0, NULL} +}; + +void* PyInit_gopkg(void) { + static struct PyModuleDef module = { + PyModuleDef_HEAD_INIT, "gopkg", NULL, -1, modMethods, + }; + return (void*)PyModule_Create(&module); +} +*/ +import "C" + +import "fmt" + +func main() {} + +// export SayHello +func SayHello(name *C.char) { + fmt.Printf("hello %s!\n", C.GoString(name)) +}