I’m looking for a way to have both the DLLs copied to the executable’s output directory (unit tests executable). I was hoping for a way to define this as part of the interface to the imported library so consumers only need to link to the imported target.
Is there any such way?
If not, what would be the CMake way for this? Set up a POST build event that copies the DLLs per executable target that links to it? (How would you then obtain the paths for the DLL from the importer target?)
Looking at that it looks to be used along with install which is then used with a separate install comment to install it to the system.
I was looking for a build-time command/script that would copy dependencies to the build output directory so when I run a target the necessary dependency is there along with the executable.
You could try GetPrerequisites, but it’s finicky (though probably better on Windows due to the simpler linking model). If this is a necessary thing, I recommend just bumping your minimum.
Is get_runtime_deps.cmake something that exists? Or a CMake script I have to write myself? I’m still not clear on how this allows me to get the dependencies of a binary in a build command.
I have a lot of trouble with get_prerequisites(). One of the problems is that it returns prerequisite dlls without their path on windows. Your cmake code then needs to take care of finding the libraries. You could now remember all the dlls found during configure and take those. This does not work with if a dll is used by the prerequisite but not needed at link time. You can then try to remember the paths of the dlls found in the configure phase and look for a prerequisites there. This may help but is still not garanteed to work if the “lower level dll” is located elsewhere.
Yesterday I had another problem with get_prerequisites() which I posted today.
I recommend not using get_prerequisites anymore. file(GET_RUNTIME_DEPENDENCIES) is a better implementation of that logic. There are many corner cases get_prerequisites does not consider and fixing it is not really feasible (it would basically involve writing the dynamic loader logic for each platform in CMake…which is incidentally why it behaves so poorly today).
Thanks for the hint. I already saw that GET_RUNTIME_DEPENDENCIES has been added in a recent cmake version.
Do you know if it is also faster than get_prerequisites(). The latter is terribly slooooooow.
It is indeed faster, since it’s written in native C++
It is also more accurate. We painstakingly drew out the flowchart of dependency searching for Windows, Linux, and macOS and mimicked it as best as we could. GetPrerequisites was not so rigorous.
Hello Kyle,
sounds superb. Thanks so much for providing this new functionality. Does the windows version now provide absolute paths to the dependencies as on linux? This would make my day even more and might make me code over night!
Best regards, Stephan.
Revisting this again. This .cmake file to run at build time, how will that know what file to be checking? Do I need to pass the path to the binary to check dependency for as a command line parameter? If so, how do I then obtain the path where the binary for a target will be?
Your CMakeLists.txt specifies what library or executable is being built and where it is going to be located. This is the place where you can add this information to “some variable”, e.g. a list of all libraries/executables that you are building. Towards the end of the CMakeLists.txt (i.e. after all libraries/executables are defined) you can advise cmake to call installation code (using install(CODE …)) at install time. This code would then evaluate the content of the “variable” and gather all depencies using file(GET_RUNTIME_DEPENCIES …). Works for me although I do not like my “global variable”. I also have problems with file (INSTALL …) and symbolic links on windows, but this is another story.
You might be able to avoid the global variable by using file(GENERATE) to create scripts to call with install(SCRIPT). Using file(GENERATE), you can use things like generator expressions that provide the locations of build executables and libraries.