1

I am trying to switch to CLion IDE in my Mac. My CMakeLists.txt works but while building the project, I get linker command failed error. I first tried with FindSDL2 and FindSDL2_IMAGE, but I removed it because I got the same error both with and without. I have a doubt about the SDL_LIBRARY and SDL2_IMAGE_LIBRARY, where it is declared?.

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(OxEngine)

set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp engine.cpp sprite.cpp classes.h inventory.cpp vector.cpp)

MESSAGE(${SDL_INCLUDE_DIR} "  Image: " ${SDL2_IMAGE_INCLUDE_DIR})

include_directories(${SDL_INCLUDE_DIR})
include_directories(${SDL2_IMAGE_INCLUDE_DIR})


add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})

MESSAGE(${SDL_LIBRARY} "  HH  " ${SDL2_IMAGE_LIBRARY})

target_link_libraries(${CMAKE_PROJECT_NAME} ${SDL_LIBRARY} ${SDL2_IMAGE_LIBRARY})

When loaded in CLion I get the following CMake output:

"/Applications/CLion 2.app/Contents/bin/cmake/mac/bin/cmake" -DCMAKE_BUILD_TYPE= -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++ -G "CodeBlocks - Unix Makefiles" /Users/nantha/Projc/my_projects/OxEngine/src
/Library/Frameworks/SDL2.framework/Headers  Image: /usr/local/include/SDL2
/usr/local/lib/libSDL.dylib-framework Cocoa  HH  /usr/local/lib/libSDL2_image.dylib
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/nantha/Projc/my_projects/OxEngine/src/cmake-build-runit

[Finished]

When I run the project I get

====================[ Build | OxEngine | Runit ]================================
"/Applications/CLion 2.app/Contents/bin/cmake/mac/bin/cmake" --build /Users/nantha/Projc/my_projects/OxEngine/src/cmake-build-runit --target OxEngine -- -lSDL2 -lSDL2_image
[ 16%] Linking CXX executable OxEngine
Undefined symbols for architecture x86_64:
  "_SDL_CreateRenderer", referenced from:
      Engine::init(Game&) in engine.cpp.o
  "_SDL_CreateTexture", referenced from:
      Inventory::render_itemButtons() in inventory.cpp.o
      Inventory::render_categoryButtons() in inventory.cpp.o
      Inventory::draw() in inventory.cpp.o
  "_SDL_CreateTextureFromSurface", referenced from:
      Engine::loadMedia() in engine.cpp.o
      Button::load_images() in engine.cpp.o
      Sprite::load_images() in sprite.cpp.o
      InventoryButton::load_icon() in inventory.cpp.o
  "_SDL_CreateWindow", referenced from:
      Engine::init(Game&) in engine.cpp.o
  "_SDL_DestroyRenderer", referenced from:
      Engine::close() in engine.cpp.o
  "_SDL_DestroyTexture", referenced from:
      Inventory::draw() in inventory.cpp.o
  "_SDL_DestroyWindow", referenced from:
      Engine::close() in engine.cpp.o
  "_SDL_QueryTexture", referenced from:
      Inventory::draw() in inventory.cpp.o
  "_SDL_RenderClear", referenced from:
      Engine::drawisoworld() in engine.cpp.o
  "_SDL_RenderCopy", referenced from:
      Engine::renderit(std::__1::vector<std::__1::vector<bool, std::__1::allocator<bool> >, std::__1::allocator<std::__1::vector<bool, std::__1::allocator<bool> > > >&, int, int, int) in engine.cpp.o
      Engine::draw_selected_tiles() in engine.cpp.o
      Button::drawButton() in engine.cpp.o
      Engine::drawisoworld() in engine.cpp.o
      InventoryButton::draw() in inventory.cpp.o
      Inventory::draw() in inventory.cpp.o
  "_SDL_RenderFillRect", referenced from:
      Inventory::render_itemButtons() in inventory.cpp.o
      Inventory::render_categoryButtons() in inventory.cpp.o
      Inventory::draw() in inventory.cpp.o
  "_SDL_RenderPresent", referenced from:
      Engine::update() in engine.cpp.o
  "_SDL_SetRenderDrawColor", referenced from:
      Engine::loadMedia() in engine.cpp.o
      Engine::drawisoworld() in engine.cpp.o
      Inventory::render_itemButtons() in inventory.cpp.o
      Inventory::render_categoryButtons() in inventory.cpp.o
      Inventory::draw() in inventory.cpp.o
  "_SDL_SetRenderTarget", referenced from:
      Inventory::render_itemButtons() in inventory.cpp.o
      Inventory::render_categoryButtons() in inventory.cpp.o
      Inventory::draw() in inventory.cpp.o
  "_SDL_SetTextureBlendMode", referenced from:
      Inventory::draw() in inventory.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [OxEngine] Error 1
make[2]: *** [CMakeFiles/OxEngine.dir/all] Error 2
make[1]: *** [CMakeFiles/OxEngine.dir/rule] Error 2
make: *** [OxEngine] Error 2

Project Structure:

Project structure

Thank You

R Nanthak
  • 344
  • 3
  • 17
  • It looks like the `SDL2_*` variables are somehow set in your CMake without calling `find_package(SDL2 ...`. You should have have the `find_package` call in your CMake before using these variables (unless you set them manually in CLion, not recommended). Using SDL2 in your CMake project is generally a *solved problem*, see the responses to [this](https://stackoverflow.com/q/28395833/3987854) question. Also, the linker error states `symbol(s) not found for architecture x86_64`, so are you sure there is not an **architecture mismatch** between the SDL2 libraries you are using and your project? – Kevin Jul 03 '20 at 14:12
  • "I have a doubt about the SDL_LIBRARY and SDL2_IMAGE_LIBRARY, where it is declared?." - They are never declared in your current `CMakeLists.txt`. Probably, these variable remains in the cache after you use `find_package()`. If you fresh configure your project, the variables will be empty(not existed), so your current `CMakeLists.txt` has a little sense. – Tsyvarev Jul 03 '20 at 16:22
  • @Tsyvarev Its ok, but why the linker command failed error is appearing. What causes this?. The header files are included correctly, but finally what causing the build to fail? – R Nanthak Jul 03 '20 at 16:24
  • @squareskittles I can compile the project from a terminal with options -lSDL2 -lSDL2_image, but I don't know how to simply make that in CLion. – R Nanthak Jul 03 '20 at 16:25
  • I linked to some complete examples showing how to include SDL2 in your project (i.e. using `find_package`), did you try these? Your CMake is incomplete, and as mentioned, your CMake variables are only populated via stale cache entries. – Kevin Jul 03 '20 at 20:34

0 Answers0