sdl2_gfx 编译成功但在使用 pysdl2 导入时出错

sdl2_gfx compiles successfully but gives error when imported with pysdl2

我写了一个小的 CMake 脚本来在 Anaconda Python 环境中在 Windows(最后的边界)上构建 SDL2_gfx。我对 SDL2 和 pysdl2 做了同样的事情,它们工作正常。但是,sdl2_gfx 模块在导入时打印如下错误信息:

>>> import sdl2
>>> import sdl2.sdlgfx
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda3\lib\site-packages\sdl2\sdlgfx.py", line 63, in <module>
    SDL_initFramerate = _bind("SDL_initFramerate", [POINTER(FPSManager)])
  File "C:\Anaconda3\lib\site-packages\sdl2\dll.py", line 87, in bind_function
    (funcname, self._dll))
ValueError: could not find function 'SDL_initFramerate' in <CDLL 'C:\Anaconda3\Library\bin\SDL2_gfx.dll', handle 7ffb05e30000 at 0x1f703f544a8>

我不知道去哪里寻找这个问题的根源,因为没有编译错误!

为了完整起见,这是我的 CMakeLists.txt:

# Written by Daniel Schreij on 1 Nov 2016
# This procedure is configured to work in an Anaconda build environment (contiuum.io)
# In this environment, it should be called like 
# 
#    %LIBRARY_BIN%\cmake -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX:PATH="%LIBRARY_PREFIX%" ..
# 
# in a Windows build script (bld.bat) to work correctly

set(PROJECT_NAME "SDL2_gfx")
project(${PROJECT_NAME})
CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0)

set(IS_LIBRARY true)
set(CMAKE_INCLUDE_CURRENT_DIR TRUE)

include(GenerateExportHeader)
message(STATUS "Search prefix: ${CMAKE_PREFIX_PATH}")

FIND_PATH(SDL2_INCLUDE_DIR
    SDL.h
    PATH_SUFFIXES include/SDL2 include
)

FIND_LIBRARY(SDL2_LIBRARIES
    NAMES SDL2
    PATH_SUFFIXES lib64 lib lib/x64 lib/x86
)

message(STATUS "Include dir: ${SDL2_INCLUDE_DIR}")
message(STATUS "Library location: ${SDL2_LIBRARIES}")

set(SRCS
    SDL2_framerate.c
    SDL2_gfxPrimitives.c
    SDL2_imageFilter.c
    SDL2_rotozoom.c
)

include_directories(${SDL2_INCLUDE_DIR})

# Dynamic libraries
add_library(${PROJECT_NAME} SHARED ${SRCS})
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})

# Static library
add_library(${PROJECT_NAME}-static STATIC ${SRCS})
set_target_properties(${PROJECT_NAME}-static PROPERTIES OUTPUT_NAME "SDL2")
target_link_libraries(${PROJECT_NAME}-static ${SDL2_LIBRARIES})

##### Installation targets #####
install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-static
  LIBRARY DESTINATION "lib${LIB_SUFFIX}"
  ARCHIVE DESTINATION "lib${LIB_SUFFIX}"
  RUNTIME DESTINATION bin)

