CLion CMake ld:找不到库 + Vulkan 和 OpenGL 问题

CLion CMake ld: library not found + Vulkan and OpenGL questions

我正在尝试为即将进行的项目使用 GLFW 和 Vulkan 进行基本设置。我试图让一个简单的 HelloTriangle Example 起作用。当 Vulkan 库正常构建时,GLFW 在 运行ning 程序上抛出 ld: library not found for -lglfw3。我也不确定我的 HelloTriangle 示例是否是一个工作示例;这是我在测试 Vulkan 时必须看到的最基本的示例。 这是我的 CMakeList.txt:

cmake_minimum_required(VERSION 3.16)
project(OpenGLRun)

set(CMAKE_CXX_STANDARD 14)
set(glfw3_DIR /usr/local/Cellar/glfw/3.3.2/lib/cmake/glfw3)

add_executable(OpenGLRun main.cpp)

find_package(vulkan REQUIRED)
find_package(glm REQUIRED)
find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)

target_link_libraries(OpenGLRun Vulkan::Vulkan)
target_link_libraries(OpenGLRun glm)
target_link_libraries(OpenGLRun OpenGL::GL)
target_link_libraries(OpenGLRun glfw3)

还有我试图 运行 用来证明一切正常的 main.cpp 示例代码:

#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

#include <iostream>
#include <stdexcept>
#include <cstdlib>

const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;

class HelloTriangleApplication {
public:
    void run() {
        initWindow();
        initVulkan();
        mainLoop();
        cleanup();
    }

    private:

        GLFWwindow* window;

        void initWindow() {
            glfwInit();

            glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
            glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

            GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
        }

private:
    void initVulkan() {

    }

    void mainLoop() {
        while (!glfwWindowShouldClose(window)) {
            glfwPollEvents();
        }
    }

    void cleanup() {
        glfwDestroyWindow(window);

        glfwTerminate();
    }
};

int main() {
    HelloTriangleApplication app;

    try {
        app.run();
    } catch (const std::exception& e) {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

最后,对于未来,您会推荐 C++/CMake 中的包管理器吗?

提前致谢

CMake 中的 GLFW Build Guide suggests that whether you are compiling and linking GLFW along with your application, or linking an installed GLFW to your application, you can link GLFW to your application using the glfw target (not glfw3). Change the target_link_libraries() 命令:

target_link_libraries(OpenGLRun PRIVATE glfw)

注意,在使用诸如此类的基于目标的命令时,您应该始终提供范围参数,以告诉 CMake 这是构建要求、使用要求还是两个都。