From 06703e17d4a68381a69577aac1289d7efff3d9ea Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 4 Apr 2022 14:13:40 +0200 Subject: [PATCH] CMake: Add paths from target_link_directories to ((DY)LD_LIBRARY_)PATH So far we only looked at a target and added all paths of actually linked libraries to the ((DY)LD_LIBRARY_)PATH, if the "Add build library search path" option is on (the default). That often is fine, but - if the library to link to is only given as a library name, not a path and not a CMake target, then CMake file-api doesn't give us a path to the library either - on Windows, where the .lib is needed for compiletime linking, but the .dll is needed for runtime linking this only helps if the .dll was in the same directory as the .lib We already have a hack on Windows, if the directory ends in /lib, that we also add /lib/../bin, but that again only helps for that specific layout. Instead actually add the "build library search path", by adding the directories from target_link_directories, even if no libraries are linked from there. This fixes the "linked only by name" issue, and allows users to add a build library search path to the .dll too, and have that used by Qt Creator for running the application. Fixes: QTCREATORBUG-27201 Change-Id: I7b9210b791b4dae3a6d1747ff36e4b82235db2f9 Reviewed-by: Cristian Adam --- .../cmakeprojectmanager/fileapidataextractor.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/plugins/cmakeprojectmanager/fileapidataextractor.cpp b/src/plugins/cmakeprojectmanager/fileapidataextractor.cpp index d5836605f48..8a21729fafb 100644 --- a/src/plugins/cmakeprojectmanager/fileapidataextractor.cpp +++ b/src/plugins/cmakeprojectmanager/fileapidataextractor.cpp @@ -268,7 +268,18 @@ QList generateBuildTargets(const PreprocessedData &input, // CMake sometimes mixes several shell-escaped pieces into one fragment. Disentangle that again: const QStringList parts = ProcessArgs::splitArgs(f.fragment); - for (const QString &part : parts) { + for (QString part : parts) { + // Library search paths that are added with target_link_directories are added as + // -LIBPATH:... (Windows/MSVC), or + // -L... (Unix/GCC) + // with role "libraryPath" + if (f.role == "libraryPath") { + if (part.startsWith("-LIBPATH:")) + part = part.mid(9); + else if (part.startsWith("-L")) + part = part.mid(2); + } + // Some projects abuse linking to libraries to pass random flags to the linker, so ignore // flags mixed into a fragment if (part.startsWith("-"))