1

I have used C code in a Python project like in this tutorial. I built an extension so that it was possible to call the A function present in the main.c code through Python. However, function A calls other various functions that are present in a file called code.c, and I'm having trouble using those functions.

There are no problems if all the functions are placed in main.c, but I would like to modularize this project for organizational reasons!

The setup.py for building the modules is as follows.

ext       =  [
                 Extension(
                 'main',
                 sources = ['main.c'] ,
                 extra_compile_args=['-lpq'] ,
                 extra_link_args = ['-L/usr/local/pgsql/lib','-lpq'],
                 language=['c']
                 )
            ]

setup(name='project', version='1.0', ext_modules = ext)

How could I modify it so that the code.c functions could be used inside main.c without any problems?

Here is an outline of the situation:

main.c

#include <Python.h>
#include "code.h"

//....

void send(char* name)
{
   //DO SOMETHING
   function_from_code(name)
}

code.c

.....

#include "code.h"

void function_from_code(char* name)
{
   //DO SOMETHING
}  

and then the Python code:

import main

...

main.send("My Name")

So in this way, the python code calls the function of module main C (so far so good). At the moment main.c calls a function from code.c, it throws the following error:

ImportError: /usr/local/lib/python2.7/dist-packages/main.so: undefined symbol: function_from_code

Apparently, using #include is not enough.

3
  • You ar looking for the documentation. Search somewhere on python.org. Commented May 31, 2017 at 21:33
  • you're calling these functions directly or are the functions in code.c c-functions that can be called also from Python? In the first case you could just link to "code.c" (either include or like a library) and in the latter case just "import" the "code.c" python module. In case your looking for good answers, could you provide some example of the files and how you want to call them? Commented May 31, 2017 at 21:33
  • @MSeifert See the EDIT. But briefly, the python code calls the function in main.c. And only main.c calls the function in code.c. Commented May 31, 2017 at 21:57

1 Answer 1

1

This is too long for a comment and I'm not sure that it will fix the problem. I think it's just because it doesn't compile code.c and code.h when they are not listed explicitly as source (see "Extension names and packages").

Personally I would use either the depends argument for the Extension:

from glob import glob

ext =  [Extension('main',
                  sources=['main.c'] ,
                  depends=glob('*.c') + glob('*.h'),
                  extra_compile_args=['-lpq'] ,
                  extra_link_args=['-L/usr/local/pgsql/lib','-lpq'],
                  language=['c']
                  )
        ]

or list all the files in source:

ext =  [Extension('main',
                  sources=['main.c', 'code.h', 'code.c'] ,
                  extra_compile_args=['-lpq'] ,
                  extra_link_args=['-L/usr/local/pgsql/lib','-lpq'],
                  language=['c']
                  )
        ]

Not sure if the order of the source files or depends files matters...

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

2 Comments

It works perfectly if you remove the .h from the sources list. Thanks very much my friend!!!!
@MFigueredo You're welcome. Just a question - I'm not sure if I understand your comment correctly: sources=['main.c', 'code.c'] (without the code.h) worked for you?

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.