我有一个使用 SDL2 和 SDL2_ttf 的 C 语言项目。
这是我的 cmakelists 文件:
cmake_minimum_required(VERSION 3.5.0)
project(mygame VERSION 0.1.0 LANGUAGES C)
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2TTF_INCLUDE_DIR})
add_executable(mygame main.c
classes/list/list.c
classes/my_string/my_string.c
classes/errors/errors.c
classes/render/render.c
classes/constants/constants.c
)
target_link_libraries(mygame PRIVATE ${SDL2_LIBRARIES} ${SDL2TTF_LIBRARY})
所以编译后我有 SDL2/SDL_ttf.h not found 错误:
[main] Building folder: /Users/ilababakov/Documents/code/Equation_solver/build all
[build] Starting build
[proc] Executing command: /opt/homebrew/bin/cmake --build /Users/ilababakov/Documents/code/Equation_solver/build --config Debug --target all -j 10 --
[build] [ 14%] Building C object CMakeFiles/mygame.dir/main.c.o
[build] [ 42%] Building C object CMakeFiles/mygame.dir/classes/list/list.c.o
[build] [ 42%] Building C object CMakeFiles/mygame.dir/classes/errors/errors.c.o
[build] [ 71%] Building C object CMakeFiles/mygame.dir/classes/render/render.c.o
[build] [ 71%] Building C object CMakeFiles/mygame.dir/classes/my_string/my_string.c.o
[build] [ 85%] Building C object CMakeFiles/mygame.dir/classes/constants/constants.c.o
[build] In file included from /Users/ilababakov/Documents/code/Equation_solver/classes/render/render.c:1:
[build] /Users/ilababakov/Documents/code/Equation_solver/classes/render/render.h:4:10: fatal error: 'SDL2/SDL_ttf.h' file not found
[build] #include <SDL2/SDL_ttf.h>
[build] ^~~~~~~~~~~~~~~~
[build] 1 error generated.
[build] make[2]: *** [CMakeFiles/mygame.dir/classes/render/render.c.o] Error 1
[build] make[2]: *** Waiting for unfinished jobs....
[build] In file included from /Users/ilababakov/Documents/code/Equation_solver/classes/constants/constants.c:1:
[build] In file included from /Users/ilababakov/Documents/code/Equation_solver/classes/constants/constants.h:6:
[build] /Users/ilababakov/Documents/code/Equation_solver/classes/constants/../errors/errors.h:4:10: fatal error: 'SDL2/SDL_ttf.h' file not found
[build] #include <SDL2/SDL_ttf.h>
[build] ^~~~~~~~~~~~~~~~
[build] 1 error generated.
[build] make[2]: *** [CMakeFiles/mygame.dir/classes/constants/constants.c.o] Error 1
[build] In file included from /Users/ilababakov/Documents/code/Equation_solver/classes/errors/errors.c:1:
[build] /Users/ilababakov/Documents/code/Equation_solver/classes/errors/errors.h:4:10: fatal error: 'SDL2/SDL_ttf.h' file not found
[build] #include <SDL2/SDL_ttf.h>
[build] ^~~~~~~~~~~~~~~~
[build] 1 error generated.
[build] In file included from /Users/ilababakov/Documents/code/Equation_solver/main.c:2:
[build] /Users/ilababakov/Documents/code/Equation_solver/classes/render/render.h:4:10: fatal error: 'SDL2/SDL_ttf.h' file not found
[build] #include <SDL2/SDL_ttf.h>
[build] ^~~~~~~~~~~~~~~~
[build] 1 error generated.
[build] make[2]: *** [CMakeFiles/mygame.dir/classes/errors/errors.c.o] Error 1
[build] make[2]: *** [CMakeFiles/mygame.dir/main.c.o] Error 1
[build] make[1]: *** [CMakeFiles/mygame.dir/all] Error 2
[build] make: *** [all] Error 2
[proc] The command: /opt/homebrew/bin/cmake --build /Users/ilababakov/Documents/code/Equation_solver/build --config Debug --target all -j 10 -- exited with code: 2
[driver] Build completed: 00:00:00.161
[build] Build finished with exit code 2
我已经通过brew安装了SDL2和ttf,并且都找到了它们(当我尝试find_package时,至少cmake不会抱怨),但错误仍然存在。不使用 SDL2_ttf 编译项目一切都很好。 我在 macos sonoma 14.2.1 上使用 vscode cmake 扩展。
调用
find_package(SDL2_ttf)
创建 IMPORTED 目标 SDL2_ttf::SDL2_ttf
。只需将您的目标与其链接,您的目标将获得所有需要的包含目录和库:
target_link_libraries(mygame PRIVATE SDL2_ttf::SDL2_ttf)