具有add_swig_library的未定义符号

问题描述 投票:0回答:1

我尝试用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编写SWIG的简单用法示例,但收到以下错误消息:架构x86_64的未定义符号:“ _ My_variable”,引用自:_Swig_var_My_variable_get ...

python c gcc cmake swig
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.