問題
どうやら多くの人 (Stackoverflow を含む) と同様に、Visual Studio の GLFW ライブラリを使用して C++ OpenGL コードをコンパイル、特にリンクする際に問題が発生しました (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>
回避策
今、私はこの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 ver.を使っているので使っていますが。3.とにかく、その行は魔法のようにエラーなしですべてを修正します。私の質問は:なぜですか?プリプロセッサに .lib を強制的に検索 (および検索) させる必要があるのはなぜですか?
この回避策を使用する必要がないように、プロジェクトの設定またはコードを変更できるものはありますか?