add_swig_library 的未定义符号

Undefined symbols with add_swig_library

我尝试使用 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 代码库,然后 link 将其添加到您的 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})