Can't build project with SDL2 libraries near to it and cmake on Windows

62 Views Asked by At

I want to build my project using cmake and SDL2 libraries, which are located near to project files. I already succeeded in linking SDL2, but can't do the same for SDL2_image and SDL2_ttf. I spent many hours, but can't understand what could be wrong and solve this. All this I do on Windows 10.

I get these errors:

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]

Here is my 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})

Here is 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;
}

And this is the file structure in my project:

File structure in my project

I tried to apply linking SDL2 libraries tutorials, but can't get it working. I tried to change files for myself a lot too, but no result. Trenki's dev blod I have also tried.

1

There are 1 best solutions below

1
Ken Y-N On

CMake variables are case-sensitive, so for instance:

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

Should be:

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

That's also why you needed all these set(SDL2_ lines; delete them and fix the case problem everywhere.