0

I build my project using cmake (It is important for me to work both generators: Ninja and Visual Studio). And I need to add assimp library to my project. So, I added assimp from git to my project as git submodule and as suggested in the docs I added this lines to my CMake:

add_subdirectory(assimp)
target_link_libraries(project assimp)

I don't understand:

  1. By default assimp compiles as shared lib, so why I should use target_link_libraries(rt assimp), if as far as i know it's link my executable with static library? And anyway after this linkage my .exe requires .dll.

  2. Also, as alternative, I tried to compile assimp as static lib, as specified in the documentation I should use set(BUILD_SHARED_LIBS OFF). I added this flag to my main CMakeLists.txt. But it still compiles two files:

  • bin/Debug/assimp.dll
  • lib/Debug/assimp.lib
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(assimp)
target_link_libraries(project assimp)

I thought that I could choose one of them, but no: after linkage with static lib (target_link_libraries(rt assimp)) it's anyway requires .dll.

Note: .dll is uncomfortable for me, because I don't want to move the .dll to my .exe directory everytime. Also, variants like donwloading assimp from vcpkg or adding .dll to PATH don't work to me, because my main goal is: the user should just clone my git repo and compile the whole project with all dependecies using only one my CMakeLists.txt without additional actions.

  • ***Note: .dll is uncomfortable for me, because I don't want to move the .dll to my .exe directory everytime.*** You could script that using CMake. Although this question is 10 years old there are some modern methods in the answers depending on the CMake version you support: [https://stackoverflow.com/questions/10671916/how-to-copy-dll-files-into-the-same-folder-as-the-executable-using-cmake](https://stackoverflow.com/questions/10671916/how-to-copy-dll-files-into-the-same-folder-as-the-executable-using-cmake) – drescherjm Jul 26 '22 at 13:46
  • Yes, I saw that. But, as for me its a little weird solution. Anyway, also Interesting to solve static lib problem. –  Jul 26 '22 at 13:58
  • ***Interesting to solve static lib problem*** I am not sure that it is a solvable problem. Not all libraries can be easily compiled into static libraries. – drescherjm Jul 26 '22 at 14:01
  • But the documentation mentions that I can compile static: https://assimp-docs.readthedocs.io/en/v5.1.0/about/quickstart.html#assimp-static-lib –  Jul 26 '22 at 14:07
  • Interesting, is there a way to tell the executable where the .dll is (using cmake, without PATH) instead of move .dll to .exe directory? –  Jul 26 '22 at 14:09
  • ***is there a way to tell the executable where the .dll is (using cmake, without PATH) instead of move .dll to .exe directory?*** No. CMake does not control run time of your compiled executable. – drescherjm Jul 26 '22 at 14:11
  • This answer may work and not require any dll copying / moving depending on what the CMakeLists.txt does for assimp: https://stackoverflow.com/a/34445220/487892 – drescherjm Jul 26 '22 at 14:19
  • Yes, its works. Also, interesting why in "shared compilation" there are two libs: .dll and .lib and I have to use both. Developers split the whole code into two parts: .lib and .dll? –  Jul 26 '22 at 14:49
  • With msvc a dll which uses implicit linking has an import library with a `.lib` file that you link. This library does not contain the implementations of the functions and requires a dll at runtime. You can try this tutorial to understand how this works: [https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170](https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170) – drescherjm Jul 26 '22 at 14:52
  • To build a static version of assimp, set `BUILD_SHARED_LIBS` to `OFF`, it's a standard option of CMake projects. By default it's OFF in CMake, but assimp changes the default value to ON: https://github.com/assimp/assimp/blob/v5.2.4/CMakeLists.txt#L63-L66 – SpacePotatoes Jul 26 '22 at 17:27
  • **Not all libraries can be easily compiled into static libraries** => usually it's the opposite, specially on Windows since many libraries don't properly export symbols. – SpacePotatoes Jul 26 '22 at 17:30
  • Like I mentioned in question I tried, but it's didn't work. –  Jul 26 '22 at 17:32
  • https://stackoverflow.com/questions/3766740/overriding-a-default-option-value-in-cmake-from-a-parent-cmakelists-txt – SpacePotatoes Jul 26 '22 at 17:37
  • ***usually it's the opposite, specially on Windows since many libraries don't properly export symbols*** My point was if the library was designed to be a dll from the developers you may not always easily alter settings to have it build as a static library. – drescherjm Jul 26 '22 at 18:14

0 Answers0