I'm working on making a C++ project for fun. However I'm stumped by this error.
My cmake file builds fine but trying to run things gives me the error: undefined reference to IMG_Load'`.
I'm on Arch Linux, and I've installed SDL2 from source, and SDL_Image from source. So I'm pretty sure all the needed includes are in place.
My CMake file:
CMAKE_MINIMUM_REQUIRED(VERSION 3.27.4)
project(Origin)
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
set(
SOURCES
src/main.cpp
src/game.cpp
)
add_executable(Origin ${SOURCES})
include_directories(
Origin
${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_INCLUDE_DIRS}
headers/
)
target_link_libraries(
Origin
${SDL2_IMAGE_LIBRARIES}
${SDL2_LIBRARIES}
)
This shows no errors and running build through VSCode, works fine. Trying to run after shows the undefined error.
I'm sure I'm doing something wrong, but I have no idea what. I'd really appreciate any help. Thank you!
Things I've tried:
I've tried double checking the names of the SDL libraries variable, and ensuring that they were found. As far as I can tell they are found.
I've re-arranged my cmake file to what I think is the best order to put things so that cmake doesn't try and do things in the improper order.
I've scoured google and only come up with that it's a linking error, which is why I'm here asking my question.
I've removed the IMG_Load function to make sure my project built and ran. It did, with no errors.
It looks like you've set up your CMakeLists.txt file correctly for SDL2 and SDL2_image. However, the "undefined reference to IMG_Load" error typically indicates a linking problem. To resolve this issue, you need to make sure that the SDL2_image library is linked correctly. Here are a few steps to help you debug and fix the problem:
Check Library Names: First, make sure that you are linking against the correct SDL2_image library. The library name for SDL2_image can vary depending on your system and how you installed it. Try changing your target_link_libraries section like this:
This assumes that you have SDL2 and SDL2_image installed as CMake packages. The "::" notation is used to find and link against the correct libraries.
Verify Installation: Double-check that you have SDL2_image installed correctly. You mentioned that you installed it from source. Ensure that you compiled and installed it successfully, and that it's in a location where CMake can find it. Make sure that the SDL2_image libraries are present and accessible.
CMake Cache: Sometimes CMake caches can cause issues. Try deleting your CMake cache and rebuilding the project:
Full Rebuild: Sometimes, a full rebuild can solve linking issues. Try running:
CMakeLists.txt Order: Ensure that your CMakeLists.txt file is in the same directory as your source files, and it's being picked up correctly by CMake. The order of your CMakeLists.txt and source files in your project directory can sometimes affect the build process.
Compiler Flags: Check if you are using the correct compiler flags. Make sure you are not inadvertently overriding necessary flags in your CMakeLists.txt or in your build environment.
Library Paths: Verify that the library paths for SDL2 and SDL2_image are correctly set in your system. You may need to update the LD_LIBRARY_PATH environment variable or add library paths to your CMakeLists.txt if they are not in the standard search path.
IDE-Specific Issues: If you are using an IDE like Visual Studio Code, sometimes IDE-specific configurations can cause issues. Make sure your IDE is configured to use the correct CMakeLists.txt file and build settings.
After trying these steps, rebuild your project, and the "undefined reference to IMG_Load" error should be resolved. If you still encounter issues, please provide more details about your system and how you installed SDL2 and SDL2_image, which can help diagnose the problem further.