gpt4 book ai didi

python - SWIG Cmake 不一致的 DLL 链接

转载 作者:行者123 更新时间:2023-11-28 05:33:06 36 4
gpt4 key购买 nike

我正在使用 CMake 在 Windows 上将 C++ 项目构建为 DLL。然后我希望使用 SWIG 为 python 包装它,但在这样做时我收到了关于“不一致的 DLL 链接”的警告。我收集到这指的是 dllexport/dllimport 的不正确使用,我需要为 SWIG 指定 #define 吗?我如何在 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
)

(它使用 CMake 生成导出 header )。

我正在使用这个库构建一个运行良好的测试可执行文件:

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")

最后,关于 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)

最佳答案

以下是我为完全避免 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()

关于python - SWIG Cmake 不一致的 DLL 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38971681/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com