【问题标题】:cmake error: no such file?cmake错误:没有这样的文件?
【发布时间】:2017-12-02 22:32:56
【问题描述】:

首先我在 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.

提前感谢您的帮助:)))))

【问题讨论】:

    标签: ubuntu cmake linker-errors


    【解决方案1】:

    使用 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 查找已安装软件的地方。

    【讨论】:

    • 我保持文件的其余部分不变,但将 PKG_SEARCH_MODULE 更改为 find_library 只是复制您的代码,但此错误上升 {Make Error at CMakeLists.txt:11 (target_include_directories): target_include_directories called with invalid arguments CMake Error:此项目中使用了以下变量,但它们设置为 NOTFOUND。请设置它们或确保它们在 CMake 文件中正确设置和测试:SDL2_LIBRARY 由目录 /home/mrtalebi/Projects/CLionProjects/pacman 中的目标“pacman”链接}
    • 我发现编译器链接器命令出现错误:像这样,它会出现错误:-o pacman -lSDL2 SDL2_image -lSDL2
    • 您甚至需要像我指定的方式一样添加 target_include_directories 和 target_link_directories。这样做并在这里发布会发生什么
    • 感谢您的帮助! cmake 的编码比我预期的要硬得多!
    • 不,实际上 cmake 让我们更容易..从您的以下回复中,我了解到您可以通过这种方式检查包 find_package(SDL2 components gfx image)...无论如何您都知道了。好吧很好!
    【解决方案2】:

    我已经通过以这种方式更改 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-26
      • 2018-01-16
      • 2022-07-21
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多