简体   繁体   English

SWIG Cmake DLL链接不一致

[英]SWIG Cmake inconsistent DLL linkage

I am using CMake to build a c++ project to a DLL on Windows. 我正在使用CMake在Windows上将C ++项目构建为DLL。 I then wish to wrap this for python using SWIG, but in doing so I am receiving warnings about 'Inconsistent DLL linkage'. 然后,我希望使用SWIG将其包装为python,但是这样做时,我收到有关“ DLL链接不一致”的警告。 I gather this refers to incorrect usage of dllexport/dllimport and I need to specify a #define for SWIG? 我收集到这是指dllexport / dllimport的用法不正确,我需要为SWIG指定一个#define吗? How can I do this in CMake? 如何在CMake中做到这一点?

My C++ library is built like so in CMake: 我的C ++库在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). (它使用CMAke生成导出头)。

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的部分:

### 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: 这是我为完全避免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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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