0

I have written sdl2 game in pure c on m1 mac using vscode+homebrew pkg manager with makefile and it works successfully. I used hombrew to install the pkgs of sdl2(like sdl2_image,sdl2_gfx) and included them in my makefile, and every thing just works great. However, when i decided to use clion with cmake to write sdl2 games, there's something wrong with my cmake. I have discoverd a very good code example from youtube, which is: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 falied on my m1 mac**. The cmake file looks like:

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.

billz
  • 11
  • 1
  • You forgot to use `SDL2_GFX_LIBRARY_DIRS` variable, which is filled by `pkg_check_modules` with **directories** where the libraries should be searched. Without knowing that directories the linker could fail to find the libraries listed in `SDL2_GFX_LIBRARIES` variable. In any case it is better to use `IMPORTED_TARGET` option to that command, as described in [that answer](https://stackoverflow.com/a/57224542/3440745). – Tsyvarev May 29 '22 at 11:02
  • @Tsyvarev Thanks a lot for your generous answer! However, this method does not helped me to solve the issue. when I use ``` target_link_libraries(procedural ${SDL2_LIBRARIES} ${SDL2_GFX_LIBRARIES_DIR} )```` instead of my previous method, the compilation does not work, the console shows: Undefined symbols for architecture arm64: "_filledCircleRGBA", referenced from: _render_o in rendering.c.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – billz May 29 '22 at 12:00
  • @Tsyvarev I have also noticed that when i just delete the {SDL_GFX_LIBRARIES}, they console returns the same information of undefined symbols – billz May 29 '22 at 12:04

0 Answers0