# Running \`try\_compile\` within \`Find\<Package\>.cmake\`

**URL:** <https://discourse.cmake.org/t/running-try-compile-within-find-package-cmake/9199>\
**Category:** Code\
**Created:** [October 13, 2023, 2:21pm UTC](https://discourse.cmake.org/t/running-try-compile-within-find-package-cmake/9199 "2023-10-13T14:21:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lecris](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lecris/32/3193_2.png) [@Lecris](https://discourse.cmake.org/u/Lecris)\
**Post date:** [October 13, 2023, 2:21pm UTC](https://discourse.cmake.org/t/running-try-compile-within-find-package-cmake/9199/1 "2023-10-13T14:21:25Z")

</div>

For example:

```cmake
add_library(Package::Target ALIAS PkgConfig::Package)
...
try_compile(test_package_api
  SOURCES ${CMAKE_CURRENT_LIST_DIR}/test_package_api.f90
  LINK_LIBRARIES Package::Target
)

```

As far as I’ve tried this one fails because the target is not made available to the inner cmake project. Probably I could get the properties and feed them `INCLUDE_DIRECTORIES` and `LINK_DIRECTORIES`, but is there no better way to do this?

Is it a bug that either:

- `ALIAS` targets are not resolved?
- There is a clash when running within `Find<Package>.cmake`?
- `pkg-config` target is not compatible?

Looking at the code in `cmCoreTryCompile.cxx` and the generated error it is quite weird that I am getting this error:

```auto
CMake Error at /.../TryCompile-DGCuOK/CMakeLists.txt:25 (target_link_libraries):
  Target "cmTC_aa9ca" links to:
    Package::Target
  but the target was not found. Possible reasons include:
    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.
CMake Error at cmake/FindPackage.cmake:33 (try_compile):
  Failed to generate test project build system.
Call Stack (most recent call first):
  CMakeLists.txt:237 (find_package)

```

---

<div class="post-metadata">

**Author:** ![Lecris](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lecris/32/3193_2.png) [@Lecris](https://discourse.cmake.org/u/Lecris)\
**Post date:** [October 13, 2023, 3:11pm UTC](https://discourse.cmake.org/t/running-try-compile-within-find-package-cmake/9199/2 "2023-10-13T15:11:14Z")

</div>

It is a bug with `ALIAS` targets: [https://gitlab.kitware.com/cmake/cmake/-/issues/25337](https://gitlab.kitware.com/cmake/cmake/-/issues/25337)
