# FetchContent and FIND\_PACKAGE\_ARGS not linking correctly

**URL:** <https://discourse.cmake.org/t/fetchcontent-and-find-package-args-not-linking-correctly/10653>\
**Category:** Code\
**Created:** [April 14, 2024, 10:35am UTC](https://discourse.cmake.org/t/fetchcontent-and-find-package-args-not-linking-correctly/10653 "2024-04-14T10:35:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbonattoj](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dbonattoj/32/4492_2.png) [@dbonattoj](https://discourse.cmake.org/u/dbonattoj)\
**Post date:** [April 14, 2024, 10:35am UTC](https://discourse.cmake.org/t/fetchcontent-and-find-package-args-not-linking-correctly/10653/1 "2024-04-14T10:35:38Z")

</div>

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](https://github.com/g-truc/glm)):

```cmake
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.

```cmake
[...]
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](https://github.com/wolfpld/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.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [April 17, 2024, 7:32pm UTC](https://discourse.cmake.org/t/fetchcontent-and-find-package-args-not-linking-correctly/10653/2 "2024-04-17T19:32:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dbonattoj](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dbonattoj/32/4492_2.png) [@dbonattoj](https://discourse.cmake.org/u/dbonattoj)\
**Post date:** [April 20, 2024, 9:47am UTC](https://discourse.cmake.org/t/fetchcontent-and-find-package-args-not-linking-correctly/10653/3 "2024-04-20T09:47:16Z")

</div>

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