fatal error: allegro5\allegro.h: No such file or directory yet found on cmake

fatal error: allegro5\allegro.h: No such file or directory yet found on cmake

所以我很久以前在 Visual Studio 上玩过一个游戏。 2 年前,我将源代码上传到 github NEGU93/ForbiddenDesert 只是为了把它放在那里。现在我从 windows 更改为 linux,我想回去为 linux 编译这个游戏。

我记得我在 GUI 中使用了 allegro,所以我按照以下步骤安装了它 Installing Allegro 5。当我编写游戏代码时,我记得直接下载了为 Visual Studio 准备的快板,所以我没有遇到任何问题。

我创建了一个 CMakeLists.txt(以前从未做过,所以我在那个领域还很陌生):

# Specify the minimum version for CMake
cmake_minimum_required(VERSION 2.8)

# projects name
project(ForbiddenDesert)

set(CMAKE_CXX_STANDARD 11)  # enable C++11 standard
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
# Finds Allegro using pkgconfig, so it must be configured correctly
find_package(Allegro5 REQUIRED)
# Set include and lib dirs.
include_directories(${ALLEGRO_INCLUDE_DIR})
set(FD_LIBS ${LIBS} ${OBJC_LIBRARIES} ${ALLEGRO_LIBRARIES})


# The following folder will be included
# include_directories(${PROJECT_SOURCE_DIR}/src)
# include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories("${PROJECT_SOURCE_DIR}")

file(GLOB all_SRCS
        "*.h"
        "*.cpp"
        )

add_executable(forbidden ${all_SRCS})
target_link_libraries(forbidden ${FD_LIBS})

我在名为 cmake/ 的文件夹中使用名为 FindAllegro5.cmake 的文件。该文件是 eruta/FindAllegro5.cmake.

的副本

当 运行ning cmake . 我得到以下信息:

-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Checking for module 'allegro-5'
--   Found allegro-5, version 5.2.4
-- Found Allegro5: /usr/lib/liballegro.so;/usr/lib/liballegro_image.so;/usr/lib/liballegro_font.so;/usr/lib/liballegro_primitives.so;/usr/lib/liballegro_ttf.so;/usr/lib/liballegro_audio.so;/usr/lib/liballegro_dialog.so;/usr/lib/liballegro_memfile.so;/usr/lib/liballegro_acodec.so;/usr/lib/liballegro_color.so;/usr/lib/liballegro_main.so;/usr/lib/liballegro_physfs.so 

到目前为止一切顺利。然而,当我 运行 make

In file included from /home/ubuntu/Documents/GitHub/ForbiddenDesert/Button.h:5:0,
                 from /home/ubuntu/Documents/GitHub/ForbiddenDesert/ArcheologistButton.h:4,
                 from /home/ubuntu/Documents/GitHub/ForbiddenDesert/ArcheologistButton.cpp:1:
/home/ubuntu/Documents/GitHub/ForbiddenDesert/allegro.h:5:10: fatal error: allegro5\allegro.h: No such file or directory
 #include <allegro5\allegro.h>
          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/forbidden.dir/build.make:62: recipe for target 'CMakeFiles/forbidden.dir/ArcheologistButton.cpp.o' failed
make[2]: *** [CMakeFiles/forbidden.dir/ArcheologistButton.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/forbidden.dir/all' failed
make[1]: *** [CMakeFiles/forbidden.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

我看到 FindAllegro5.cmake 有:

# Include dir
find_path(Allegro5_INCLUDE_DIR
  NAMES allegro5/allegro5.h
  PATHS ${Allegro5_PKGCONF_INCLUDE_DIRS}
)

所以我把include改成了#include <allegro5/allegro5.h>但是还是没有解决。

我没能找到解决方案(我找到了很多关于 "fatal error: allegro5\allegro.h: No such file or directory" 的信息,但没有找到适用于我的情况的信息)。


所以我用以下命令安装了 allegro:Quickstart Allegro,然后我创建了 hello.c 文件并按照其中的说明编译它 (gcc hello.c -o hello $(pkg-config allegro-5 allegro_font-5 --libs --cflags)),它成功了。所以问题是如何将这些标志添加到 cmakelists.txt

所以我尝试了:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $(pkg-config allegro-5 allegro_font-5 allegro_primitives-5 allegro_ttf-5 allegro_image-5 allegro_audio-5 allegro_acodec-5  --libs --cflags)")

但是还是不行。


运行 find /usr/ -name "allegro*" 我在 /usr/include/allegro5/allegro5.h 上找到了 header allegro5.h,在 /usr/lib/pkgconfig/ 上找到了一些其他文件。 所以我做了:

INCLUDE_DIRECTORIES( /usr/include/allegro5 )
LINK_DIRECTORIES(  /usr/lib/pkgconfig )
TARGET_LINK_LIBRARIES(game liballegro.a )

还是不行。


我尝试直接从我从 github 上的源代码编译的构建中添加库(按照我在此处找到的信息 Allegro and CMake),但仍然没有用:

#Include Allegro
include_directories(/home/ubuntu/Documents/GitHub/allegro5/build/include)
include_directories(/home/ubuntu/Documents/GitHub/allegro5/build/lib/Headers)
link_directories(/home/ubuntu/Documents/GitHub/allegro5/build/lib)
#connect all the libraries you need
set(game_LIBS liballegro.so liballegro_dialog.so liballegro_image.so)
target_link_libraries(game ${game_LIBS})

在 Ubuntu 下,我会用 apt-get:

安装 allegro
apt-get install liballegro5-dev

那么 include 就会在正确的位置 (/usr/include/allegro5/...)

你说你安装了 allegro,但我不太确定它是否与软件包中的相同。如果它来自构建,默认安装目录通常是 /usr/local 而不是 /usr。所以包含在 /usr/local/include/allegro5/...

也就是说,您在 cmake 中的 include() 命令是正确的:

include_directories(${ALLEGRO_INCLUDE_DIR})

但是,您的库处理完全错误。您永远不必更改 LD_FLAGS。 (至少,在过去的 6 或 7 年里,我一直使用 cmake,我从来没有需要。)相反,您想使用 target_link_libraries()。像这样:

target_link_libraries(${PROJECT_NAME} ${ALLEGRO_LIBRARIES})

它将在 project(...).

内(如之后)工作

如果要验证变量的命名是否正确,可以使用message()命令。一些库使用 NAME_LIBRARYNAME_INCLUDE_PATH 等名称。它们应该遵循正确的命名约定,但没有任何强制它们这样做,因此您经常会发现一些有趣的地方。

下面会打印出一条消息:

message("allegro libraries = " ${ALLEGRO_LIBRARIES})

如果变量名不正确(或找不到库),您将什么也看不到。如果你做对了,它会告诉你变量库。我想那是对的,但 LD_FLAGS 可能是你弄错的地方。