我想使用 SDL2 库来捕获游戏手柄输入,因此我编写了一个简单的测试程序,但它没有执行任何操作,没有错误消息,没有构建错误,什么都没有:
源代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h> //for sleep
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include "SDL.h"
int main(int argc, char *argv[])
{
/* SDL test */
if(SDL_Init(SDL_INIT_GAMECONTROLLER) <0)
{
perror("Failed to initialize SDL2 library\n");
return EXIT_FAILURE;
}
else printf("SDL2 initialized\n");
printf("Test message");
return EXIT_SUCCESS;
}
我使用了 VScode 和 CMake,CMakeLists.txt 文件如下所示:
cmake_minimum_required(VERSION 3.30.1)
project(sandbox_project VERSION 1.0 LANGUAGES C)
add_executable(sandbox_project WIN32 ${SOURCES})
# define the source files for the project
target_sources(sandbox_project PRIVATE
src/sandbox_main.c
src/ring_buffer/ring_buff.h
src/ring_buffer/ring_buff.c
)
# add the path of the cmake configuration files
list(APPEND CMAKE_PREFIX_PATH "D:/Everyngineering/programming/general_c_cpp/libraries/SDL2-devel-2.30.7-mingw/SDL2-2.30.7/cmake")
find_package(SDL2 REQUIRED)
# include directories
target_include_directories(sandbox_project
PRIVATE ${SOURCE_DIR}/src
PRIVATE ${SOURCE_DIR}/src/ring_buffer
PRIVATE ${SDL2_INCLUDE_DIRS}
)
# include libraries
target_link_libraries(sandbox_project PRIVATE ${SDL2_LIBRARIES})
# specify C standard
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)
# specifying the executable output
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin/)
SDL2 库不是我从源代码构建的,我已经下载了带有预编译文件的开发人员库。
许多类似的帖子建议我应该将 SDL2.dll 复制到可执行文件旁边,我这样做了,但什么也没发生。这是项目结构:
有人可以指出我解决这个问题的正确方向吗?
结果证明,当 SDL2 运行时,简单的 printf 不起作用,这就是为什么我从控制台看不到任何东西。