{"id":16836381,"url":"https://github.com/jcfr/stackoverflow-56135785-answer","last_synced_at":"2026-03-18T22:32:05.406Z","repository":{"id":137721950,"uuid":"188578159","full_name":"jcfr/stackoverflow-56135785-answer","owner":"jcfr","description":"Answer to stackoverflow question 56135785","archived":false,"fork":false,"pushed_at":"2019-05-25T16:44:41.000Z","size":22,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-24T10:23:58.611Z","etag":null,"topics":["stackoverflow-answer"],"latest_commit_sha":null,"homepage":"https://stackoverflow.com/questions/56135785/correctly-set-the-location-of-imported-cmake-targets-for-an-installed-package","language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jcfr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE_CC-BY-SA-4.0.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-25T14:48:04.000Z","updated_at":"2022-04-21T07:24:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"d6146b84-1507-441e-9c3d-077534e24314","html_url":"https://github.com/jcfr/stackoverflow-56135785-answer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcfr%2Fstackoverflow-56135785-answer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcfr%2Fstackoverflow-56135785-answer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcfr%2Fstackoverflow-56135785-answer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcfr%2Fstackoverflow-56135785-answer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcfr","download_url":"https://codeload.github.com/jcfr/stackoverflow-56135785-answer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244147343,"owners_count":20405942,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["stackoverflow-answer"],"created_at":"2024-10-13T12:13:17.589Z","updated_at":"2026-02-15T01:03:58.659Z","avatar_url":"https://github.com/jcfr.png","language":"CMake","funding_links":[],"categories":[],"sub_categories":[],"readme":"This project was created to help answer Stackoverflow question 56135785.\nSee https://stackoverflow.com/questions/56135785/correctly-set-the-location-of-imported-cmake-targets-for-an-installed-package\n\n\nIt improves the sample code originally submitted by user [bruce-adams](https://stackoverflow.com/users/1569204/bruce-adams)\nand provides some recommendations to better organize the associated CMake project.\n\n### ChangeLog summarizing edits\n* 2019-05-25\n  * Create GitHub project to streamline reuse and adaptation. See https://github.com/jcfr/stackoverflow-56135785-answer\n  * Rename project and source directory from `foobar` to `FooBarLib`, update [Suggestions](#suggestions) section accordingly\n  * Improve `build.sh`\n  * Updated suggestions (`CPACK_PACKAGING_INSTALL_PREFIX` should indeed be absolute to properly impact RPM layout)\n  * RPM:\n     * Be aware that setting `CPACK_PACKAGING_INSTALL_PREFIX` also impact the layout of the `TGZ` archive, this propably\n       not what you want.\n     * Update `build.sh` to display content of RPM package\n     * `CPACK_PACKAGING_INSTALL_PREFIX` is set to `/opt`\n\n### Remarks\n\n* Two config files should be generated:\n  * one for the build tree: this allow user of your project to directly build against your project and import targets\n  * one for the install tree (which also end up being packaged)\n* Do not force the value of `CMAKE_INSTALL_PREFIX`\n* \u003cs\u003e`CPACK_PACKAGING_INSTALL_PREFIX` should NOT be set to an absolute directory\u003c/s\u003e\n* \u003cs\u003eFor sake of consistency, use `foobarTargets` instead of `foobarLibTargets`\u003c/s\u003e\n* `\u003cprojecname_uc\u003e` placeholder used below correspond to the name of the project upper-cased (`ABC` instead of `abc`)\n* To allow configuring your project when vendorized along other one, prefer variable with `\u003cprojecname_uc\u003e_`. This means `\u003cprojecname_uc\u003e_INSTALL_LIBRARY_DIR` is better than `LIBRARY_INSTALL_DIR`.\n* To allow user of the project to configure `*_INSTALL_DIR` variables, wrap them around `if(DEFINED ...)`\n* Consistently use variables (e.g `LIBRARY_INSTALL_DIR` should always be used instead of `lib`)\n* Prefer naming variable `\u003cprojecname_uc\u003e_INSTALL_*_DIR` instead of `\u003cprojecname_uc\u003e_*_INSTALL_DIR`, it make it easier to know the purpose of the variable when reading the code.\n* Since version is already associated with the project, there is no need to set `VERSION` variable. Instead, you can use `PROJECT_VERSION` or `FOOBAR_VERSION`\n* If starting a new project, prefer the most recent CMake version. CMake 3.13 instead of CMake 3.7\n* Introduced variable `\u003cprojecname_uc\u003e_INSTALL_CONFIG_DIR`\n* `\u003cproject_name\u003eTargets.cmake` should not be installed using `install(FILES ...)`,  it is already associated with an install rule\n* conditionally set `CMAKE_INSTALL_RPATH`, it is valid only on Linux\n* `\u003cproject_name\u003eConfig.cmake.in`:\n  * there is no need to set `FOOBAR_LIBRARY`, this information is already associated with the exported `foobar` target\n  * FOOBAR_LIBRARY_DIR is also not needed, this information is already associated with the exported `foobar` target\n  * instead of setting FOOBAR_INCLUDE_DIR, the command `target_include_directories` should be used\n  * remove setting of `FOOBAR_VERSION`, the generate version file already takes care of setting the version.\n* always specify ARCHIVE, LIBRARY and RUNTIME when declaring install rules for target. It avoid issue when switching library type. One less thing to think about.\n* always specify component with your install rule. It allows user of your project to selectively install part of it only development component or only runtime one, ...\n* initializing `CMAKE_BUILD_TYPE` is also important,  it ensures the generated Targets file are associated with a configuration (instead of having the suffix `-noconfig.cmake`)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcfr%2Fstackoverflow-56135785-answer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcfr%2Fstackoverflow-56135785-answer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcfr%2Fstackoverflow-56135785-answer/lists"}