简体   繁体   中英

SWIG Cmake inconsistent DLL linkage

I am using CMake to build a c++ project to a DLL on Windows. I then wish to wrap this for python using SWIG, but in doing so I am receiving warnings about 'Inconsistent DLL linkage'. I gather this refers to incorrect usage of dllexport/dllimport and I need to specify a #define for SWIG? How can I do this in CMake?

My C++ library is built like so in CMake:

# glob all the sources
file(GLOB SOURCES "src/core/*.cpp")

add_library(galgcore SHARED ${SOURCES})
target_link_libraries(galgcore ${GDAL_LIBRARY})

GENERATE_EXPORT_HEADER( galgcore
             BASE_NAME GeoAlg
             EXPORT_MACRO_NAME GALGCORE_DLL
             EXPORT_FILE_NAME ${PROJECT_SOURCE_DIR}/src/core/core_exp.h
             STATIC_DEFINE GeoAlg_BUILT_AS_STATIC
)

(It is using CMAke to generate the export header).

I am using this library to build a test executable which works well:

include(FindGTest)
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

# If *nix, pthread must be specified *after* the googletest libs
if(WIN32)
  set (PTHREAD "")
else(WIN32)
  set (PTHREAD pthread)
endif(WIN32)

add_executable(galgtest test/galg_unittest.cpp)
target_link_libraries(galgtest ${GTEST_BOTH_LIBRARIES} galgcore galgfunc ${PTHREAD})
add_test(AllTestsInGalg galgtest "${CMAKE_CURRENT_LIST_DIR}/test/10_12_1.tif")

Finally, the section dealing with swig:

### SWIG
# This generates the python bindings
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_PATH})

set(CMAKE_SWIG_FLAGS "-Wall")
set_source_files_properties("${PROJECT_SOURCE_DIR}/python/galg.i" PROPERTIES CPLUSPLUS ON)
set_property(SOURCE "${PROJECT_SOURCE_DIR}/python/galg.i" PROPERTY SWIG_FLAGS "-builtin")
SWIG_ADD_MODULE(galg python "${PROJECT_SOURCE_DIR}/python/galg.i" ${SOURCES})
SWIG_LINK_LIBRARIES(galg ${PYTHON_LIBRARIES} galgcore)

Here is the full recipe to what I do to completely avoid warnings on Windows:

# We don't have Python with debug information installed
if (MSVC)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4127")
  add_definitions(-DSWIG_PYTHON_INTERPRETER_NO_DEBUG)
endif()

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

find_package(PythonLibs REQUIRED)

include_directories(${PYTHON_INCLUDE_PATH})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR}) # generated files

if (MSVC)
  set(CMAKE_SWIG_FLAGS "-D_SWIG_WIN32")
endif()

set_source_files_properties(swig_project.i PROPERTIES CPLUSPLUS ON)
swig_add_module(swig_project python swig_project.i ${swig_project_HEADERS})

if (MSVC)
  # Potential uninitialized variable in SWIG_AsVal_
  set_source_files_properties( ${swig_generated_file_fullname} PROPERTIES COMPILE_FLAGS "/wd4701")
endif()

if (WIN32)
  # Allow to debug under windows, if debug versions of Python are missing
  string(REPLACE "_d" "" PYTHON_LIBRARIES "${PYTHON_LIBRARIES}")
endif()

swig_link_libraries(swig_project project ${PYTHON_LIBRARIES})

if (WIN32)
  # pyconfig.h is not autogenerated on Windows. To avoid warnings, we
  # add a compiler directive
  get_directory_property(DirDefs COMPILE_DEFINITIONS )
  set_target_properties(_swig_project PROPERTIES
  COMPILE_DEFINITIONS "${DirDefs};HAVE_ROUND")
endif()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM