在main.cpp(带有CMake的Allegro)的上下文中找不到目录]]

问题描述 投票:0回答:1

我正在尝试创建一个使用CMake链接到allegro5媒体库的库。然后,我想在可执行文件中使用我的库。

我的目录结构是:

src
 |----core
        |------src
        |------tests
        |------CMakeLists.txt
 |----main.cpp
 |----CMakeLists.txt

核心文件夹中的CMakeLists.txt文件为:

set(MY_HEADERS #My header files here)
set(MY_SRC #My source files here)

add_library(MyLib ${MY_HEADERS} ${MY_SRC})
target_include_directories(MyLib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")

## Allegro Lib

set(ALLEGRO_DIR "${PROJECT_SOURCE_DIR}/packages/allegro5/include")

if(WIN32)
    file(GLOB ALLEGRO_LIB "${PROJECT_SOURCE_DIR}/packages/allegro5/windows/x86/lib/*.lib")
    add_library(Allegro SHARED IMPORTED)
    set_target_properties(Allegro PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES ${ALLEGRO_DIR}
        IMPORTED_IMPLIB ${ALLEGRO_LIB}
    )
    target_link_libraries(MyLib PRIVATE Allegro)
    file(GLOB ALLEGRO_DLL "${PROJECT_SOURCE_DIR}/packages/allegro5/windows/x86/bin/*.dll")
    foreach(dll ${ALLEGRO_DLL})
        add_custom_command(TARGET MyLib POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
            ${dll} $<TARGET_FILE_DIR:MyLib>)
    endforeach()
endif()

现在在我的顶层CMakeLists.txt中是:

cmake_minimum_required(VERSION 3.15)

project(MyProject)
enable_testing()

add_subdirectory(core)
add_executable(MyGame main.cpp)
target_link_libraries(MyGame MyLib)

在我的IDE(Visual Studio)中,我看到源文件中“ allegro5 / allegro.h”的include语句位于核心文件夹内,这给了我警告“在搜索路径中找不到目录allegro5 ... ../ src / main.cpp“。

构建项目时出现错误:

Cannot open source file 'allegro5/allegro'
Cannot open include file 'allegro5/allegro.h': No such file or directory.

仅当我在main.cpp中引用MyLib时,才会发生此错误。 (我的主体没有任何代码,只有一个hello world语句):

// #include "MyLib.hpp"

int main(int argc, const char *argv[])
{
    /*const auto engine = &MyLib::EngineManager::getInstance();
    engine->start();
    const auto display = &MyLib::DisplayManager::getInstance();

    auto displayConfig = DisplayConfig();
    displayConfig.fullscreen = false;
    const auto displayId = display->createDisplay(displayConfig);*/
}

我想这与我在某处设置的可见性有关?我不太清楚如何解决。感谢您的帮助,谢谢!

我正在尝试创建一个使用CMake链接到allegro5媒体库的库。然后,我想在可执行文件中使用我的库。我的目录结构是:src | ---- core | ------...

c++ cmake allegro5
1个回答
0
投票

allegro5的包含目录在您的CMake中定义为ALLEGRO_DIR

© www.soinside.com 2019 - 2024. All rights reserved.