Swig parameter -outdir does not include the .so file
I have a small project where I am using CMake system to create Python module from C ++ files. In the CMakeLists.txt file I have Swig:
# only the Swig part here
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_PATH})
set(CMAKE_SWIG_OUTDIR ${PROJECT_BINARY_DIR}/../lib/Foo)
SET_SOURCE_FILES_PROPERTIES(swig/interface.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties(swig/interface.i SWIG_FLAGS "-includeall;-c++;-shadow")
swig_add_module(Foo python swig/interface.i code/foo.cpp)
swig_link_libraries(Foo foolib ${PYTHON_LIBRARIES})
My first question is, why aren't Foo.py and _Foo.so created at the location given CMAKE_SWIG_OUTDIR
? Only a .py file is generated in this directory. Is this a bug in the CMake UseSWIG.cmake file? The .so file is still in PROJECT_BINARY_DIR
. As a result, I cannot load the module in Python unless the variable CMAKE_SWIG_OUTDIR
is in an environment variable PYTHON_PATH
. So to solve this problem I could:
- Add the directory
PROJECT_BINARY_DIR
toPYTHON_PATH
. - Copy the .so file to
CMAKE_SWIG_OUTDIR
or create a symbolic link using the CMake system. - Don't set the variable
CMAKE_SWIG_OUTDIR
so that everything will be created inPROJECT_BINARY_DIR
and only add that location toPYTHON_PATH
.
But none of them seems to be a logical task as it CMAKE_SWIG_OUTDIR
should be used to output .py and .so files. Did I miss something?
source to share
I'm not sure why CMAKE_SWIG_OUTDIR doesn't affect the location of the .so file. However, I can tell you about an easier and more straightforward way of specifying where the .so file should be generated.
After yours swig_add_module(Foo ...)
, a CMake target named _Foo
. Then you can edit the target properties and change where its library (.so) will be generated - set_target_properties(.. LIBRARY_OUTPUT_DIRECTORY <so_file_output_dir>)
.
So, in your code, just add the line set_target_properties(..)
shown below:
...
swig_add_module(Foo python swig/interface.i code/foo.cpp)
set_target_properties(_Foo PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SWIG_OUTDIR})
swig_link_libraries(Foo foolib ${PYTHON_LIBRARIES})
...
source to share