0

I have written the sdl2 game in pure c on m1 mac using vscode+homebrew pkg manager with a makefile and it works successfully. I used homebrew to install the pkgs of sdl2(like sdl2_image,sdl2_gfx) and included them in my makefile, and everything works great. However, when I decided to use clion with cmake to write sdl2 games, there was something wrong with my cmake. I have discovered an excellent code example from youtube:https://github.com/tsoding/profun/tree/master/procedural(Thanks a lot for the guy's great work!). I download his whole project and used his cmake, it works great on my ** Linux machine however failed on my m1 mac**. The cmake file looks like this:

cmake_minimum_required(VERSION 3.7)

project(procedural C)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

find_package(PkgConfig)
pkg_check_modules(SDL2_GFX SDL2_gfx)
include_directories(${SDL2_GFX_INCLUDE_DIRS})

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -Wall -Werror")

set(SRCS
  main.c
  logic.c
  rendering.c
)

set(HEADERS
  logic.h
  rendering.h
  game.h
)

add_executable(procedural ${SRCS} ${HEADERS})
target_link_libraries(procedural ${SDL2_LIBRARIES} ${SDL2_GFX_LIBRARIES})

This cmake helped my clion to find the header files and code completion, and when I hit

cmake.

everything works great, it complies and the console shows

 The C compiler identification is AppleClang 13.0.0.13000029
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Found PkgConfig: /opt/homebrew/bin/pkg-config (found version "0.29.2") 
-- Checking for module 'SDL2_gfx'
--   Found SDL2_gfx, version 1.0.2
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mac/Downloads/prof/procedural

however, when I hit

make

the code cannot run and the console shows:

[ 25%] Building C object CMakeFiles/procedural.dir/main.c.o
[ 50%] Building C object CMakeFiles/procedural.dir/logic.c.o
[ 75%] Building C object CMakeFiles/procedural.dir/rendering.c.o
[100%] Linking C executable procedural
ld: library not found for -lSDL2_gfx
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [procedural] Error 1
make[1]: *** [CMakeFiles/procedural.dir/all] Error 2
make: *** [all] Error 2

it says that it cannot find sdl2_gfx, which is very confusing. can anyone help me to figure it out? I will be more than appreciated! I really want to use cmake instead of make(I want the cross-platform feature), and I really want to use cmake with homebrew.

update:
I followed an answer of USING SDL2_GFX_LIBRARIES_DIR instead of SDL2_GFX_LIBRARIES, which still cannot work

Guided by the comment, I used the IMPORTED_TARGET method, and my code is:

cmake_minimum_required(VERSION 3.7)

project(procedural C)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

find_package(PkgConfig)
pkg_check_modules(SDL2_GFX REQUIRED IMPORTED_TARGET SDL2_gfx)
include_directories(${SDL2_GFX_INCLUDE_DIRS})

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -Wall -Werror")

set(SRCS
  main.c
  logic.c
  rendering.c
)

set(HEADERS
  logic.h
  rendering.h
  game.h
)



add_executable(procedural ${SRCS} ${HEADERS})
target_link_libraries(procedural ${SDL2_LIBRARIES} ${SDL2_GFX_LIBRARIES} ${SDL2_GFX_LIBRARIES_DIR})

However the terminal shows:

/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/mac/Downloads/prof/procedural --target procedural -- -j 6
Consolidate compiler generated dependencies of target procedural
[ 50%] Building C object CMakeFiles/procedural.dir/rendering.c.o
[ 50%] Building C object CMakeFiles/procedural.dir/main.c.o
[ 75%] Building C object CMakeFiles/procedural.dir/logic.c.o
[100%] Linking C executable procedural
ld: library not found for -lSDL2_gfx
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [procedural] Error 1
make[2]: *** [CMakeFiles/procedural.dir/all] Error 2
make[1]: *** [CMakeFiles/procedural.dir/rule] Error 2
make: *** [procedural] Error 2

which is the exactly the same error like my previous cmake code.

And I want to show how SDL2_gfx can work with Makefile in another project, here's the last line of the output in the terminal:

clang src/io.o src/logic.o src/main.o src/renderer_game.o  -Iinclude -L/opt/homebrew/lib -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf  -o ./gol 

say just -L/opt/homebrew/libcan find the lib, but i just cannot figure it out in cmake.

billz
  • 11
  • 1
  • Why reask the question with the **same code** and the **same error message** as in your [previous one](https://stackoverflow.com/q/72421616/3440745)? As stated in the comment to the previous question, the **proper** using of `pkg_check_modules` should eliminate "library not found" error. If after fixing that issue you have other errors (like "undefined references"), then show your **updated code** and the **updated error message** which it produces. – Tsyvarev May 29 '22 at 13:24
  • @Tsyvarev Thanks a lot for answering my question. I did not post the same code, I updated the question at the end of the post. I repost a question since the previous one is closed, I am not sure others can see the question or not – billz May 30 '22 at 08:44
  • "I updated the question at the end of the post." - If you are about the phrase "I followed an answer of USING SDL2_GFX_LIBRARIES_DIR instead of SDL2_GFX_LIBRARIES, which still cannot work" then it firstly does not correctly reflect the suggestion (variable `SDL2_GFX_LIBRARIES_DIR` should be used **in addition** to `SDL2_GFX_LIBRARIES`, not as replace for it), and, what is the most important, it contains neither your new **code** nor the **error message** is causes. Solely a description "cannot work" is not useful when trying to solve the problem. – Tsyvarev May 30 '22 at 09:00
  • @I am confused what dose "variable SDL2_GFX_LIBRARIES_DIR should be used in addition to SDL2_GFX_LIBRARIES, not as replace for it" mean, since I have added `message(${SDL2_GFX_LIBRARIES_DIR})` before the line of `add_executable(procedural ${SRCS} ${HEADERS})`, and the console returns a error, I guess `${SDL2_GFX_LIBRARIES_DIR}` not exist here?(because my Linux machine can run the cmake directly.) – billz May 30 '22 at 09:34
  • "the console returns a error" - A **console** cannot return an error. Once again, please show your **code** (add it to the question post) and the **exact error message**. – Tsyvarev May 30 '22 at 09:37
  • "Guided by the comment, I used the `IMPORTED_TARGET` method" - That method implies no only an additional parameter to `pkg_check_modules`, but also using IMPORTED target instead of the variable for link the library: `target_link_libraries(procedural PkgConfig::SDL2_GFX)`. See [that answer](https://stackoverflow.com/a/57224542/3440745) as example. – Tsyvarev May 30 '22 at 10:03
  • @Tsyvarev sir, this solved my question! I am more than delighted! Thank you so much for teaching me how to use stack overflow, it's such a great community! I will edit the question in a more clear way later to let others know how to cope the issue easily, Thank you sir, HAVE A NICE DAY! – billz May 30 '22 at 10:23

0 Answers0