python 导入 'undefined symbol: mysql_query' 时出现 SWIG 错误

SWIG error on python import 'undefined symbol: mysql_query'

我正在 python 中构建一个小型的“概念验证”图书馆系统,并希望由 c 扩展处理数据库(出于安全考虑并阻止他人干预代码)

我编译了所有内容,现在有了我的 _databaseHandler.so 文件,但遗憾的是 python 在导入时崩溃

我的C代码

#include "library.h"

#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>



MYSQL connect(){

    MYSQL *con = mysql_init(NULL);
    const char *address = "rarehost.net";
    const char *username = "MySQL-name";
    const char *password = "MySQL-password";
    const char *databaseName = "dbname";
    const unsigned int port = 3306;


    if(con == NULL){
        fprintf(stderr, "%s\n", mysql_error(con));
        exit(1);
    }

    if(mysql_real_connect(con, address, username, password, databaseName, port, NULL, 0) == NULL) {
        fprintf(stderr, "%s\n", mysql_error(con));
    } else{
        return *con;
    }
}

void finish_with_error(MYSQL *con)
{
    fprintf(stderr, "%s\n", mysql_error(con));
    mysql_close(con);
    exit(1);
}

char* getCustomers() {
    MYSQL con = connect();

    if (mysql_query(&con, "SELECT * FROM cars")) {
        finish_with_error(&con);
    }

    MYSQL_RES *result = mysql_store_result(&con);
    int num_fields = mysql_num_fields(result);

    MYSQL_ROW row;

    while((row = mysql_fetch_row(result))) {
        for (int i = 0; i < num_fields; ++i) {
            printf("%s ", row[i] ? row[i] : "NULL");
            /*return row[i] ? row[i] : "NULL";*/
        }

        printf("\n");
    }
    return "hi";
}

是的,我只知道 returns 嗨,这是为了测试目的,直到我设法将它导入 python。

我的头文件

#ifndef DATABASEHANDLER_LIBRARY_H
#define DATABASEHANDLER_LIBRARY_H

#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>

char* getCustomers();


#endif //DATABASEHANDLER_LIBRARY_H

我的界面文件

%module databaseHandler
%{
#include "library.h"
#include <mysql/mysql.h>
%}

char* getCustomers();

最后但同样重要的是我的 makefile


_databaseHandler.so: library.o library_wrap.o
    gcc -shared library.o library_wrap.o -o _databaseHandler.so

library.o:
    gcc -c -fPIC library.c -I/usr/include/mariadb/mysql

library_wrap.o:
    swig -python library.i
    gcc -c -fPIC library_wrap.c -I/usr/include/python3.7

这是让我做噩梦的输出:

Traceback (most recent call last):
  File "/home/user/CLionProjects/databaseHandler/databaseHandler.py", line 14, in swig_import_helper
    return importlib.import_module(mname)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 670, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 583, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1043, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: /home/user/CLionProjects/databaseHandler/_databaseHandler.so: undefined symbol: mysql_query

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import databaseHandler
  File "/home/user/CLionProjects/databaseHandler/databaseHandler.py", line 17, in <module>
    _databaseHandler = swig_import_helper()
  File "/home/user/CLionProjects/databaseHandler/databaseHandler.py", line 16, in swig_import_helper
    return importlib.import_module('_databaseHandler')
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: /home/user/CLionProjects/databaseHandler/_databaseHandler.so: undefined symbol: mysql_query

(python 程序只是导入和 print("hello") 函数)

这可能是一些愚蠢的小错误,但我一辈子都找不到它。感谢任何帮助。

由于您的库使用了 mysql 中的函数,因此您需要 link 在 link 使用该库时针对适当的库。 (还有其他方法可以安排在运行时定义符号,但这是迄今为止最明智的选择)

因此,您需要在 Makefile 中添加如下内容:

_databaseHandler.so: library.o library_wrap.o
    gcc -shared library.o library_wrap.o -o _databaseHandler.so -lmysql -L/path/to/lib

(您需要提供确切的库名称和 link 的路径,而不是您用来提供它的包,通常有一个 pkgconfig 文件可以帮助处理这个或一些文档,如果它不是不清楚)。