Search code examples
cmakevisual-studio-2017static-librariesstatic-linkingbullet

Can't link to bullet physics library - LNK2019,LNK2001


I have built bullet3-3.08 on Windows using cmake (commands have been executed from the build folder created in the bullet3-3.08 directory):

cmake -G "Visual Studio 15 2017" -A x64 -D "CMAKE_INSTALL_PREFIX:PATH=C:/MyLibs/bullet3-3.08" -D "USE_MSVC_RUNTIME_LIBRARY_DLL=ON" -D "INSTALL_LIBS=ON" ..
cmake --build . --config Release --parallel 8 --target install

I have an application which uses MD/MDd runtime library so I have built Bullet with the following option: USE_MSVC_RUNTIME_LIBRARY_DLL=ON. Bullet is built as a static library by default. I use Visual Studio 2017 and build my application using cmake. When I link to Bullet I get many linker errors (LNK2019, LNK2001), for example:

error LNK2019: unresolved external symbol "public: __cdecl btCollisionDispatcher::btCollisionDispatcher(class btCollisionConfiguration *)" (??0btCollisionDispatcher@@QEAA@PEAVbtCollisionConfiguration@@@Z) referenced in function main
error LNK2001: unresolved external symbol "public: virtual void __cdecl btCollisionShape::getBoundingSphere(class btVector3 &,float &)const " (?getBoundingSphere@btCollisionShape@@UEBAXAEAVbtVector3@@AEAM@Z)

I use direct library paths in CMakeLists.txt:

target_link_libraries(${executableName} "C:/MyLibs/bullet3-3.08/lib/Bullet3Collision.lib"
                                        "C:/MyLibs/bullet3-3.08/lib/Bullet3Dynamics.lib"
                                        "C:/MyLibs/bullet3-3.08/lib/LinearMath.lib")

Here is CMakeSettings.json:

{
  "configurations": [
    {
      "name": "x64-Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [
        "msvc_x64_x64"
      ],
      "buildRoot": "${projectDir}\\build\\${name}",
      "installRoot": "${projectDir}\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": ""
    },
    {
      "name": "x64-Release",
      "generator": "Ninja",
      "configurationType": "Release",
      "inheritEnvironments": [
        "msvc_x64_x64"
      ],
      "buildRoot": "${projectDir}\\build\\${name}",
      "installRoot": "${projectDir}\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": ""
    }
  ]
}

What is wrong ?


Solution

  • As Bullet provides a BulletConfig.cmake file it is quite simple to link against Bullet.

    First you need to install Bullet (if not done so) and add -DCMAKE_PREFIX_PATH=C:/MyLibs/bullet3-3.08 (or the appropriate installation directory) to your cmake command line.

    Then in your CMakeLists.txt file you need to add

    find_package(Bullet REQUIRED)
    # your add_executable call follows here
    add_executable(${executableName} .......)
    target_compile_definitions(${executableName} PRIVATE ${BULLET_DEFINITIONS})
    target_include_directories(${executableName} PRIVATE ${BULLET_INCLUDE_DIRS})
    target_link_libraries(${executableName} PRIVATE ${BULLET_LIBRARIES})
    

    This should be the steps necessary to link to Bullet.