0

I have a c program that I need to compile and use in the middle of a python code. it is not c++ program so I assume I should change this syntax for compiling because I get a lot of errors related to not having "main()" in the program which is just because it is a c (not c++)for a x86_64 Mac system?

how should I change this syntax for a program.c code?

g++ -o program.x program.c

2
  • 3
    I suggest you start by looking at e.g. SWIG. Commented Oct 3, 2012 at 8:43
  • I looked at SWIG a couple of times. The documentation is so massive that I decided to use Boost::Python and am not looking back. Commented Oct 3, 2012 at 9:29

1 Answer 1

1

Your C code should be compiled into a shared library:

gcc -shared -o program.so -Wall -Wextra -fPIC -O2 -DNDEBUG program.c

And then you can use ctypes module to invoke functions in your library from Python.

Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't this be .dylib on a Mac?
It probably can have any extension since when loading the shared library in Python one can specify the full path. But you may be right in general, I only ever use Macs with Linux.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.