file(GLOB INCLUDE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
install(FILES ${INCLUDE_FILES} DESTINATION include/SDL2)

这里是编译过程的输出:

BUILD START: sdl2_gfx-1.0.1-py35_vc14_0
    (actual version deferred until further download or env creation)
updating index in: C:\Anaconda3\conda-bld\win-64
updating index in: C:\Anaconda3\conda-bld\noarch
Using Anaconda API: https://api.anaconda.org

The following NEW packages will be INSTALLED:

    certifi:        2016.8.31-py35_0 conda-forge
    cmake:          3.6.2-0          conda-forge
    pip:            8.1.2-py35_0     conda-forge
    python:         3.5.2-2          conda-forge
    sdl2:           2.0.4-vc14_0     local       [vc14]
    setuptools:     26.1.1-py35_0    conda-forge
    vs2015_runtime: 14.0.25420-0     conda-forge
    wheel:          0.29.0-py35_0    conda-forge
    wincertstore:   0.2-py35_0       conda-forge

[... ommitted for brevity ...]

(C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\_b_env) C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\build>C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\_b_env\Library\bin\cmake -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX:PATH="C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\_b_env\Library" -DCMAKE_BUILD_TYPE:STRING=Release ..
-- The C compiler identification is MSVC 19.0.23506.0
-- The CXX compiler identification is MSVC 19.0.23506.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Search prefix:
-- Include dir: C:/Anaconda3/conda-bld/sdl2_gfx_1478036776627/_b_env/Library/include/SDL2
-- Library location: C:/Anaconda3/conda-bld/sdl2_gfx_1478036776627/_b_env/Library/lib/SDL2.lib
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Anaconda3/conda-bld/sdl2_gfx_1478036776627/work/SDL2_gfx-1.0.1/build

(C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\_b_env) C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\build>if errorlevel 1 exit 1

(C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\_b_env) C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\build>nmake

Microsoft (R) Program Maintenance Utility Version 14.00.23506.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Scanning dependencies of target SDL2_gfx
[ 10%] Building C object CMakeFiles/SDL2_gfx.dir/SDL2_framerate.c.obj
SDL2_framerate.c
[ 20%] Building C object CMakeFiles/SDL2_gfx.dir/SDL2_gfxPrimitives.c.obj
SDL2_gfxPrimitives.c
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(1748): warning C4028: formal parameter 1 different from declaration
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(1855): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(1856): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(1932): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(3798): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(3799): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4177): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4179): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4181): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4184): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4186): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4188): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
[ 30%] Building C object CMakeFiles/SDL2_gfx.dir/SDL2_imageFilter.c.obj
SDL2_imageFilter.c
[ 40%] Building C object CMakeFiles/SDL2_gfx.dir/SDL2_rotozoom.c.obj
SDL2_rotozoom.c
[ 50%] Linking C shared library SDL2_gfx.dll
[ 50%] Built target SDL2_gfx
Scanning dependencies of target SDL2_gfx-static
[ 60%] Building C object CMakeFiles/SDL2_gfx-static.dir/SDL2_framerate.c.obj
SDL2_framerate.c
[ 70%] Building C object CMakeFiles/SDL2_gfx-static.dir/SDL2_gfxPrimitives.c.obj
SDL2_gfxPrimitives.c
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(1748): warning C4028: formal parameter 1 different from declaration
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(1855): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(1856): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(1932): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(3798): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(3799): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4177): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4179): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4181): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4184): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4186): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\SDL2_gfxPrimitives.c(4188): warning C4244: 'function': conversion from 'double' to 'float', possible loss of data
[ 80%] Building C object CMakeFiles/SDL2_gfx-static.dir/SDL2_imageFilter.c.obj
SDL2_imageFilter.c
[ 90%] Building C object CMakeFiles/SDL2_gfx-static.dir/SDL2_rotozoom.c.obj
SDL2_rotozoom.c
[100%] Linking C static library SDL2.lib
[100%] Built target SDL2_gfx-static

(C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\_b_env) C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\build>if errorlevel 1 exit 1

(C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\_b_env) C:\Anaconda3\conda-bld\sdl2_gfx_1478036776627\work\SDL2_gfx-1.0.1\build>nmake install

Microsoft (R) Program Maintenance Utility Version 14.00.23506.0
Copyright (C) Microsoft Corporation.  All rights reserved.

[ 50%] Built target SDL2_gfx
[100%] Built target SDL2_gfx-static
Install the project...
-- Install configuration: "Release"
-- Installing: C:/Anaconda3/conda-bld/sdl2_gfx_1478036776627/_b_env/Library/bin/SDL2_gfx.dll
-- Installing: C:/Anaconda3/conda-bld/sdl2_gfx_1478036776627/_b_env/Library/lib/SDL2.lib
-- Installing: C:/Anaconda3/conda-bld/sdl2_gfx_1478036776627/_b_env/Library/include/SDL2/SDL2_framerate.h
-- Installing: C:/Anaconda3/conda-bld/sdl2_gfx_1478036776627/_b_env/Library/include/SDL2/SDL2_gfxPrimitives.h
-- Installing: C:/Anaconda3/conda-bld/sdl2_gfx_1478036776627/_b_env/Library/include/SDL2/SDL2_gfxPrimitives_font.h
-- Installing: C:/Anaconda3/conda-bld/sdl2_gfx_1478036776627/_b_env/Library/include/SDL2/SDL2_imageFilter.h
-- Installing: C:/Anaconda3/conda-bld/sdl2_gfx_1478036776627/_b_env/Library/include/SDL2/SDL2_rotozoom.h

据我所知,没有异常?

在 windows 上导出的符号必须明确标记。 SDL_gfx expects DLL_EXPORT 构建 dll 时要定义的标志。

添加

set_target_properties(${PROJECT_NAME} PROPERTIES DEFINE_SYMBOL "DLL_EXPORT")

到你的 cmake 文件。