'main'的多重定义什么时候包括SDL.h

时间:2018-02-17 11:59:27

标签: c++ cmake sdl-2 clion

经过2天的研究,我终于将SDL库添加到我的" CLion / Cmake / Windows" -Project。

我做的是:

  1. 在CLion中创建了一个名为" sdl_test_vc"
  2. 的新项目
  3. 我从https://www.libsdl.org/download-2.0.php下载了SDL2-devel-2.0.7-VC.zip并将其解压缩到我的项目文件夹中。
  4. 我在项目文件夹中添加了一个子目录" cmake"使用https://github.com/tcbrindle/sdl2-cmake-scripts
  5. 中的FindSDL2.cmake

    这是我的Cmake文件:

    cmake_minimum_required(VERSION 3.8)
    project(sdl_test_vc)
    
    set(CMAKE_CXX_STANDARD 11)
    
    set(SOURCE_FILES main.cpp)
    add_executable(sdl_test_vc ${SOURCE_FILES})
    
    # SDL2
    set(SDL2_PATH "${sdl_test_vc_SOURCE_DIR}/SDL2-2.0.7")
    
    # .cmake for finding SDL2: https://github.com/tcbrindle/sdl2-cmake-scripts
    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${sdl_test_vc_SOURCE_DIR}/cmake")
    
    find_package(SDL2 REQUIRED)
    # find_package(SDL2_Image REQUIRED)
    # find_package(SDL2_ttf REQUIRED)
    
    include_directories(${SDL2_INCLUDE_DIR})
    # ${SDL2_IMAGE_INCLUDE_DIR}
    # ${SDL2_TTF_INCLUDE_DIR})
    
    target_link_libraries(sdl_test_vc ${SDL2_LIBRARY})
    # ${SDL2_IMAGE_LIBRARIES}
    

    之后我创建了以下main.cpp

    #include <iostream>
    #include <SDL.h>
    
    int main(int argc, char* argv[]) {
    // Start SDL2
    SDL_Init(SDL_INIT_EVERYTHING);
    
    // Create a Window in the middle of the screen
    SDL_Window *window = 0;
    
    window = SDL_CreateWindow("Hello World!",
                              SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED,
                              640, 480,
                              SDL_WINDOW_SHOWN);
    
    // Delay so that we can see the window appear
    SDL_Delay(2000);
    
    // Cleanup and Quit
    SDL_DestroyWindow(window);
    SDL_Quit();
    
    return 0;
    }
    

    构建项目时,出现以下错误。

    ../SDL2-2.0.7/lib/x64/SDL2main.lib(x64/Release/SDL_windows_main.obj):(.text[main]+0x0): multiple definition of `main'
    C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x0): first defined here
    collect2.exe: error: ld returned 1 exit status
    mingw32-make.exe[3]: *** [CMakeFiles\sdl_test_vc.dir\build.make:99: sdl_test_vc.exe] Error 1
    mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:67: CMakeFiles/sdl_test_vc.dir/all] Error 2
    mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:79: CMakeFiles/sdl_test_vc.dir/rule] Error 2
    mingw32-make.exe: *** [Makefile:117: sdl_test_vc] Error 2
    

    任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

您正在使用SDLmain库,它定义了一个与平台相关的入口点,解析命令行参数(将它们转换为utf-8),然后调用SDL应用程序入口点。因此,在您的代码而不是main()中,您应该定义函数

int SDL_main(int argc, char *argv[])

请注意,如果定义了SDL_MAIN_NEEDEDSDL_MAIN_AVAILABLE,则SDL会自动将main重新定义为SDL_main,因此您无需更改main名称代码。