简体   繁体   中英

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).

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):

#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:

<?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):

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. 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

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.

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. Next go to Linker > Input and add the libraries to the list of 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.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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