修复LNK2019:无法解决的外部符号与freeglut

Fix LNK2019: unresolved external symbol with freeglut

本文关键字:外部 符号 freeglut 解决 LNK2019 修复      更新时间:2023-10-16

使用本手册,我试图在我的计算机上编译免费的OpenGL应用程序。这意味着I:

  • 从freeglut存档文件复制到MS程序文件文件夹
  • 在附加依赖项
  • 中指定freeglut.lib
  • 显式设置freeglut。库文件夹
  • 中的lib路径

但都不起作用。c++头文件是正确的,但库不是-所以我得到这些错误:

1>OpenglTest.obj : error LNK2019: unresolved external symbol __imp__glutMainLoop@0 referenced in function _wmain
1>OpenglTest.obj : error LNK2019: unresolved external symbol __imp__glutSpecialFunc@4 referenced in function _wmain
1>OpenglTest.obj : error LNK2019: unresolved external symbol "void __cdecl specialKeys(int,int,int)" (?specialKeys@@YAXHHH@Z) referenced in function _wmain
1>OpenglTest.obj : error LNK2019: unresolved external symbol __imp__glutDisplayFunc@4 referenced in function _wmain
1>OpenglTest.obj : error LNK2019: unresolved external symbol __imp__glutInitDisplayMode@4 referenced in function _wmain
1>OpenglTest.obj : error LNK2019: unresolved external symbol __imp____glutInitWithExit@12 referenced in function _glutInit_ATEXIT_HACK@8
1>OpenglTest.obj : error LNK2019: unresolved external symbol __imp____glutCreateWindowWithExit@8 referenced in function _glutCreateWindow_ATEXIT_HACK@4
1>OpenglTest.obj : error LNK2019: unresolved external symbol __imp__glutSwapBuffers@0 referenced in function "void __cdecl display(void)" (?display@@YAXXZ)

我的问题是:

  1. 你能猜出是哪里出了问题吗?(不容易,但我不能找到这个明显)
  2. 是否有一些其他的手动谷歌没有找到,但比我使用的一个更好?
  3. 是否有我应该下载的其他档案?

我的c++代码来自这里。所有函数都可以在freeglut头文件中找到。

这似乎是一个C vs C++链接问题。您正在从那里编译C source代码作为C++ code(也许您将其保存为c++特定的扩展并且编译器假设它是c++)。

c++会混淆函数名。例如,__imp__glutMainLoop@0很可能是源代码中对glutMainLoop的调用。

库必须来自C代码。因此,它将有一个简单的glutMainLoop版本的函数。这就是为什么找不到它的原因。

解决方案是将代码编译为C代码或使用外部"C"。

编辑:一些关于如何处理C/c++链接问题的链接(从注释中移出)。

  • http://www.thegeekstuff.com/2013/01/mix-c-and-cpp/
  • 结合c++和C -如何#ifdef __cplusplus工作?
  • http://www.parashift.com/c + + -faq-lite/c-calls-cpp.html