Binding SWIG要求我将整个应用程序与模块链接

Binding SWIG要求我将整个应用程序与模块链接,binding,linker,swig,python-embedding,Binding,Linker,Swig,Python Embedding,我有一个C应用程序,我想在SWIG的帮助下用Python脚本扩展它。然而,我有一个严重的问题:每当SWIG模块引用一个C对象,比如函数或变量时,我就必须编译文件(包含它的文件)并将其与结果\u pymodule2链接起来。因此…我正在构建pymodule2.I文件,如下所示: swig -verbose -cpperraswarn -builtin \ -I`pwd` -I`pwd`/src \ -I/usr/include/glib-2.0/ \

我有一个C应用程序,我想在SWIG的帮助下用Python脚本扩展它。然而,我有一个严重的问题:每当SWIG模块引用一个C对象,比如函数或变量时,我就必须编译文件(包含它的文件)并将其与结果
\u pymodule2链接起来。因此
…我正在构建
pymodule2.I
文件,如下所示:

    swig -verbose -cpperraswarn -builtin \
        -I`pwd` -I`pwd`/src \
        -I/usr/include/glib-2.0/ \
        -I/lib/glib-2.0/include \
        -I/usr/include/python3.8 \
        -I/usr/include \
            -python src/python/pymodule2.i
%module pymodule2
%include openfiles.h
然后我链接
\u pymodule2。所以

    gcc -fPIC -Wall -shared \
        -I `pwd` -I `pwd`/src \
        -I /usr/include/glib-2.0/ \
        -I /lib/glib-2.0/include \
        -I /usr/include/python3.8/ \
        some_c_source1.c some_c_source2.c \
            pymodule2_wrap.c \
        -o _pymodule2.so
模块如下:

    swig -verbose -cpperraswarn -builtin \
        -I`pwd` -I`pwd`/src \
        -I/usr/include/glib-2.0/ \
        -I/lib/glib-2.0/include \
        -I/usr/include/python3.8 \
        -I/usr/include \
            -python src/python/pymodule2.i
%module pymodule2
%include openfiles.h
标题:

struct OpenFile {
    int modified;
    const char *filename;
};
typedef struct OpenFile OpenFile;

struct OpenFiles {
    OpenFile *open_files;
    int count;
};

typedef struct OpenFiles OpenFiles;

extern OpenFiles* pymc_get_open_files();
问题是,这会创建两个所有内容的实例-函数、全局变量等。但是,如果我不将使用过的C文件与模块链接,则在从嵌入式解释器导入应用程序中的模块时,我会得到以下信息:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/root/Dokumenty/swig-test/pymodule2.py", line 15, in <module>
    from _mc import *
ImportError: /root/Documents/swig-test/_pymodule2.so: undefined \
     symbol: pymc_get_open_files
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/root/Dokumenty/swig test/pymodule2.py”,第15行,在
从(mc import)*
ImportError:/root/Documents/swig test/_-pymodule2.so:未定义\
符号:pymc_获取_打开_文件
难道这些符号不可用吗?毕竟,加载模块的应用程序具有在

我在函数中设置了一个全局变量,并且我已经验证了,这样的链接会导致它的两个独立实例