FindSWIG.cmake无法找到正确的标头/库对

时间:2015-01-26 14:15:13

标签: python cmake swig

我在CMakeLists.txt文件中有以下代码:

# find Python
find_package(PythonInterp)

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

find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_PATH})

message(STATUS "PYTHON_INCLUDE_PATH: ${PYTHON_INCLUDE_PATH}")
message(STATUS "PYTHON_LIBRARIES:    ${PYTHON_LIBRARIES}")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/core)        

SET_SOURCE_FILES_PROPERTIES(swig/core.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties(swig/core.i SWIG_FLAGS "-includeall;-c++;-shadow")
swig_add_module(core python swig/core.i core/foo.cpp)
swig_link_libraries(core hybrida_core ${PYTHON_LIBRARIES})

当我看到输出时,这给了我:

-- Found PythonInterp: /Users/aaragon/.virtualenvs/pydev/bin/python (found version "3.4.1") 
-- PYTHON_EXECUTABLE: /Users/aaragon/.virtualenvs/pydev/bin/python
-- Python version: 3.4.1
-- Found SWIG: /usr/local/bin/swig (found version "3.0.3") 
-- Found PythonLibs: /usr/lib/libpython3.4.dylib (found version "2.7.6") 
-- PYTHON_INCLUDE_PATH: /System/Library/Frameworks/Python.framework/Headers
-- PYTHON_LIBRARIES:    /usr/lib/libpython3.4.dylib

因为它发现版本为2.7.6,所以找到了PythonLibs的错误。这导致后来出现大量的链接错误:

Linking CXX shared module _core.so
Undefined symbols for architecture x86_64:
  "_PyClass_Type", referenced from:
      SwigPyClientData_New(_object*) in corePYTHON_wrap.cxx.o
  "_PyInstance_NewRaw", referenced from:
      SWIG_Python_NewShadowInstance(SwigPyClientData*, _object*) in corePYTHON_wrap.cxx.o
  "_PyInstance_Type", referenced from:
      SWIG_Python_GetSwigThis(_object*) in corePYTHON_wrap.cxx.o
  "_PyInt_AsLong", referenced from:
      SWIG_AsVal_long(_object*, long*) in corePYTHON_wrap.cxx.o
      SWIG_AsVal_unsigned_SS_long(_object*, unsigned long*) in corePYTHON_wrap.cxx.o
      SWIG_AsVal_double(_object*, double*) in corePYTHON_wrap.cxx.o
  "_PyInt_FromLong", referenced from:
      SWIG_From_int(int) in corePYTHON_wrap.cxx.o
  "_PyString_AsString", referenced from:
      SWIG_Python_str_AsChar(_object*) in corePYTHON_wrap.cxx.o
  "_PyString_AsStringAndSize", referenced from:
      SWIG_AsCharPtrAndSize(_object*, char**, unsigned long*, int*) in corePYTHON_wrap.cxx.o
  "_PyString_ConcatAndDel", referenced from:
      SwigPyObject_repr(SwigPyObject*) in corePYTHON_wrap.cxx.o
      swig_varlink_str(swig_varlinkobject*) in corePYTHON_wrap.cxx.o
  "_PyString_Format", referenced from:
      SwigPyObject_format(char const*, SwigPyObject*) in corePYTHON_wrap.cxx.o
  "_PyString_FromFormat", referenced from:
      SwigPyObject_repr(SwigPyObject*) in corePYTHON_wrap.cxx.o
      SwigPyPacked_repr(SwigPyPacked*) in corePYTHON_wrap.cxx.o
      SwigPyPacked_str(SwigPyPacked*) in corePYTHON_wrap.cxx.o
  "_PyString_FromString", referenced from:
      SWIG_Python_str_FromChar(char const*) in corePYTHON_wrap.cxx.o
      swig_varlink_repr(swig_varlinkobject*) in corePYTHON_wrap.cxx.o
      swig_varlink_str(swig_varlinkobject*) in corePYTHON_wrap.cxx.o
  "_PyString_FromStringAndSize", referenced from:
      SWIG_FromCharPtrAndSize(char const*, unsigned long) in corePYTHON_wrap.cxx.o
  "_Py_InitModule4_64", referenced from:
      _init_core in corePYTHON_wrap.cxx.o
      SWIG_Python_SetModule(swig_module_info*) in corePYTHON_wrap.cxx.o
  "__PyInstance_Lookup", referenced from:
      SWIG_Python_GetSwigThis(_object*) in corePYTHON_wrap.cxx.o
ld: symbol(s) not found for architecture x86_64

如果我硬编码以下变量:

set (PYTHON_INCLUDE_DIR /Library/Frameworks/Python.framework/Versions/3.4/Headers)

然后我就能正确编译和链接了。 cmake无法为Python确定正确的头文件/库组合吗?

1 个答案:

答案 0 :(得分:0)

显然,这是我在使用CMake 3.1.0时遇到的一个问题,但它已在较新版本的CMake(3.1.1)中得到解决。