首先我在ubuntu 17上使用clion和内置cmake
建立按摩日志:
[ 20%] Linking C executable pacman
cc: error: SDL2_image: No such file or directory
CMakeFiles/pacman.dir/build.make:172: recipe for target 'pacman' failed
make[3]: *** [pacman] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/pacman.dir/all' failed
make[2]: *** [CMakeFiles/pacman.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/pacman.dir/rule' failed
make[1]: *** [CMakeFiles/pacman.dir/rule] Error 2
Makefile:118: recipe for target 'pacman' failed
make: *** [pacman] Error 2
cmakeList.txt:
cmake_minimum_required(VERSION 3.9)
project(pacman C)
set(CMAKE_C_STANDARD 99)
set(SOURCE src/main.c src/input.c src/input.h src/view.c src/view.h src/models.h src/models.c)
add_executable(pacman "${SOURCE}")
include_directories(src)
include_directories(src/SDL2-2.0.7/include)
INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED)
set(SDL2_IMAGE_INCLUDE_DIR "src/SDL2_image-2.0.2")
include_directories("${SDL2_IMAGE_INCLUDE_DIR}")
target_link_libraries(pacman "${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES}")
和一个名为Makefile的文件:(我不太了解它!不幸的是,错误终于从这里升起)
# Makefile for showimage
CC = gcc
CFLAGS = $(shell sdl2-config --cflags) -Wall -O
LIBS = $(shell sdl2-config --libs) -lSDL2_image
EXE = showimage
all: $(EXE)
showimage: showimage.c Makefile
$(CC) -o $@ $@.c $(CFLAGS) $(LIBS)
clean:
-rm *.o $(EXE)
请注意,我100%确定使用此命令安装了所有sdl2库:
sudo apt-get install libsdl2*
它工作正常,一切都没有任何错误输出:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'libsdl2-mixer-dev' for glob 'libsdl2*'
Note, selecting 'libsdl2-image-dev' for glob 'libsdl2*'
Note, selecting 'libsdl2-gfx-dev' for glob 'libsdl2*'
Note, selecting 'libsdl2-gfx-doc' for glob 'libsdl2*'
Note, selecting 'libsdl2-mixer-2.0-0' for glob 'libsdl2*'
Note, selecting 'libsdl2-dev' for glob 'libsdl2*'
Note, selecting 'libsdl2-doc' for glob 'libsdl2*'
Note, selecting 'libsdl2-ttf-dev' for glob 'libsdl2*'
Note, selecting 'libsdl2-net-2.0-0' for glob 'libsdl2*'
Note, selecting 'libsdl2-net-dev' for glob 'libsdl2*'
Note, selecting 'libsdl2-image-2.0-0' for glob 'libsdl2*'
Note, selecting 'libsdl2-2.0-0' for glob 'libsdl2*'
Note, selecting 'libsdl2-gfx-1.0-0' for glob 'libsdl2*'
Note, selecting 'libsdl2-ttf-2.0-0' for glob 'libsdl2*'
libsdl2-2.0-0 is already the newest version (2.0.6+dfsg1-3ubuntu1).
libsdl2-dev is already the newest version (2.0.6+dfsg1-3ubuntu1).
libsdl2-doc is already the newest version (2.0.6+dfsg1-3ubuntu1).
libsdl2-gfx-1.0-0 is already the newest version (1.0.1+dfsg-5).
libsdl2-gfx-dev is already the newest version (1.0.1+dfsg-5).
libsdl2-gfx-doc is already the newest version (1.0.1+dfsg-5).
libsdl2-image-2.0-0 is already the newest version (2.0.1+dfsg-3).
libsdl2-image-dev is already the newest version (2.0.1+dfsg-3).
libsdl2-mixer-2.0-0 is already the newest version (2.0.1+dfsg1-3).
libsdl2-mixer-dev is already the newest version (2.0.1+dfsg1-3).
libsdl2-net-2.0-0 is already the newest version (2.0.1+dfsg1-3).
libsdl2-net-dev is already the newest version (2.0.1+dfsg1-3).
libsdl2-ttf-2.0-0 is already the newest version (2.0.14+dfsg1-2).
libsdl2-ttf-dev is already the newest version (2.0.14+dfsg1-2).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
感谢先帮忙:))))))
答案 0 :(得分:0)
使用find_library而不是PKG_SEARCH_MODULE
find_library(SDL2_LIBRARY NAME SDL2)
target_include_directories(pacman ${SDL2_INCLUDE_DIR})
target_link_libraries(pacman ${SDL2_LIBRARY})
如果找不到SDL2,则必须将SDL2的路径添加到CMAKE_PREFIX_PATH,这是CMake查找已安装软件的地方。
答案 1 :(得分:0)
我通过这种方式更改cmake代码来解决所有问题: 简单地手动给出sdl目录路径!
cmake_minimum_required(VERSION 3.9)
project(pacman C)
set(CMAKE_C_STANDARD 99)
set(SOURCE src/main.c src/input.c src/input.h src/view.c src/view.h src/models.h src/models.c src/controler.c src/controler.h)
add_executable(pacman "${SOURCE}")
include_directories(src)
include_directories("/usr/include/SDL2")
target_link_libraries(pacman m SDL2 SDL2_gfx SDL2_image)
ADD_DEFINITIONS(-D_REENTRANT)