【发布时间】:2018-09-19 10:04:27
【问题描述】:
我是一名 C++ 初学者,具有一定的 Java 经验,尝试在 Windows 上使用 GLM、GLFW 和 Vulkan 设置项目。这将是我第一次接触像 C++ 这样的低级语言。我在让编译器将 Vulkan 和 GLFW 库链接到我的项目时遇到了很多麻烦。我正在按照教程here at vulkan-tutorial.org 开始。这是 main.cpp 中的代码:
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <iostream>
int main() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::cout << extensionCount << " extensions supported\n";
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
以下是用于编译它的命令:
g++ -std=c++11 -fexceptions -g -IC:/glfw-3.2.1/include -IC:/glm-0.9.9.1/glm -IC:/VulkanSDK/1.1.82.1/Include -IC:/glfw-3.2.1/include -c "src/main.cpp"
g++ -LC:/glfw-3.2.1/lib-mingw-w64 -LC:/VulkanSDK/1.1.82.1/Lib -o VulkanTest.exe main.o -lglfw3 -lvulkan-1
第一个命令成功地将 .cpp 编译成 .o,但是第二个命令给了我来自链接器的错误。我对 Vulkan 或 GLFW 成员的每一次引用都是未定义的。 (路径已被缩短以便于阅读)
[omitted]/src/main.cpp:12: undefined reference to `glfwInit'
[omitted]/src/main.cpp:14: undefined reference to `glfwWindowHint'
[omitted]/src/main.cpp:15: undefined reference to `glfwCreateWindow'
[omitted]/src/main.cpp:18: undefined reference to `vkEnumerateInstanceExtensionProperties@12'
[omitted]/src/main.cpp:26: undefined reference to `glfwWindowShouldClose'
[omitted]/src/main.cpp:27: undefined reference to `glfwPollEvents'
[omitted]/src/main.cpp:30: undefined reference to `glfwDestroyWindow'
[omitted]/src/main.cpp:32: undefined reference to `glfwTerminate'
似乎链接器找不到我用 -L 和 -l 提供的库文件,但如果我将 -lglfw3 更改为 -llibglfw3.a 或 -lglwf3.dll,我会得到:
[omitted]/mingw32/bin/ld.exe: cannot find -llibglfw3.a
或
[omitted]/mingw32/bin/ld.exe: cannot find -lglfw3.dll
让我认为链接器最初确实找到了库,因为它没有抱怨无法找到库 - 但为什么它不能找到 GLFW / Vulkan 函数引用的来源?我不知道发生了什么。找到库文件了吗?
我正在使用 GLFW 3.2.1、Vulkan SDK 1.1.82.1、MingW GCC 版本 6.3.0,并且我在 Windows 10 Pro 64 位上运行。
【问题讨论】:
-
链接器正在寻找满足
-lglfw3 -lvulkan-1选项的库,但它找到的库没有提供它需要解析的符号引用的定义。它们不是您的编译器的正确库。如果不确切知道您的编译器是什么(根据g++ -v --version的输出)、您从哪里获得这些库以及如何安装它们,我们无法说出原因。 -
我在上面列出了 GLFW、Vulkan 和 MingW 的版本。我正在使用GLFW 3.2.1 64-bit windows pre-compiled binaries、LunarG Vulkan SDK 1.1.82.1 和 MingW GCC 6.3.0(从“gcc --version”的输出返回)