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/lib
can find the lib, but i just cannot figure it out in cmake.