Issue
This Content is from Stack Overflow. Question asked by keatsoo
for the past week I’ve been wanting to create a program using SDL2. However, whatever I do, I keep getting this error
usr/bin/g++ -fdiagnostics-color=always -g /home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp -o /home/myUsername/Documents/Coding/CPP/Pong/src/main
/usr/bin/ld: /tmp/ccbnY9S2.o: in function `main':
/home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp:6: undefined reference to `SDL_Init'
/usr/bin/ld: /home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp:12: undefined reference to `SDL_CreateWindow'
/usr/bin/ld: /home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp:24: undefined reference to `SDL_GetWindowSurface'
/usr/bin/ld: /home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp:32: undefined reference to `SDL_UpdateWindowSurface'
/usr/bin/ld: /home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp:34: undefined reference to `SDL_Delay'
collect2: error: ld returned 1 exit status
I’m on Ubuntu, using VSCode with CMake and have been following this tutorial.
Here is the structure of my project :
Project Structure
Here is my main.cpp :
#include <SDL2/SDL.h>
#include <iostream>
int main()
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Failed to initialize the SDL2 libraryn";
return -1;
}
SDL_Window *window = SDL_CreateWindow("SDL2 Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
680, 480,
0);
if(!window)
{
std::cout << "Failed to create windown";
return -1;
}
SDL_Surface *window_surface = SDL_GetWindowSurface(window);
if(!window_surface)
{
std::cout << "Failed to get the surface from the windown";
return -1;
}
SDL_UpdateWindowSurface(window);
SDL_Delay(5000);
}
And here is my CMakeLists.txt :
cmake_minimum_required(VERSION 3.7)
project(Pong)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(Pong src/main.cpp)
target_link_libraries(Pong ${SDL2_LIBRARIES})
Note that I did install CMake (and Ninja, because I got errors involving Ninja, that were solved by installing it), G++ and SDL2 (the libsdl2-dev
package) using apt-get
beforehand.
Could someone help me please ? Thank you in advance.
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.