0

Using this manual, I tried to get freeglut OpenGL application to compile on my computer. This means I:

  • Copied files from freeglut archive to MS program files folder
  • Specified freeglut.lib in additional dependencies
  • Set explicitly the freeglut.lib path in libraries folders

But all takes no effect. The C++ header files are found right, but the libraries not - so I get these errors:

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)

So my questions:

  1. Could you guess what may be wrong? (not easy, but I cannot find this out obviously)
  2. Is there some other manual google didn't find, but better than the one I used?
  3. Is there some other archive I should download?

My C++ code comes from here. All functions are well found in freeglut header files. Just the libraries are missing.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778

1 Answers1

1

This seems like a C vs C++ linkage issue. You are compiling a C source code from there as a C++ code (maybe you saved it with C++ specific extensions and the compiler assumed it is C++).

C++ will mangle function names. For example __imp__glutMainLoop@0 is most probabley a call to glutMainLoop in your source code.

The library must have been from a C code. Hence it will have a non mangled version of the function simply as glutMainLoop. That's why it can not be found.

The solution is either compile your code as C code or use extern "C".

EDIT: Some Links on how to deal with with C/C++ linkage issues (Moved from the comments).

Community
  • 1
  • 1
  • The `extern "C"` is to be placed in the C function defenitions in the library? – Tomáš Zato Mar 30 '13 at 22:57
  • For your case I think you just have to compile your code as C. Assuming you don't need any C++ features in the code. However if you need to use C++ for our own code then you need to wrap the functions you use with extern. See this on how to do it. http://www.thegeekstuff.com/2013/01/mix-c-and-cpp/ –  Mar 30 '13 at 23:45
  • The first example is your case. –  Mar 30 '13 at 23:51
  • 1
    Your answer has given me a lot, but it showed up that all problems were caused by using x64 precompiled .dll and .lib. But I'm happy to know this about C and C++, thank you. – Tomáš Zato Mar 30 '13 at 23:52
  • Glad I could help :). Just curious, So your code was compiled as C? –  Mar 30 '13 at 23:53
  • By the way, can whole included file be wrapped in extern? `extern "C"{#include "c_file.h"}` (imagine some new lines here) – Tomáš Zato Mar 30 '13 at 23:54
  • Yes. But add an ifedf __cplusplus to it. So that it will compile accordingly when it is C++. like this. http://stackoverflow.com/questions/3789340/combining-c-and-c-how-does-ifdef-cplusplus-work –  Mar 30 '13 at 23:55
  • And this should help you if you need more in depth. http://www.parashift.com/c++-faq-lite/c-calls-cpp.html –  Mar 30 '13 at 23:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27256/discussion-between-tomas-zato-and-stardust) – Tomáš Zato Mar 31 '13 at 00:17