简体   繁体   English

使用Visual Studio 2012编译GLFW

[英]Compiling GLFW with Visual Studio 2012

The Issue 问题

As apparently many people (including here on Stackoverflow), I have run into some trouble compiling, and specifically linking, my C++ OpenGL code with the GLFW library on Visual Studio (I am using v11.0 AKA 2012 Ultimate). 显然很多人(包括在这里的Stackoverflow),我在使用Visual Studio上的GLFW库编译和特别链接我的C ++ OpenGL代码时遇到了一些麻烦(我使用的是v11.0 AKA 2012 Ultimate)。

Here is the error list: 这是错误列表:

Error   1   error LNK2019: unresolved external symbol __imp__glfwInit referenced in function _main  C:\Users\Dennis\MyOpenGL\OpenGL1\OpenGL1\Main.obj   OpenGL1
Error   2   error LNK2019: unresolved external symbol __imp__glfwTerminate referenced in function _main C:\Users\Dennis\MyOpenGL\OpenGL1\OpenGL1\Main.obj   OpenGL1
Error   3   error LNK2019: unresolved external symbol __imp__glfwCreateWindow referenced in function _main  C:\Users\Dennis\MyOpenGL\OpenGL1\OpenGL1\Main.obj   OpenGL1
Error   4   error LNK2019: unresolved external symbol __imp__glfwWindowShouldClose referenced in function _main C:\Users\Dennis\MyOpenGL\OpenGL1\OpenGL1\Main.obj   OpenGL1
Error   5   error LNK2019: unresolved external symbol __imp__glfwPollEvents referenced in function _main    C:\Users\Dennis\MyOpenGL\OpenGL1\OpenGL1\Main.obj   OpenGL1
Error   6   error LNK2019: unresolved external symbol __imp__glfwMakeContextCurrent referenced in function _main    C:\Users\Dennis\MyOpenGL\OpenGL1\OpenGL1\Main.obj   OpenGL1
Error   7   error LNK2019: unresolved external symbol __imp__glfwSwapBuffers referenced in function _main   C:\Users\Dennis\MyOpenGL\OpenGL1\OpenGL1\Main.obj   OpenGL1

Here is my Main.cpp file (containing all my code and the workaround I found): 这是我的Main.cpp文件(包含我发现的所有代码和解决方法):

#include <GL/glfw3.h>

//The workaround:
#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
//End workaround

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 */

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

Finally the Project .XML: 最后是Project .XML:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|Win32">
      <Configuration>Debug</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|Win32">
      <Configuration>Release</Configuration>
      <Platform>Win32</Platform>
    </ProjectConfiguration>
  </ItemGroup>
  <PropertyGroup Label="Globals">
    <ProjectGuid>{D8015B21-ACEE-4F49-B2E3-42D93127981A}</ProjectGuid>
    <Keyword>Win32Proj</Keyword>
    <RootNamespace>OpenGL1</RootNamespace>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    <UseDebugLibraries>true</UseDebugLibraries>
    <PlatformToolset>v110</PlatformToolset>
    <CharacterSet>Unicode</CharacterSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    <UseDebugLibraries>false</UseDebugLibraries>
    <PlatformToolset>v110</PlatformToolset>
    <WholeProgramOptimization>true</WholeProgramOptimization>
    <CharacterSet>Unicode</CharacterSet>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ImportGroup Label="ExtensionSettings">
  </ImportGroup>
  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </ImportGroup>
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <LinkIncremental>true</LinkIncremental>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <LinkIncremental>false</LinkIncremental>
  </PropertyGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <ClCompile>
      <PrecompiledHeader>
      </PrecompiledHeader>
      <WarningLevel>Level3</WarningLevel>
      <Optimization>Disabled</Optimization>
      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <AdditionalIncludeDirectories>C:\Users\Dennis\Programming\Libraries\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    </ClCompile>
    <Link>
      <SubSystem>Console</SubSystem>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <AdditionalLibraryDirectories>C:\Users\Dennis\Programming\Libraries\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <ClCompile>
      <WarningLevel>Level3</WarningLevel>
      <PrecompiledHeader>
      </PrecompiledHeader>
      <Optimization>MaxSpeed</Optimization>
      <FunctionLevelLinking>true</FunctionLevelLinking>
      <IntrinsicFunctions>true</IntrinsicFunctions>
      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      <AdditionalIncludeDirectories>C:\Users\Dennis\Programming\Libraries\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
    </ClCompile>
    <Link>
      <SubSystem>Console</SubSystem>
      <GenerateDebugInformation>true</GenerateDebugInformation>
      <EnableCOMDATFolding>true</EnableCOMDATFolding>
      <OptimizeReferences>true</OptimizeReferences>
      <AdditionalLibraryDirectories>C:\Users\Dennis\Programming\Libraries\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
    </Link>
  </ItemDefinitionGroup>
  <ItemGroup>
    <ClCompile Include="Main.cpp" />
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
  </ImportGroup>
</Project>

The Workaround 解决方法

Now I did find a rather strange workaround in the answer to this stackoverflow question (credits to Vircung): 现在我确实在这个 stackoverflow问题的答案中找到了一个相当奇怪的解决方法(归功于Vircung):

Make sure you have glfw.dll in folder with your .exe file. 
If this won't help, add another library glu32.lib.

I use to add libraries in code by adding this before main function.
With this you see wich libraries you have linked without digging through
options and menus.

#pragma comment(lib, "GLFW.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")

Although I use #pragma comment(lib, "glfw3.lib") since I am using GLFW ver. 虽然我使用#pragma comment(lib, "glfw3.lib")因为我正在使用GLFW ver。 3. Anyway, that line magically fixes everything with no errors. 无论如何,那条线神奇地修复了一切,没有任何错误。 My question is: Why? 我的问题是:为什么? How come I have to force the pre-proccessor to look for (and find) the .lib 为什么我必须强制前处理器寻找(并找到).lib

Is there anything I can change to my project settings or code to not have to use this workaround? 有什么我可以更改为我的项目设置或代码,以便不必使用此变通方法吗?

As yngum says in the comments, this has nothing to do with the preprocessor, it is just one way of linking libraries to your project. 正如yngum在评论中所说,这与预处理器无关,它只是将库链接到项目的一种方式。

The other way (providing you have your paths set up properly) is fairly simple. 另一种方式(提供正确设置路径)非常简单。 Open your project settings by going to PROJECT > (Your project name) Properties or right click on the project in the Solution Explorer and select properties. 通过转到PROJECT>(您的项目名称)属性或在解决方案资源管理器中右键单击项目并选择属性来打开项目设置。 Next go to Linker > Input and add the libraries to the list of Additional Dependencies . 接下来转到Linker> Input并将库添加到Additional Dependencies列表中。 You might also want to change the dropdown in the upper top left to All configurations so it does this for both debug and release builds. 您可能还希望将左上角的下拉列表更改为所有配置,以便它为调试版本和发布版本执行此操作。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM