CMake 和 Conan:致命错误 - 没有这样的文件或目录

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

我在构建 C++ 项目时遇到问题,需要一些帮助来解决它。我已经建立了一个使用 Conan 管理依赖项的项目,并且创建了一个 CMakeLists.txt 文件来链接这些库。但是,当我尝试使用 cmake 命令构建项目时,遇到以下错误:

C:/Users/veltr/Desktop/Exercises/OpenGLProject/Application.cpp:1:10: fatal error: GLEW/glew.h: No such file or directory
    1 | #include <GLEW/glew.h>
      |          ^~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/OpenGLProject.dir/Application.cpp.obj] Błąd 1
make[1]: *** [CMakeFiles/OpenGLProject.dir/all] Błąd 2

我有一个 conan.txt 文件,在其中指定要使用 conan install 安装的依赖项。 我创建了一个 CMakeLists.txt 文件来链接所需的库。 该项目似乎构建没有错误,但当我尝试编译测试代码来检查功能时,我遇到了提到的致命错误。

  1. conanfile.txt

    [requires]
    opengl/system
    glfw/3.3.8
    glew/2.2.0
    [generators]
    CMakeDeps
    CMakeToolchain
    [layout]
    cmake_layout
    
  2. CMakeLists.txt

    cmake_minimum_required(VERSION 3.27)
    project(OpenGLProject)
    
    find_package(glfw3 REQUIRED)
    find_package(OpenGL REQUIRED)
    find_package(GLEW REQUIRED)
    
    add_executable(${PROJECT_NAME} Application.cpp)
    
    target_link_libraries(${PROJECT_NAME} glfw)
    target_link_libraries(${PROJECT_NAME} GLEW::glew_s)
    target_link_libraries(${PROJECT_NAME} OpenGL::GL)
    
  3. 应用程序.cpp

    #include <GLEW/glew.h>
    #include <GLFW/glfw3.h>
    
    int main(void)
    {
        GLFWwindow* window;
    
        /* Initialize the library */
        if (!glfwInit())
            return -1;
    
        /* Create a windowed mode window and its OpenGL context */
        window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
        if (!window)
        {
            glfwTerminate();
            return -1;
        }
    
        /* Make the window's context current */
        glfwMakeContextCurrent(window);
    
        /* Loop until the user closes the window */
        while (!glfwWindowShouldClose(window))
        {
            /* Render here */
            glClear(GL_COLOR_BUFFER_BIT);
    
            glBegin(GL_TRIANGLES);
            glVertex2f(-0.5f,-0.5f);
            glVertex2f(0.0f,  0.5f);
            glVertex2f(0.5f, -0.5f);
            glEnd();
    
            /* Swap front and back buffers */
            glfwSwapBuffers(window);
    
            /* Poll for and process events */
            glfwPollEvents();
        }
    
        glfwTerminate();
        return 0;
    }
    

例如,“fmt”库 - 通过遵循类似的步骤 - 可以正确构建和工作,并且我可以使用其中所有可用的 #include 指令。上述所有库都可以在 conan.io 上找到,因此它们应该是正确的。我做错了什么或不正确吗?

//编辑 经过多次尝试解决问题,我找不到解决方案,最终使问题变得更糟。为了回到之前的状态,我必须添加通过 Conan 下载的包的绝对路径,以及我从网站下载的 GLEW 库。这是我更新的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.27)
project(OpenGLProject)

# Set the paths to GLFW and OpenGL package configuration files
set(glfw3_DIR "C:/Users/veltr/Desktop/Exercises/OpenGLProject/build/Release/generators")
set(opengl_system_DIR "C:/Users/veltr/Desktop/Exercises/OpenGLProject/build/Release/generators")

# Find and link GLFW
find_package(glfw3 REQUIRED)

# Set the path to GLEW package configuration files 
set(GLEW_ROOT "C:/Users/veltr/Downloads/glew-2.1.0-win32/glew-2.1.0")
set(GLEW_INCLUDE_DIRS "${GLEW_ROOT}/include")
set(GLEW_LIBRARIES "${GLEW_ROOT}/lib/Release/Win32/glew32s.lib")

# Find and link GLEW
find_package(GLEW REQUIRED)

# Find and link OpenGL
find_package(OpenGL REQUIRED)

add_executable(${PROJECT_NAME} Application.cpp)

# Link the libraries to your project
target_link_libraries(${PROJECT_NAME} glfw)
target_link_libraries(${PROJECT_NAME} GLEW::GLEW)
target_link_libraries(${PROJECT_NAME} OpenGL::GL)

现在,当我构建项目时,我得到以下输出:

cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ..
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is GNU 13.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/msys64/mingw64/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/msys64/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Conan: Target declared 'glfw'
-- Conan: Target declared 'opengl::opengl'
-- Found GLEW: C:/Users/veltr/Downloads/glew-2.1.0-win32/glew-2.1.0/include (found version "2.1.0") 
-- Found OpenGL: opengl32
-- Configuring done (3.1s)
-- Generating done (0.1s)
-- Build files have been written to: C:/Users/veltr/Desktop/Exercises/OpenGLProject/build

如您所见,链接器找到了 GLEW,但不幸的是,当我使用 make 编译项目时,我仍然收到相同的错误。

//编辑#2

cmake_minimum_required(VERSION 3.27)
project(OpenGLProject)
set(glfw3_DIR "C:/Users/veltr/Desktop/Exercises/OpenGLProject/build/Release/generators")
set(opengl_system_DIR "C:/Users/veltr/Desktop/Exercises/OpenGLProject/build/Release/generators")

find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIRS})

add_executable(${PROJECT_NAME} Application.cpp)

target_link_libraries(${PROJECT_NAME} glfw)
target_link_libraries(${PROJECT_NAME} GLEW::glew_s)
target_link_libraries(${PROJECT_NAME} OpenGL::GL)

输出:

(...)
-- Detecting CXX compile features - done
-- Conan: Target declared 'glfw'
-- Conan: Target declared 'opengl::opengl'
-- Found OpenGL: opengl32   
CMake Error at C:/Program Files/CMake/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find GLEW (missing: GLEW_INCLUDE_DIRS GLEW_LIBRARIES)
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.27/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
  C:/Program Files/CMake/share/cmake-3.27/Modules/FindGLEW.cmake:238 (find_package_handle_standard_args)
  CMakeLists.txt:8 (find_package)
(...)
windows opengl cmake lib conan
1个回答
0
投票

我认为问题在于您没有在 Conan 工作区中正确包含 GLEW 标头。

当您使用 Conan 安装依赖项时,头文件不会自动提供给您的项目。您有几个选项可以解决此问题:

将 GLEW 包含目录添加到您的 CMakeLists.txt:

# After find_package(GLEW ...)
include_directories(${GLEW_INCLUDE_DIRS})

将 GLEW 包含目录添加到您的 C++ 编译器包含路径:

add_executable(${PROJECT_NAME} Application.cpp)

target_include_directories(${PROJECT_NAME} PUBLIC ${GLEW_INCLUDE_DIRS})

target_link_libraries(${PROJECT_NAME} ...)

使用 conan_basic_setup() CMake 宏为您设置所有包含目录:

include(conanbuildinfo.cmake)
conan_basic_setup()

add_executable(${PROJECT_NAME} Application.cpp)

target_link_libraries(${PROJECT_NAME} ...)
© www.soinside.com 2019 - 2024. All rights reserved.