无法在 Windows 上使用附近的 SDL2 库和 cmake 构建项目[重复]

问题描述 投票:0回答:1

我想使用 cmake 和 SDL2 库构建我的项目,它们位于项目文件附近。我已经成功链接 SDL2,但无法对 SDL2_image 和 SDL2_ttf 执行相同操作。我花了很多时间,但不明白可能出了什么问题并解决这个问题。所有这些都是我在 Windows 10 上完成的。

我收到这些错误:

baseClasses.obj : error LNK2019: unresolved external symbol IMG_LoadTexture referenced in function "public: bool __cdecl imageDrawable::loadImage(char *,int,int,int,int)" (?loadImage@imageDrawable@@QEAA_NPEADHHHH@Z) [C:\Users\me\Desktop\C++\another_game\cmake\ga
me.vcxproj]
map.obj : error LNK2019: unresolved external symbol IMG_Load referenced in function "public: bool __cdecl tilemap::loadImage(char *,int,int)" (?loadImage@tilemap@@QEAA_NPEADHH@Z) [C:\Users\me\Desktop\C++\another_game\cmake\game.vcxproj]
C:\Users\me\Desktop\C++\another_game\build\Debug\game.exe : fatal error LNK1120: 2 unresolved externals [C:\Users\me\Desktop\C++\another_game\cmake\game.vcxproj]

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)

project(game)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build)

set(SDL2_PATH "${CMAKE_SOURCE_DIR}/libs/SDL2-2.28.5/x86_64-w64-mingw32/bin")
set(SDL2_IMAGE_PATH "${CMAKE_SOURCE_DIR}/libs/SDL2_image-2.8.2/x86_64-w64-mingw32/bin")
set(SDL2_TTF_PATH "${CMAKE_SOURCE_DIR}/libs/SDL2_ttf-2.22.0/x86_64-w64-mingw32/bin")

set(SDL2_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/libs/SDL2-2.28.5/x86_64-w64-mingw32/include")
set(SDL2_IMAGE_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/libs/SDL2_image-2.8.2/x86_64-w64-mingw32/include")
set(SDL2_TTF_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/libs/SDL2_ttf-2.22.0/x86_64-w64-mingw32/include")

set(SDL2_DIR "${CMAKE_SOURCE_DIR}/libs/SDL2-2.28.5/cmake")
set(SDL2_image_DIR "${CMAKE_SOURCE_DIR}/libs/SDL2_image-2.8.2/cmake")
set(SDL2_ttf_DIR "${CMAKE_SOURCE_DIR}/libs/SDL2_ttf-2.22.0/cmake")

list(APPEND CMAKE_PREFIX_PATH ${SDL2_PATH} ${SDL2_IMAGE_PATH} ${SDL2_TTF_PATH})

find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)

file(GLOB PROJECT_INCLUDES "src/*.cpp") 

add_executable(game WIN32 main.cpp ${PROJECT_INCLUDES})

target_link_libraries(game ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES} ${SDL2_TTF_LIBRARIES})
target_include_directories(game PUBLIC ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS} "include" ${CMAKE_SOURCE_DIR})

这是main.cpp:

#include <iostream>
#include <SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "map.h"

int main(int argc, char *argv[])
{
  std::cout << "PROGRAMM START\n";

  bool QUIT = false;

  if(SDL_Init(SDL_INIT_VIDEO) < 0){
    std::cout << "Cound not initialize sdl subsystem: " << SDL_GetError() << "\n";
    QUIT = false;
  }

  SDL_Window* window = SDL_CreateWindow("GAME", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);

  SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
  SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);

  tilemap first_chunk;
  if(!first_chunk.loadImage("./media/image.jpg", 40, 40))
  {
    std::cout << "Image wasnt found.";
    QUIT = true;
  }
  first_chunk.setRenderer(renderer);

  while(!QUIT)
  {
    SDL_Event event;
    while(SDL_PollEvent(&event))
    {
      if (event.type == SDL_QUIT)
      {
        QUIT = true;
      }
      if (event.type == SDL_KEYDOWN)
      {
        switch (event.key.keysym.sym)
        {
          case SDLK_UP:
            std::cout << "UP key is pressed\n";
            break;
          case SDLK_DOWN:
            std::cout << "DOWN key is pressed\n";
            break;
          case SDLK_RIGHT:
            std::cout << "RIGHT key is pressed\n";
            break;
          case SDLK_LEFT:
            std::cout << "LEFT key is pressed\n";
            break;
          case SDLK_q:
            std::cout << "User entered q which means he wants exit the program. terminating...\n";
            QUIT = true;
            break;
          default:
            break;
        }
      }
    }

    SDL_RenderClear(renderer);

    first_chunk.draw();

    SDL_RenderPresent(renderer);
    SDL_Delay(10);
  }

  SDL_DestroyWindow(window);
  SDL_Quit();

  std::cout << "PROGRAMM END";
  return 0;
}

这是我项目中的文件结构:

File structure in my project

我尝试应用链接 SDL2 库教程,但无法使其工作。我也多次尝试为自己更改文件,但没有结果。 Trenki 的开发血液我也尝试过。

c++ cmake sdl-2
1个回答
-1
投票

CMake 变量区分大小写,例如:

target_link_libraries(game ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES} ${SDL2_TTF_LIBRARIES})

应该是:

target_link_libraries(game ${SDL2_LIBRARIES} ${SDL2_image_LIBRARIES} ${SDL2_ttf_LIBRARIES})

这也是为什么你需要所有这些

set(SDL2_
行;删除它们并修复各处的大小写问题。

© www.soinside.com 2019 - 2024. All rights reserved.