问题
I am making a 2d side-scroller in C++ using OpenGL on Visual Studio 2010 Express. I am trying to compile my code, and it builds properly, but I get linker errors for the GLFW functions I am initializing in the main() function. Here is my code:
#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();
}
When I compile, I get the following errors:
1>------ Build started: Project: Prized Fighter, Configuration: Debug Win32 ------ 1>main.obj : error LNK2019: unresolved external symbol _glfwSwapBuffers referenced in >function _WinMain@8
1>main.obj : error LNK2019: unresolved external symbol _glfwSetWindowTitle referenced in >function _WinMain@8
1>main.obj : error LNK2019: unresolved external symbol _glfwOpenWindow referenced in >function _WinMain@8
1>main.obj : error LNK2019: unresolved external symbol _glfwInit referenced in function >_WinMain@8
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol WinMain@16 >referenced in function __tmainCRTStartup
1>c:\users\brennan\documents\visual studio 2010\Projects\Prized Fighter\Debug\Prized >Fighter.exe : fatal error LNK1120: 5 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What is going wrong during the linking - is there an error in the code?
回答1:
It looks like you need to link against:
glfw.lib
opengl32.lib
It would be worth reading the release notes: here
回答2:
You're getting these errors because you're not linking to the GFWL library, or not linking to it correctly. That library contains definitions for the functions you're trying to call.
Section 4.2 of the GFWL readme file explains which libraries to use when linking; if you're statically linking (looks like you are) or using the DLL, the steps you need will be different.
回答3:
For future readers that already have linked against the correct libraries and might have a similar problem with GLAD files for C++ projects:
I simply had to include the glad.c file in the project via the Properties tab (Which should pop up whenever you click on a file, you can enable properties tab via View > Properties Window).
Make sure that "Included in project" is set to True:

This was the reason why I had unresolved external symbols for glad.c, which anyone in here might find useful.
回答4:
I had everything linked in the settings correctly as far as I can tell but still for some reason visual studio 2017 was not including glad.c in my solution. Even though it is dropped into the folder it only appears if I select folder view, not solution view.
Ultimately I just typed #include "glad.c" after my pre-processor statements and it works for me.
来源:https://stackoverflow.com/questions/13595956/visual-studio-2012-c-opengl-glfw-linker-error