【问题标题】:Visual Studio 2012 C++ OpenGL GLFW linker error [duplicate]Visual Studio 2012 C++ OpenGL GLFW 链接器错误 [重复]
【发布时间】:2012-11-28 00:50:03
【问题描述】:

我正在使用 Visual Studio 2010 Express 上的 OpenGL 在 C++ 中制作二维横向滚动条。我正在尝试编译我的代码,并且它可以正确构建,但是我在 main() 函数中初始化的 GLFW 函数出现链接器错误。这是我的代码:

#include <iostream>
#include <ctime>


#include <GL\glfw.h>


#include "Player.h"


void render();
void update();


Player Player1;
 //Cross platform sleep implementation
void _sleep(double ms)
{
    double st = clock();
    if(ms <= 0)
            ms = 10;
    while(clock() < (ms + st));
}

int main(int argc, char** argv)
{
    std::cout <<"Loading Prized Fighter"<< std::endl;
    glfwInit(); //Initialize GLFW

    //Create GLFW window
    if(glfwOpenWindow(800, 600, 5, 6, 5, 0, 8, 0, GLFW_WINDOW) != GL_TRUE)
        std::cout << "Error creating window!" << std::endl;

    //Set window title
    glfwSetWindowTitle("Prized Fighter");

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //Start main game loop
    //This calls the functions which update objects and render them to the screen
    while(true)
    {
        update();
        render();

        glfwSwapBuffers(); //Switch buffers (double rendering)
        _sleep(10.0); //Let a bit of CPU time for other processes
    }

    return 0;
}

/*
     - Render function -
      Used to draw objects to the screen
*/
void render()
{
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Color to clear the screen
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

     //TODO: Draw stuff here
     Player1.draw();
}

/*
     - Update function -
     Updates objects; states, locations, etc
*/
void update()
{
     //TODO: Update objects here
     Player1.update();
}

编译时出现以下错误:

1>----- 构建开始:项目:Priced Fighter,配置:调试 Win32 ------ 1>main.obj:错误 LNK2019:>function _WinMain@8

中引用的未解析外部符号 _glfwSwapBuffers

1>main.obj:错误 LNK2019:>function _WinMain@8

中引用的未解析外部符号 _glfwSetWindowTitle

1>main.obj:错误 LNK2019:>function _WinMain@8

中引用的未解析外部符号 _glfwOpenWindow

1>main.obj : 错误 LNK2019: 函数 >_WinMain@8 中引用的未解析外部符号 _glfwInit

1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol WinMain@16 >在函数_中引用_tmainCRTStartup

1>c:\users\brennan\documents\visual studio 2010\Projects\Prized Fighter\Debug\Prized >Fighter.exe : 致命错误 LNK1120: 5 unresolved externals ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

链接过程中出了什么问题 - 代码中是否有错误?

【问题讨论】:

  • 您是否链接到相应的库?

标签: c++ visual-c++ opengl glfw


【解决方案1】:

看来您需要链接到:

glfw.lib 
opengl32.lib

发行说明值得一读:here

【讨论】:

  • 我现在已经根据 GLFW 站点链接了 GLFW.lib 文件,但仍然出现错误。 "警告 C4007: 'WinMain' 必须是 '_stdcall'
  • 还有一个链接错误:unresolved external symbol _main
  • 它是您创建的控制台应用程序(或窗口)。您可以通过将“main()”更改为“_main()”来使其工作;或者相反,转到链接器选项并将入口点显式设置为“main()”;链接器 -> 高级 -> 入口点。
【解决方案2】:

您收到这些错误是因为您没有链接到 GFWL 库,或者没有正确链接到它。该库包含您尝试调用的函数的定义。

GFWL 自述文件的第 4.2 节解释了链接时要使用哪些库;如果您是静态链接(看起来像)或使用 DLL,您需要的步骤会有所不同。

【讨论】:

    【解决方案3】:

    对于已经链接到正确库并且可能对 C++ 项目的 GLAD 文件有类似问题的未来读者:

    我只需要通过“属性”选项卡将glad.c文件包含在项目中(当您单击文件时会弹出该文件,您可以通过“视图”>“属性窗口”启用属性选项卡)。

    确保“包含在项目中”设置为 True:

    这就是为什么我有未解析的glad.c 外部符号的原因,这里的任何人都可能会发现它有用。

    【讨论】:

      【解决方案4】:

      据我所知,我在设置中正确链接了所有内容,但由于某种原因,Visual Studio 2017 仍然没有在我的解决方案中包含glad.c。即使它被放入文件夹中,它也只会在我选择文件夹视图而不是解决方案视图时出现。

      最后我只是在我的预处理器语句之后键入了#include "glad.c",它对我有用。

      【讨论】:

        猜你喜欢
        • 2013-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-08
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多