c++ - OpenGL GLFW 构建错误或链接错误

标签 c++ opengl visual-studio-2012 glfw cmake-gui

有人可以帮助我让 GLFW 在 Visual Studio 2012 中工作吗?我无法通过初始编译。我不断收到 GLFW 函数 Unresolved external 问题。问题是,我不知道我做错了什么。我可以:

  • 使用 CMake 不正确地制作源文件。
  • 图书馆 build 不当。
  • 不正确地链接到 GLFW 库。

我不确定是哪一个,所以我只列出我所做的:

1. Downloaded glfw-3.1.1 from glfw.org.
2. Used CMake GUI to make the source files.
    a. Set source code to C:/GLFW/glfw-3.1.1
    b. Set "Where to build the binaries" to C:/GLFW/glfw-3.1.1/GLFW_Built (I created that directory)
    c. Clicked "Configure" 
    d. Selected "Visual Studio 11 2012" as the generator (I also tried "Visual Studio 11 2012 Win64," but it gave me the same results. The ARM one wouldn't build at all)
    e. Left "Use default native compilers" selected.
    f. Clicked "Finish."
    g. Set "BUILD_SHARED_LIBS" to true (checked).
    h. Clicked "Generate."
3. Built the library.
    a. Went to C:\GLFW\glfw-3.1.1\GLFW_Built and opened GLFW.sln in Visual Studio 2012.
    b. Clicked BUILD->Build ALL_BUILD (worked fine, no errors)
4. Created a new blank project in Visual Studio 2012.
5. Went into the properties pages and set:
    a. Configuration Properties/VC++ Directories/Include Directories: added "C:\GLFW\glfw-3.1.1\include"
    b. Configuration Properties/VC++ Directories/Library Directories: added "C:\GLFW\glfw-3.1.1\GLFW_Built\CMakeFiles\Export\lib" (it was the only lib folder I saw) and "C:\GLFW\glfw-3.1.1\GLFW_Built\src\Debug" (contains glfw3.dll and glfw3dll.lib)
    c. Configuration Properties/C/C++/Additional Include Directories: added "C:\GLFW\glfw-3.1.1\include"
    d. Configuration Properties/Linker/General: added "C:\GLFW\glfw-3.1.1\GLFW_Built\src\Debug
    e. Configuration Properties/Linker/Input: added "glfw3dll.lib" and "opengl32.lib"

对于我的代码,我在 main.cpp 中只有一个非常简单的测试程序:

#include <GLFW/glfw3.h>
#include <thread>

int main()
{
    glfwInit();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    glfwTerminate();
}

但我总是在输出中得到这个:

1>main.obj : error LNK2019: unresolved external symbol _glfwInit referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _glfwTerminate referenced in function _main
1>C:\Users\Adam\Desktop\Revenant\Revenant\Debug\Revenant.exe : fatal error LNK1120: 2 unresolved externals

最佳答案

将 GLFW 解压缩到 C:\glfw-src(这样压缩包中的 CMakeLists.txt 位于 C:\glfw-src\CMakeLists.txt )

  • cd C:\glfw-src
  • mkdir 构建
  • cd 构建
  • cmake-gui ../
  • 按配置,选择工具链
  • CMAKE_INSTALL_PREFIX设置为C:/glfw
  • 创建c:\glfw目录
  • 再次按配置
  • 按生成
  • 在 Visual Studio 中打开 build\GLFW.sln
  • 构建 ALL_BUILD 项目的 Release 版本
  • 构建 INSTALL 项目的发布版本

C:\glfw 现在应该包含 includelib 目录。

假设一个全新的“空项目”解决方案:

  • 编译器:将 c:\glfw\include 添加到 /I(配置属性 -> C/C++ -> 常规 -> 附加包含目录)
  • 链接器:将 c:\glfw\lib 添加到 /LIBPATH(配置属性 -> 链接器 -> 常规 -> 其他库目录)并添加 glfw3 .libopengl32.lib/DYNAMICBASE(配置属性 -> 链接器 -> 输入 -> 附加依赖项)

关于c++ - OpenGL GLFW 构建错误或链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30419904/

相关文章:

c++ - 不包含 key 时的 std::map::lower_bound 或 std::map::upper_bound?

c++ - g++ 4.8.2 在列表方法参数默认初始化时出错

c++ - '对象' : 'struct/class' type redefinition WITH header guards. 如何修复?

c - Visual Studio 2015 与 2012 对比

c++ - 实现 64 位 Mersenne Twister - 定义

opengl - WebGL 与 openGL 中的着色器?

c++ - OpenGL glDrawElements 只有黑屏

c - 为 SDL 1.2 创建当前 OpenGL 上下文

visual-studio-2012 - 升级到Visual Studio 2012后,链接文件的MSBuild错误 “CSC : error CS2001: Source File ' x'找不到”

c# - 此项目(Visual Studio 2012 Express)与当前版本的 Visual Studio 2013 Express 不兼容