FetchContent and FIND_PACKAGE_ARGS not linking correctly

I’m currently working on a project where I aim to use find_package() to check if a library is available in the system. If the library is not available, I intend to fetch it and compile it using FetchContent . I’ve managed to achieve this using FetchContent alone, as shown below (for glm):

FetchContent_Declare(
  opencv
  GIT_REPOSITORY https://github.com/opencv/opencv.git
  GIT_TAG        4.9.0
  GIT_SHALLOW    TRUE
  GIT_PROGRESS   TRUE
  FIND_PACKAGE_ARGS NAMES OpenCV
)
FetchContent_MakeAvailable(opencv)

FetchContent_Declare(
  glm
  GIT_REPOSITORY https://github.com/g-truc/glm.git
  GIT_TAG        1.0.1
)
FetchContent_MakeAvailable(glm)

[...]

target_link_libraries(rvs
    PRIVATE
        rvslib
        TracyClient
        opencv_core opencv_imgproc opencv_imgcodecs opencv_photo opencv_videoio opencv_highgui
)

target_include_directories(rvs
    PUBLIC
        ${RVS_PROJECT_SOURCE_DIR}/include/
    PRIVATE
        # OpenCV
        ${OPENCV_CONFIG_FILE_INCLUDE_DIR}
        ${OPENCV_MODULE_opencv_core_LOCATION}/include
        ${OPENCV_MODULE_opencv_highgui_LOCATION}/include
        ${OPENCV_MODULE_opencv_imgproc_LOCATION}/include
        ${OPENCV_MODULE_opencv_imgcodecs_LOCATION}/include
        ${OPENCV_MODULE_opencv_videoio_LOCATION}/include
        ${OPENCV_MODULE_opencv_photo_LOCATION}/include # inpaint function
)

However, I encountered an issue when attempting to use FIND_PACKAGE_ARGS. While it works for some libraries (such as OpenCV), it doesn’t work for others, and I’m struggling to understand why.

Initially, it couldn’t find the headers for glm. I resolved this by adding glm::glm-header-only as a target in target_link_libraries, which allowed it to find the headers. However, it then complained about not finding the “glm.lib” file for linking. Adding glm::glm as a target somewhat resolved this, but it only added “glm.lib” to the linked libraries without the full path, preventing the project to compile.

[...]
FetchContent_Declare(
  glm
  GIT_REPOSITORY https://github.com/g-truc/glm.git
  GIT_TAG        1.0.1
  FIND_PACKAGE_ARGS NAMES glm
)
FetchContent_MakeAvailable(glm)

target_link_libraries(rvs
  PRIVATE
    rvslib
    TracyClient
    opencv_core opencv_imgproc opencv_imgcodecs opencv_photo opencv_videoio opencv_highgui
    glm::glm-header-only glm::glm
)

If I manually change the path to a full path to glm.lib in visual studio, it compiles.

I’m encountering similar issues with other libraries like Tracy and glfw3, but not with OpenCV for some reason.

If anyone has encountered similar challenges or has insights into why this might be happening, I would greatly appreciate any assistance or advice. Thank you.

Do glm and friends have #pragma comment(lib, "glm.lib")-like lines in them? This is “autolinking” where the header tells the linker to link to a library by name (but not the path). I’d look to see if there’s a way to disable it for each library. If not, then the “header only”-ness of these projects is questionable on Windows.

I did’t find any hardcoded glm.lib in their codebase at least.