【问题标题】:Compiling GLFW with Visual Studio 2012使用 Visual Studio 2012 编译 GLFW
【发布时间】:2013-08-12 23:16:50
【问题描述】:

问题

显然很多人(包括 Stackoverflow 上的这里),我在编译,特别是链接我的 C++ OpenGL 代码与 Visual Studio 上的 GLFW 库时遇到了一些麻烦(我使用的是 v11.0 AKA 2012 Ultimate)。

这是错误列表:

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

这是我的 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;
}

最后是项目 .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>

解决方法

现在我确实在this 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")

虽然我使用#pragma comment(lib, "glfw3.lib"),因为我使用的是 GLFW 版本。 3. 不管怎样,那条线神奇地修复了一切,没有错误。我的问题是:为什么?为什么我必须强制预处理器查找(并找到).lib

我可以对我的项目设置或代码进行任何更改以不必使用此解决方法吗?

【问题讨论】:

  • 你糊涂了,这和前处理器无关,这是链接器错误,不是变通方法,是要求。
  • 有要求吗?真奇怪,我从来不用“#pragma”来指定库...?
  • 你不需要使用#pragma,你可以在你的项目中添加一个包含.lib文件的搜索路径。
  • 那我该怎么做呢?
  • this

标签: c++ opengl visual-studio-2012 linker glfw


【解决方案1】:

正如 yngum 在 cmets 中所说,这与预处理器无关,它只是将库链接到项目的一种方式。

另一种方法(前提是您的路径设置正确)相当简单。通过转到 PROJECT > (Your project name) Properties 或在 Solution Explorer 中右键单击项目并选择属性来打开您的项目设置。接下来转到 Linker > Input 并将库添加到 Additional Dependencies 列表中。您可能还希望将左上角的下拉列表更改为 所有配置,以便它对调试和发布版本都执行此操作。

【讨论】:

    猜你喜欢
    • 2014-07-10
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 2015-04-12
    • 2012-10-19
    相关资源
    最近更新 更多