gpt4 book ai didi

python - add_swig_library 的 undefined symbol

转载 作者:行者123 更新时间:2023-12-01 00:25:07 25 4
gpt4 key购买 nike

我尝试使用 CMake 编写 SWIG 的简单使用示例,但收到以下错误消息:

Undefined symbols for architecture x86_64:
"_My_variable", referenced from:
_Swig_var_My_variable_get in examplePYTHON_wrap.c.o
_Swig_var_My_variable_set in examplePYTHON_wrap.c.o
"_fact", referenced from:
__wrap_fact in examplePYTHON_wrap.c.o
"_get_time", referenced from:
__wrap_get_time in examplePYTHON_wrap.c.o
"_my_mod", referenced from:
__wrap_my_mod in examplePYTHON_wrap.c.o
ld: symbol(s) not found for architecture x86_64

CMake 脚本是:

cmake_minimum_required(VERSION 3.8)

cmake_policy(SET CMP0078 OLD)
cmake_policy(SET CMP0086 OLD)

project(example C)

find_package(PythonLibs 3 REQUIRED)
find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} REQUIRED)

message(STATUS "Python executable is ${PYTHON_EXECUTABLE}")
message("PYTHONLIBS_VERSION_STRING: ${PYTHONLIBS_VERSION_STRING}")

include_directories(${PYTHON_INCLUDE_PATH} ${CMAKE_SOURCE_DIR})

find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

message("CMAKE_SWIG_FLAGS: ${CMAKE_SWIG_FLAGS}")

swig_add_library(example TYPE SHARED LANGUAGE python SOURCES example.i)
swig_link_libraries(example ${PYTHON_LIBRARIES})

我的 SWIG 示例依赖于两个文件。

首先,有一个 C 文件(example.c):

#include <time.h>

double My_variable = 3.0;

int fact(int n)
{
if (n <= 1)
return 1;
else
return n * fact(n-1);
}

int my_mod(int x, int y)
{
return (x%y);
}

char *get_time()
{
time_t ltime;
time(&ltime);
return ctime(&ltime);
}

还有一个 SWIG 文件(example.i):

%module example

%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}

extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();

由于我不明白的原因,使用 CMake 时无法识别 example.c 中的函数和变量。但是,如果没有 CMake,一切都可以正常工作:

swig -python example.i
gcc -c example.c example_wrap.c -I<path_to_directory_containing_Python.h>
gcc -shared example.o example_wrap.o -o _example.so

我尝试将这些行与

的详细输出进行比较
make 

但后者比前者复杂得多。

编辑:这表明 CMake 有效地调用了 swig:

 /usr/local/Cellar/cmake/3.15.3/bin/cmake -E env SWIG_LIB=/usr/local/Cellar/swig/4.0.1/share/swig/4.0.1 /usr/local/bin/swig -python -outdir /Users/myself/forexample/swig/build -interface _example -I/usr/local/Frameworks/Python.framework/Versions/3.7/include/python3.7m -I/Users/myself/forexample/swig -o /Users/myself/forexample/swig/build/CMakeFiles/_example.dir/examplePYTHON_wrap.c /Users/myself/forexample/swig/example.i

最佳答案

您的 CMake 似乎根本没有引用 example.c;这就是为什么无法找到所有函数定义的原因。错误:

Undefined symbols for architecture x86_64:

red herring因为实际上并不存在架构不匹配;这些符号根本没有被定义。尝试修改您的 CMake 以包含 C 代码的库,然后将其链接到您的 SWIG 库。像这样的事情应该有助于解决错误:

cmake_minimum_required(VERSION 3.8)

cmake_policy(SET CMP0078 OLD)
cmake_policy(SET CMP0086 OLD)

project(example C)

find_package(PythonLibs 3 REQUIRED)
find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} REQUIRED)

message(STATUS "Python executable is ${PYTHON_EXECUTABLE}")
message("PYTHONLIBS_VERSION_STRING: ${PYTHONLIBS_VERSION_STRING}")

include_directories(${PYTHON_INCLUDE_PATH} ${CMAKE_SOURCE_DIR})

find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

message("CMAKE_SWIG_FLAGS: ${CMAKE_SWIG_FLAGS}")

add_library(MyCLib example.c)

swig_add_library(example TYPE SHARED LANGUAGE python SOURCES example.i)
swig_link_libraries(example MyCLib ${PYTHON_LIBRARIES})

关于python - add_swig_library 的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58646496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com