# try\_compile to use include directory from imported target

**URL:** <https://discourse.cmake.org/t/try-compile-to-use-include-directory-from-imported-target/5127>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [February 28, 2022, 1:02pm UTC](https://discourse.cmake.org/t/try-compile-to-use-include-directory-from-imported-target/5127 "2022-02-28T13:02:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bryn](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@bryn](https://discourse.cmake.org/u/bryn)\
**Post date:** [February 28, 2022, 1:02pm UTC](https://discourse.cmake.org/t/try-compile-to-use-include-directory-from-imported-target/5127/1 "2022-02-28T13:02:52Z")

</div>

I am trying to figure out during configure time if the BLAS I find (ubuntu-20.04) is from Intel MKL, or some other vendor.

```auto
  find_package(BLAS REQUIRED)

  try_compile(
    test_use_mkl ${CMAKE_BINARY_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/test_include_mkl.cpp
    LINK_LIBRARIES BLAS::BLAS OUTPUT_VARIABLE test_out)
  message(STATUS "Using Intel MKL: ${test_use_mkl } ${test_out}")

```

During configuration I get:

> – Found BLAS: /usr/lib/x86\_64-linux-gnu/libmkl\_intel\_lp64.so;/usr/lib/x86\_64-linux-gnu/libmkl\_intel\_thread.so;/usr/lib/x86\_64-linux-gnu/libmkl\_core.so;/usr/lib/x86\_64-linux-gnu/libiomp5.so;-lpthread;-lm;-ldl  
> – Using Intel MKL: FALSE Change Dir: /home/lloyd/src/VegaFEM/CIBuild/CMakeFiles/CMakeTmp  
> Run Build Command(s):/usr/bin/make -f Makefile cmTC\_fdefb/fast && /usr/bin/make -f CMakeFiles/cmTC\_fdefb.dir/build.make CMakeFiles/cmTC\_fdefb.dir/build  
> make[1]: Entering directory ‘/home/lloyd/src/VegaFEM/CIBuild/CMakeFiles/CMakeTmp’  
> Building CXX object CMakeFiles/cmTC\_fdefb.dir/test\_include\_mkl.cpp.o  
> /usr/bin/c++ -std=c++14 -o CMakeFiles/cmTC\_fdefb.dir/test\_include\_mkl.cpp.o -c /home/lloyd/src/VegaFEM/cmake/test\_include\_mkl.cpp  
> /home/lloyd/src/VegaFEM/cmake/test\_include\_mkl.cpp:1:10: fatal error: mkl\_blas.h: No such file or directory

How can I use `try_compile` with an imported target? Can I get the include directories from the target `BLAS::BLAS`?

---

<div class="post-metadata">

**Author:** ![bryn](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/43a26b/32.png) [@bryn](https://discourse.cmake.org/u/bryn)\
**Post date:** [February 28, 2022, 2:10pm UTC](https://discourse.cmake.org/t/try-compile-to-use-include-directory-from-imported-target/5127/2 "2022-02-28T14:10:48Z")

</div>

I think the issue is that FindBlas does not actually set the include dirs, so the imported target `BLAS::BLAS` is not enough. I added the lines:

```auto
  foreach(lib ${BLAS_LIBRARIES})
    if (${lib} MATCHES "mkl")
      set(USE_INTEL_MKL ON)
    endif()
  endforeach()
  if(USE_INTEL_MKL )
    find_path(BLAS_INCLUDE_DIRS mkl_cblas.h PATHS /usr/include /usr/local/include /usr/include/mkl REQUIRED)
  endif()

```

to find the header locations, and skip the `try_compile`.

---

<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:** [February 28, 2022, 3:27pm UTC](https://discourse.cmake.org/t/try-compile-to-use-include-directory-from-imported-target/5127/3 "2022-02-28T15:27:06Z")

</div>

> [@bryn](#):
>
> How can I use `try_compile` with an imported target?

`try_compile` does not support imported targets AFAIK.

> [@bryn](#):
>
> Can I get the include directories from the target `BLAS::BLAS`?

There is the `INTERFACE_INCLUDE_DIRECTORIES` property, but this will not get any transitive requirements from other dependencies.

---

<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:08pm UTC](https://discourse.cmake.org/t/try-compile-to-use-include-directory-from-imported-target/5127/4 "2023-10-13T15:08:20Z")

</div>

@bryn Maybe not relevant anymore, but `try_compile` does work with imported targets. For now though, make sure it is not an aliased target because of [this bug](https://gitlab.kitware.com/cmake/cmake/-/issues/25337)
