1

I've recently created a library that needs a non-text (but platform agnostic) resource to be used. I basically tell CMake, if this is an install interface, it's in the "${CMAKE_INSTALL_INCLUDEDIR}/resources" directory, otherwise it's in the "${CMAKE_CURRENT_SOURCE_DIR}/../resources" directory. Then I generate a header file with this path embedded into it (excerpt from "file_paths.h")

 constexpr std::string_view resource_file_path = std::string_view{ "....some_path/to/resources" }; 

and include that header in the code that relies on loading said resource.

I thought all was well and good until after I packaged my library up for use in Vcpkg and it claimed

 warning: There should be no absolute paths, such as the following, in an installed package: path_to_project\vcpkg\packages\my-package_x64-windows-static path_to_project\cmake-build-debug\vcpkg_installed path_to_project\vcpkg\buildtrees\my-package path_to_project\vcpkg\downloads Absolute paths were found in the following files: path_to_project\vcpkg\packages\my-package_x64-windows-static\include\my-package\file_paths.h 

What I'm trying to understand is if it's actually alright in my case. In my case, the absolute path only exists after VCPKG runs cmake. This wouldn't be an issue in a regular build, and shouldn't be an issue in VCPKG --download-only offline builds, since it will only generate the absolute path once you actually install the library, allowing for the whole vcpkg directory to move before build like normal. Making the path relative also doesn't change the fact the files are installed in a given location.

Should I be doing something else here or is generating a hardcoded path based on system parameters the correct move here?

7
  • Unless I'm misunderstanding things, this works only if the library is compiled on every machine it is used on - I can't take the binary compiled on one machine and use it on a different machine. That certainly breaks the Principle of Least Surprise, but it may be what your expected workflow is. Commented Nov 2, 2023 at 23:02
  • @PhilipKendall Is there an alternative? I can't use the executables location, as that has no relationship to where the libraries are installed. Can't use #embed, though that wouldn't work in this scenario anyway (the lib I'm using requires a file directory, no way to load resource raw). Commented Nov 3, 2023 at 5:11
  • @Krupip: when I understand this correctly, you are developing a library which is published and deployed in source code, right? And the absolute paths are only generated after deployment, by CMake and/or your own code generator, on the machine where the library is used for building software which depends on it? Please confirm. What I don't understand, however, why - in this case - you need to deploy a header file which includes an (probably incorrect for the user) absolute path at the time of deployment, before CMake generates the correct path. Commented Nov 3, 2023 at 8:25
  • @DocBrown In this case, yes, I'm deploying source code for a library used by other software, and the paths are only generated after CMake runs (in install or build interface), I'm packaging it for VCPKG. I'm confused by your second statement, what do you mean before cmake generates a correct path? It should be the correct path unless the user moves the install include directory for their project. The path is a variable expansion using generator expressions. The file has a hard-coded path after generation, CMake doesn't at all. And regardless, I still see no alternative. Commented Nov 3, 2023 at 17:16
  • @Krupip: forbid me being a noob of your use case, but I still don't understand the order in which things happen. This is my current understanding: there is a header file on your machine, which contains a generated absolute path which fits to your machine at the time when the package is created (otherwise Vcpkg would not complain). Then the package is deployed to the user's machine, which probably requires different absolute paths. Then there is a process which changes the absolute paths to the user's machine. Is that how it happens? Commented Nov 3, 2023 at 18:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.