简体   繁体   English

python 上的 SWIG 错误导入“未定义符号:mysql_query”

[英]SWIG error on python import 'undefined symbol: mysql_query'

I am building a small "proof of concept" library system in python and would like to have the database handled by a c extension (for security and to stop someone from meddling with the code)我正在 python 中构建一个小型“概念证明”库系统,并希望由 c 扩展处理数据库(为了安全并阻止某人干预代码)

I compiled everything and now have my _databaseHandler.so file but sadly python crashes upon import我编译了所有内容,现在有了我的 _databaseHandler.so 文件,但遗憾的是 python 在导入时崩溃

My C code我的 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";
}

And yes I know it just returns hi, this is for testing purposes until I manage to import it into python.是的,我知道它只是返回你好,这是为了测试目的,直到我设法将它导入 python。

My header file我的 header 文件

#ifndef DATABASEHANDLER_LIBRARY_H
#define DATABASEHANDLER_LIBRARY_H

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

char* getCustomers();


#endif //DATABASEHANDLER_LIBRARY_H

My interface file我的接口文件

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

char* getCustomers();

And last but not least my makefile最后但并非最不重要的是我的 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

And here is the output that has been giving me nightmares:这是让我做噩梦的output:

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

(The python program is just the import and a print("hello") function) (python 程序只是导入和 print("hello") 函数)

It's probably some stupid little mistake but I cannot for the life of me find it.这可能是一些愚蠢的小错误,但我终生无法找到它。 Any help is appreciated.任何帮助表示赞赏。

Since your library uses functions from mysql you'll need to link that library against the appropriate library when linking it.由于您的库使用 mysql 中的函数,因此您需要在链接时将该库与相应的库链接。 (There are other ways you could arrange for the symbol to be defined at runtime, but this is by far the most sensible option) (还有其他方法可以安排在运行时定义符号,但这是迄今为止最明智的选择)

So in your Makefile you'll want to add something like this:因此,在您的 Makefile 中,您需要添加如下内容:

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

(You'll need to supply the exact libary name and path to link against from the package you used to provide it, there's often a pkgconfig file that can help with this or some documentation if it isn't clear). (您需要提供准确的库名称和路径以从您用来提供它的 package 链接到它,如果不清楚,通常有一个 pkgconfig 文件可以帮助解决这个问题或一些文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM