使用CMake构建swig无法链接(有时)

时间:2017-02-13 14:59:02

标签: python c++ cmake swig

我有一个简单的项目,我希望与SWIG。该目录如下所示:

├── CMakeLists.txt
├── README.md
└── src
    ├── lib
    │   ├── CMakeLists.txt
    │   ├── foo.cpp
    │   └── foo.h
    ├── main
    │   ├── CMakeLists.txt
    │   └── main.cpp
    ├── swig
    │   ├── CMakeLists.txt
    │   ├── example.i
    │   └── python
    │       └── example-driver.py
    └── swig-new
        ├── CMakeLists.txt
        ├── newexample.i
        └── python
            └── newexample-driver.py

我创建了两个不同的接口:example和otherexample,是模块的唯一区别。然后我有主要的CMakeLists构建不同的子文件夹

cmake_minimum_required(VERSION 2.6)
project(swig-example)

# Options for shared library. On or Off.
set(BUILD_SHARED_LIBS YES CACHE BOOL "Enable shared libraries.")
set(LIB_TYPE STATIC)
if(BUILD_SHARED_LIBS)
  set(LIB_TYPE SHARED)
endif()

include_directories("src/lib")
add_subdirectory(src/lib)
add_subdirectory(src/main)
add_subdirectory(src/swig)
add_subdirectory(src/swig-new)

构建

时出现以下错误
$ make
[ 78%] Swig source
Scanning dependencies of target _pynewexample
[ 95%] Building CXX object src/swig-new/CMakeFiles/_pynewexample.dir/__/__/python/newexamplePYTHON_wrap.cxx.o
[100%] Linking CXX shared module ../../python/_pynewexample.so
/usr/bin/ld: cannot find -lnewexample
collect2: error: ld returned 1 exit status
src/swig-new/CMakeFiles/_pynewexample.dir/build.make:103: recipe for target 'python/_pynewexample.so' failed
make[2]: *** [python/_pynewexample.so] Error 1
CMakeFiles/Makefile2:476: recipe for target 'src/swig-new/CMakeFiles/_pynewexample.dir/all' failed
make[1]: *** [src/swig-new/CMakeFiles/_pynewexample.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

如果我看一下构建不同子目录的方式,我可以看到

$ more src/swig/CMakeFiles/_pyexample.dir/link.txt
/usr/bin/c++  -fPIC   -shared  -o ../../python/_pyexample.so CMakeFiles/_pyexample.dir/__/__/python/e
xamplePYTHON_wrap.cxx.o ../lib/libexample.so -lpython2.7 -Wl,-rpath,/home/segonpin/codesother/build/s
rc/lib

它有效。但对于新的例子,它并不是

$ more src/swig-new/CMakeFiles/_pynewexample.dir/link.txt
/usr/bin/c++  -fPIC   -shared  -o ../../python/_pynewexample.so CMakeFiles/_pynewexample.dir/__/__/py
thon/newexamplePYTHON_wrap.cxx.o -lnewexample -lpython2.7

有关为什么第二个模块试图以不同方式查找库的任何提示?

编辑feb 13,16:01

如果有必要,newexample的CMakeFiles.txt是

FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})
FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
SET(CMAKE_SWIG_FLAGS "")

set (CMAKE_CXX_STANDARD 11)

# we want the python stuff to all land in the same place
SET(CMAKE_SWIG_OUTDIR "${CMAKE_BINARY_DIR}/python")
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python")

SET_SOURCE_FILES_PROPERTIES(newexample.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(pynewexample python newexample.i)
SWIG_LINK_LIBRARIES(pynewexample newexample ${PYTHON_LIBRARIES})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

0 个答案:

没有答案