Search code examples
c++visual-studiostatic-linkingopenscenegraph

Unresolved externals on static linking OpenSceneGraph libraries in Visual Studio


I'm using OpenSceneGraph libraries in my application and I want to link them statically, so I don't have to carry DLLs with binary. I have downloaded OpenSceneGraph 3.4.0 sources and generated Visual Studio project by cmake. I did disabled DYNAMIC_OPENSCENEGRAPH and DYNAMIC_OPENTHREADS in cmake GUI. After that I successfully compiled OSG and have static libraries (.lib). In my application I added include and library directories in project properties -> VC++ Directories -> Include Directories & Library Directories (I'm using Visual Studio 2013 by the way). I also added directory with OSG .lib's in Linker -> General -> Additional Library Directories and typed all required libs in Linker -> Input -> Additional Dependencies:

ot20-OpenThreads.lib
osg130-osg.lib
osg130-osgUtil.lib
osg130-osgGA.lib
osg130-osgDB.lib
osg130-osgViewer.lib
osg130-osgText.lib
opengl32.lib
osgdb_3ds.lib
osgdb_freetype.lib
osgdb_png.lib

And I'm still getting unresolved externals errors for OSG symbols. I also tried setting General -> Character Set to Unicode but it didn't help (in OSG project generated by cmake I have Multi-Byte Character anyway). I don't have any ideas how to solve that now, it's my first time compiling application with static linked libraries.

I would really appreciate any help.

P.s. Sorry for no code, I can't provide it but I try to give as much details as I can. With dynamic linking I can build my application without any problems.


Solution

  • I managed to solve this problem myself. I was missing two more libraries in Linker -> Input -> Additional Dependencies:

    msvcrt.lib
    libcmt.lib
    

    And I also had to add preprocessor definition OSG_LIBRARY_STATIC in C/C++ -> Preprocessor -> Preprocessor Definitions. The last thing was adding these few lines of code in my main.cpp to link OSG plugins:

    #ifdef OSG_LIBRARY_STATIC
        USE_OSGPLUGIN(freetype)
        USE_OSGPLUGIN(png)
        USE_OSGPLUGIN(3ds)
    #endif
    

    Now my application is successfully compiling and working :)

    P.s. On first build I had errors with PNG and FreeType plugins which were caused by lack of .lib-s for libpng and freetype. There errors were solved in my case by adding libpng16_static.lib and freetype250.lib to Linker -> Input -> Additional Dependencies and copying them into my libraries directory (obviously other plugins would depend on other libraries).