I am trying to extend python using a simple C file. I succeeded in creating my own python module, but I also need the C file itself to compile and run as a standalone executable. I can successfully compile it, but when I try to run it I get the error "cannot execute binary file: Exec format error".
Here is my C source file (hellomodule.c):
#include <Python.h>
void print_hello(const char* name){
printf("Hello %s!\n", name);
}
//Only used by Python
static PyObject*
say_hello(PyObject* self, PyObject* args)
{
const char* name;
if (!PyArg_ParseTuple(args, "s", &name))
return NULL;
//printf("Hello %s!\n", name);
print_hello("World");
Py_RETURN_NONE;
}
//Only used by Python
static PyMethodDef HelloMethods[] =
{
{"say_hello", say_hello, METH_VARARGS, "Greet somebody."},
{NULL, NULL, 0, NULL}
};
//Only used by Python
PyMODINIT_FUNC
inithello(void)
{
(void) Py_InitModule("hello", HelloMethods);
}
int main(){
print_hello("World");
}
I can "successfully" compile it without any errors or warnings with the following:
gcc -I /usr/include/python2.7 -c hellomodule.c -o hellomodule
After making the "hellomodule" file executable, I run it and get the error:
-bash: ./hellomodule: cannot execute binary file: Exec format error
Why would this be causing such an error?