Cmake 找不到 msys2 的 luajit 包 (windows)

Cmake Can´t find luajit packages of msys2 (windows)

我在将库包含到我的 cmake 项目中时遇到了另一个问题。 项目规格为:

IDE: Visual Studio 代码

套件:GCC 10.3.0 Mingw64(通过 msys2)

Freetype 和 Luajit 这两个包也是通过 msys 安装的(在 mingw64 模式下)。

所以,这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

project(TileGameStudio_Runtime CXX)

set(CMAKE_CXX_FLAGS -v)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

set(PROJ_SRC
    ${PROJECT_SOURCE_DIR}/src
)

file(GLOB PROJECT_SOURCES CONFIGURE_DEPENDS
    ${PROJ_SRC}/*.cpp
)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}\Game")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}\Game")

INCLUDE(FindPkgConfig)
pkg_check_modules(SDL2 REQUIRED sdl2)
pkg_check_modules(SDL2_IMAGE REQUIRED SDL2_image)
pkg_check_modules(SDL2_TTF REQUIRED SDL2_ttf)
pkg_check_modules(SDL2_MIXER REQUIRED SDL2_mixer)
find_package(OpenGL REQUIRED)
find_package(Freetype REQUIRED)
find_package(Lua51 REQUIRED)

if(NOT FREETYPE_FOUND)
  message(STATUS "freetype2:   NO")
else()
  message(STATUS "freetype2:   YES..   Version:${FREETYPE_VERSION_STRING}")
endif()

if(NOT LUA_FOUND)
  message(STATUS "LuaJit:   NO")
else()
  message(STATUS "LuaJit:   YES..   Version:${LUA_VERSION_MAJOR }  and Patched: ${LUA_VERSION_PATCH }")
endif()

include_directories(
    ${SDL2_INCLUDE_DIRS}
    ${SDL2_IMAGE_INCLUDE_DIRS}
    ${SDL2_TTF_INCLUDE_DIRS}
    ${SDL2_MIXER_INCLUDE_DIRS}
    ${OPENGL_INCLUDE_DIRS}
    ${LUA_INCLUDE_DIR}
    ${FREETYPE_INCLUDE_DIRS}
)

link_directories (
    ${SDL2_LIBRARY_DIRS}
    ${SDL2_IMAGE_LIBRARY_DIRS}
    ${SDL2_TTF_LIBRARY_DIRS}
    ${SDL2_MIXER_LIBRARY_DIRS}
    ${OPENGL_LIBRARY_DIRS}
    ${LUA_INCLUDE_DIR}
    ${FREETYPE_INCLUDE_DIRS}
)

# add the executable
add_executable(TileGameStudio_Runtime
    ${PROJECT_SOURCES}
)

target_link_libraries (TileGameStudio_Runtime 
    ${SDL2_LIBRARIES}
    ${SDL2_IMAGE_LIBRARIES}
    ${SDL2_TTF_LIBRARIES}
    ${SDL2_MIXER_LIBRARIES}
    ${OPENGL_gl_LIBRARY}
    ${LUA_LIBRARIES}
)

set_target_properties(
    TileGameStudio_Runtime
    PROPERTIES
        OUTPUT_NAME "Game"
        SUFFIX ".exe"
)

这就是我的全部main.cpp:

#include <ft2build.h>
#include FT_FREETYPE_H
#include <iostream>
#include <vector>
#include <string>
#include <assert.h>
#include <SDL2/SDL.h>
#include <SDL2/sdl_ttf.h>
#include <SDL2/SDL_opengl.h>
#include <GL/gl.h>
#include <lua.h>


#ifdef main
#undef main
#endif /* main */

using namespace std;

#define WinWidth 1280
#define WinHeight 720

int main(int argc, char *argv[])
{
    SDL_Window *window = NULL;
    SDL_Renderer *renderer = NULL;

    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    uint32_t windowFlags = SDL_WINDOW_OPENGL;
    window = SDL_CreateWindow("Runtime", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WinWidth, WinHeight, windowFlags);
    assert(window);
    SDL_GLContext Context = SDL_GL_CreateContext(window);

    bool Running = true;
    bool FullScreen = false;
    while (Running)
    {
        SDL_Event Event;
        while (SDL_PollEvent(&Event))
        {
            if (Event.type == SDL_KEYDOWN)
            {
                switch (Event.key.keysym.sym)
                {
                case SDLK_ESCAPE:
                    Running = 0;
                    break;
                case 'f':
                    FullScreen = !FullScreen;
                    if (FullScreen)
                    {
                        SDL_SetWindowFullscreen(window, windowFlags | SDL_WINDOW_FULLSCREEN_DESKTOP);
                    }
                    else
                    {
                        SDL_SetWindowFullscreen(window, windowFlags);
                    }
                    break;
                default:
                    break;
                }
            }
            else if (Event.type == SDL_QUIT)
            {
                Running = 0;
            }
        }

        glViewport(0, 0, WinWidth, WinHeight);
        glClearColor(0.f, 0.f, 0.f, 0.f);
        glClear(GL_COLOR_BUFFER_BIT);

        SDL_GL_SwapWindow(window); //Update Window Context
    }
    return 0;
}

这是我现在的整个项目结构:

项目无法找到并打开 lua.h。 (文件打不开)

安装的Luajit版本为:

mingw64/mingw-w64-x86_64-luajit 2.1.0_beta3-2 [installed]
    Just-in-time compiler and drop-in replacement for Lua 5.1 (mingw-w64)

这是 cmake 输出:

[main] Configuring folder: TileGameStudio_Runtime 
[proc] Executing command: C:\msys64\mingw64\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\msys64\mingw64\bin\x86_64-w64-mingw32-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\msys64\mingw64\bin\x86_64-w64-mingw32-g++.exe -He:/Game_Design/TileGameStudio/TileGameStudio_Runtime -Be:/Game_Design/TileGameStudio/TileGameStudio_Runtime/CMakeBuild -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] CMake Error at C:/msys64/mingw64/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
[cmake]   Could NOT find Lua51 (missing: LUA_INCLUDE_DIR)
[cmake] Call Stack (most recent call first):
[cmake]   C:/msys64/mingw64/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
[cmake]   C:/msys64/mingw64/share/cmake-3.20/Modules/FindLua51.cmake:80 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
[cmake]   CMakeLists.txt:30 (find_package)
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[cmake] See also "E:/Game_Design/TileGameStudio/TileGameStudio_Runtime/CMakeBuild/CMakeFiles/CMakeOutput.log".

我为自己找到了答案。 错误似乎是随机出现的。重新启动 Vscode 2-3 次后,错误随机出现或不出现。 IDE

似乎有问题