2012-11-28 67 views
1

我在C++中使用Visual Studio 2010 Express上的OpenGL制作了2d侧滚动器。我试图编译我的代码,并且它的构建正确,但是我在main()函数中初始化GLFW函数时出现链接器错误。这里是我的代码: Visual Studio 2012 C++ OpenGL 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> ------构建开始:项目:珍贵的战斗机,配置:调试的Win32 - ----- 1> main.obj:错误LNK2019:错误LNK2019:中引用>功能_WinMain @ 8

1> main.obj解析外部符号_glfwSwapBuffers解析外部符号_glfwSetWindowTitle引用在>功能_WinMain @ 8

1> main.obj:错误LNK2019:解析外部符号_glfwOpenWindow引用在>功能_WinMain @ 8

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

1> MSVCRTD.LIB(crtexew.obj):错误LNK2019:解析外部符号的WinMain @ 16>在函数引用_ _tmainCRTStartup

1> C:\用户\布伦南\文件\视觉工作室2010 \项目\ Priged Fighter \ Debug \ Prized> Fighter.exe:致命错误LNK1120:5个未解析的外部设备 === =======构建:0成功,1失败,0最新,0跳过==========

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

+0

你链接到适当的库吗? –

回答

1

它看起来像你需要对链接:

glfw.lib 
opengl32.lib 

这将是值得一读的发行说明:

+0

我已经根据GLFW网站链接了GLFW.lib文件,但我仍然收到错误消息。 “警告C4007:'WinMain'必须是'_stdcall' –

+0

另外,链接错误:无法解析的外部符号_main –

+0

它是您创建的控制台应用程序(或Windows)。您可能可以通过更改” main()“改为”_main()“;或者相反,转到链接器选项并明确设置入口点为”main()“;链接器 - >高级 - >入口点。 –

0

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

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