如何在 windows 中使用柯南的 SDL2

How to use conan's SDL2 in windows

我刚开始使用 windows 中的 conan 进行编译,我在使用 SDL2 方面遇到困难。

Here 有一个使用 conan 的 hello world,它在 linux 中编译并运行良好:

git clone https://bitbucket.org/mutcoll/conan-sdl2-hello-world.git
cd conan-sdl2-hello-world
conan install --build=missing
cmake -G "Unix Makefiles"
make
./bin/test_sdl2

但在 windows 中(使用 git 作为 windows bash,不知道这是否重要)针对 SDL2 的 link 失败并出现错误像这样:

testspriteminimal.obj : error LNK2019: unresolved external symbol _SDL_GetError referenced in function "int __cdecl LoadSprite(char *,struct SDL_Renderer *)" (?LoadSprite@@YAHPADPAUSDL_Renderer@@@Z) [C:\Users\josemi\Documents\conan\conan-sdl2-hello-world\test_sdl2.vcxproj]

问题是:如何才能获得成功的 linking?最好与共享 (SDL2.dll)。提前感谢大家,下面有一些细节, 如果有人需要更多细节,请询问(例如,将整个编译放在 pastebin 中)。

重现错误

这是我的CMakeLists.txt

project(SDL2_test)
cmake_minimum_required(VERSION 2.8)

include(conanbuildinfo.cmake)
conan_basic_setup()


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")    
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # I think I should do this only if gcc

add_executable(test_sdl2 testspriteminimal.cpp)
target_link_libraries(test_sdl2 ${CONAN_LIBS})

和我的 conanfile.txt:

[requires]
SDL2/2.0.3@lasote/stable

[generators]
cmake

这些是我的设置我的 .conan/conan.conf(柯南安装正在使用的设置):

[settings_defaults]
arch=x86_64
build_type=Release
compiler=Visual Studio
compiler.runtime=MD
compiler.version=14
os=Windows

我在克隆后使用这些行:

conan install --build=missing  # this works fine, compiles SDL2.dll, SDL2.lib, SDL2main.lib
cmake -G "Visual Studio 14"
cmake --build .    # this fails

最后一个命令执行此操作失败(即使我可以看到它正在使用 SDL2.lib):

Link:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\link.exe /ERRORREPORT:QUEUE /OUT:"C:\Users\josemi\Documents\conan\conan-sdl2-hello-world\bin\test_sdl2.exe" /INCREMENTAL /NOLOGO /LIBPATH:C:/Users/josemi/.conan/data/SDL2/2.0.3/lasote/stable/package/c85f9b402dd4d46acdf074e1c63b768a41181d7a/lib /LIBPATH:C:/Users/josemi/.conan/data/SDL2/2.0.3/lasote/stable/package/c85f9b402dd4d46acdf074e1c63b768a41181d7a/lib/Debug kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib SDL2.lib SDL2main.lib user32.lib gdi32.lib winmm.lib imm32.lib ole32.lib oleaut32.lib version.lib uuid.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:/Users/josemi/Documents/conan/conan-sdl2-hello-world/bin/test_sdl2.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/Users/josemi/Documents/conan/conan-sdl2-hello-world/lib/test_sdl2.lib" /MACHINE:X86 /SAFESEH  /machine:X86 test_sdl2.dir\Debug\testspriteminimal.obj

更多警告,可能无关

为 x86 构建的 sdl2?

"C:\Users\josemi\Documents\conan\conan-sdl2-hello-world\ALL_BUILD.vcxproj" (destino predeterminado) (1) ->
"C:\Users\josemi\Documents\conan\conan-sdl2-hello-world\test_sdl2.vcxproj" (destino predeterminado) (3) ->

(Link destino) ->
C:/Users/josemi/.conan/data/SDL2/2.0.3/lasote/stable/package/c85f9b402dd4d46acdf074e1c63b768a41181d7a/lib\SDL2.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'X86' [C:\Users\josemi\Documents\conan\conan-sdl2-hello-world\test_sdl2.vcxproj]
C:/Users/josemi/.conan/data/SDL2/2.0.3/lasote/stable/package/c85f9b402dd4d46acdf074e1c63b768a41181d7a/lib\SDL2main.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'X86' [C:\Users\josemi\Documents\conan\conan-sdl2-hello-world\test_sdl2.vcxproj]

像下一个这样的约 200 个警告,使用 C 函数:

C:\Program Files (x86)\Windows Kits\Include.0.10240.0\ucrt\stdlib.h(354): warning C4514: 'abs': unreferenced inline function has been removed [C:\Users\josemi\Documents\conan\conan-sdl2-hello-world\test_sdl2.vcxproj]

好的,回答我自己的问题,正如 IInspectable 指出的那样,问题是我正在以 32 位构建我的可执行文件,并与 64 位 SDL2 链接。告诉柯南做64位可执行文件的方法是:

cmake -G "Visual Studio 14 Win64"

而不是像我之前那样 cmake -G "Visual Studio 14"

示例:http://docs.conan.io/en/latest/getting_started.html#building-the-timer-example