# How to control \_IMPORT\_PREFIX in exported Targets.cmake list file generated by cmake

**URL:** https://discourse.cmake.org/t/how-to-control-import-prefix-in-exported-targets-cmake-list-file-generated-by-cmake/2291
**Category:** Usage
**Tags:** os:linux
**Created:** [December 4, 2020, 4:21am UTC](https://discourse.cmake.org/t/how-to-control-import-prefix-in-exported-targets-cmake-list-file-generated-by-cmake/2291 "2020-12-04T04:21:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [December 4, 2020, 4:21am UTC](https://discourse.cmake.org/t/how-to-control-import-prefix-in-exported-targets-cmake-list-file-generated-by-cmake/2291/1 "2020-12-04T04:21:12Z")

</div>

The Target.cmake target list file generated by install(EXPORT…) has line that set \_IMPORT\_PREFIX to absolute path of the project that generates this file. Therefore the consuming project is not able to find files in imported package directory.  
I’d like to fix this such that \_IMPORT\_PREFIX is set to “${CMAKE\_CURRENT\_LIST\_FILE}”. What should be changed for this?  
DETAILS  
Based on cmake source code GenerateImportPrefix function it picks case // The export file is being installed to an absolute path despite my setting in install(EXPORT DESTINATION to relative path…  
My project generates Target.cmake which sets \_IMPORT\_PREFIX to target.cmake location in under ${CMAKE\_INSTALL\_PREFIX} and then \_IMPORT\_PREFIX is used to set\_target\_properties of each target

```auto
# The installation prefix configured by this project.
set(_IMPORT_PREFIX "/home/myspace/install/armp")

```

in my CMakeLists.txt, the DESTINATION dir the same relative path value is set in

```auto
install(EXPORT ... DESTINATION "${CONFIG_INSTALL_DIR}" 
  and in
 configure_package_config_file( ... INSTALL_DESTINATION "${CONFIG_INSTALL_DIR}" 

```

I see that in a [sample project](https://github.com/jcfr/stackoverflow-56135785-answer/blob/master/FooBarLib/CMakeLists.txt) \_IMPORT\_PREFIX is being set properly in generated:  
FooBarLibinstall/opt/lib/cmake/FooBarLib/FooBarLibTargets.cmake file

```auto

# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)

```

but I am not clear on what part of code is responsible for that because the correct generation occurs despite that the stackoverflow-56135785-answer CMakeLists.txt examplesets not matching DESTINATION and INSTALL\_DESTINATION values, and INSTALL\_DESTINATION is set to an absolute path

```auto
/home/myspace/stackoverflow-56135785-answer/FooBarLib-build 

```

as per

```auto
configure_package_config_file( INSTALL_DESTINATION "${PROJECT_BINARY_DIR}"  

```

which differs from the relative path lib/cmake/FooBarLib set by

```auto
install( EXPORT DESTINATION ${FOOBARLIB_INSTALL_CONFIG_DIR}  

```

The same question was asked before in [Create relocatable package with proper autogenerated config cmake](https://stackoverflow.com/questions/45730997/create-relocatable-package-with-proper-autogenerated-config-cmake) and [correctly set the location of imported cmake targets for an installed package](https://stackoverflow.com/questions/56135785/correctly-set-the-location-of-imported-cmake-targets-for-an-installed-package) but I could not pinpoint what is the difference in my code.  
I see several cache variables are set to the same absolute path as the value written in the to set command in the “The installation prefix configured by this project.” section of Targets.cmake file : CMAKE INSTALL\_PREFIX and CPACK\_PACKAGING\_INSTALL\_PREFIX, (and other CPACK\_\* vars) but I think both should be in absolute path  
There is another problem with the additional absolute path (e.g /home/myspace/mylib/include) added after the proper path in the INTERFACE\_INCLUDE\_DIRECTORIES and Id like to drop that:

```auto
		# Create imported target dalint::cnhlogger
add_library(myspace::mylib SHARED IMPORTED)

set_target_properties(myspace::mylib PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/mylib/include;/home/myspace/mylib/include"
)

```

---

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [December 4, 2020, 9:04pm UTC](https://discourse.cmake.org/t/how-to-control-import-prefix-in-exported-targets-cmake-list-file-generated-by-cmake/2291/2 "2020-12-04T21:04:50Z")

</div>

I applied [solution from this Answer](https://stackoverflow.com/questions/45751218/can-parameter-destination-for-install-be-empty) adding

```auto
set(CMAKE_INSTALL_PREFIX "\${CMAKE_CURRENT_LIST_FILE}/..")

```

right before install(EXPORT provided the result I wanted, but it s a kludge as so I still like to ge proper solution. Also the [sample projec](https://github.com/jcfr/stackoverflow-56135785-answer/blob/master/FooBarLib/CMakeLists.txt)t given above works without this kludge.

---

<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: [January 4, 2021, 6:47pm UTC](https://discourse.cmake.org/t/how-to-control-import-prefix-in-exported-targets-cmake-list-file-generated-by-cmake/2291/3 "2021-01-04T18:47:48Z")

</div>

`_IMPORT_PREFIX` is a variable used by CMake in the targets file for its own purposes. It is intended to be the installation prefix (and is computed the way it is because the literal `CMAKE_INSTALL_PREFIX` can change once relocated).

Do you have an example of how you’re installing files and setting up installation interfaces that what it does doesn’t work for you?
