{"id":16631052,"url":"https://github.com/zmb3/cmaketest","last_synced_at":"2026-03-12T12:31:04.364Z","repository":{"id":80787372,"uuid":"44409347","full_name":"zmb3/cmaketest","owner":"zmb3","description":"Repository for troubleshooting a CMake issue","archived":false,"fork":false,"pushed_at":"2015-10-21T12:42:45.000Z","size":132,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-18T06:28:47.676Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/zmb3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-16T20:38:40.000Z","updated_at":"2015-10-16T20:54:20.000Z","dependencies_parsed_at":"2023-03-12T12:10:07.166Z","dependency_job_id":null,"html_url":"https://github.com/zmb3/cmaketest","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/zmb3%2Fcmaketest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zmb3%2Fcmaketest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zmb3%2Fcmaketest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zmb3%2Fcmaketest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zmb3","download_url":"https://codeload.github.com/zmb3/cmaketest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243095796,"owners_count":20235546,"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":[],"created_at":"2024-10-12T04:50:44.031Z","updated_at":"2025-12-16T10:31:18.345Z","avatar_url":"https://github.com/zmb3.png","language":"CMake","funding_links":[],"categories":[],"sub_categories":[],"readme":"Relocatable CMake Libraries\n===========================\n\nHow do you properly create a library that depends on another library?\n\nIn this case, the library is an example _libfoo_ library, and it depends on libpcap,\nwhich is not a CMake project.\n\nThis project started out as an example to illustrate the problem.  After the wonderful \nfolks on the CMake mailing list helped me out, I figured I'd update the example to \nshow the solution.\n\nIf you try toproject compiles and links to libpcap correctly, but is not relocatable.\n\n## To reproduce\n\n1. Clone this repository and `cd` into it.\n2. Checkout the original commit: `git checkout 720d9dd`\n3. `mkdir build \u0026\u0026 cd build`\n4. `cmake ..`\n\nCMake completes without error, but the generated libfooTargets.cmake file contains\nan absolute path to libpcap.so, which is not relocatable.\n\n```\n\u003e grep LINK_INTERFACE libfooTargets.cmake\nIMPORTED_LINK_INTERFACE_LIBRARIES_NOCONFIG \"/usr/lib/x86_64-linux-gnu/libpcap.so\"\n```\n\n### Imported Targets\n\nThe solution is to use imported targets and the find dependency macro.\n\nStart by reading [Creating Relocatable Packages](http://cmake.org/cmake/help/v3.3/manual/cmake-packages.7.html#creating-relocatable-packages)\nfrom the CMake documentation.\n\nHere's the relevant portion:\n\n\u003e It is not advisable to populate any properties which may contain paths, such as\n\u003e `INTERFACE_INCLUDE_DIRECTORES` and `INTERFACE_LINK_LIBRARIES` with paths relevant to\n\u003e dependencies.\n\n\u003e The referenced variables may contain the absolute paths to libraries and include\n\u003e directories *as found on the machine the package was made on*.  This would create\n\u003e a package with hard-coded paths to dependencies and not suitable for relocation.\n\nThen it says \"ideally such dependencies should be used through their own IMPORTED targets.\"\n\nFirst, update the FindPCAP.cmake module so that it creates an imported target.\n\n```cmake\nif (PCAP_FOUND AND NOT TARGET PCAP)\n   add_library(PCAP IMPORTED SHARED)\n   set_target_properties(PCAP PROPERTIES IMPORTED_LOCATION ${PCAP_LIBRARY})\n   set(PCAP_LIBRARIES PCAP)\nendif()\n```\n\nNow update the library's package config file:\n\n```cmake\ninclude(CMakeFindDependencyMacro)\nfind_dependency(PCAP)\n```\n\nLastly, make sure the CMakeLists.txt file is using the variable you set for your imported target:\n\n```cmake\ntarget_link_libraries(libfoo PUBLIC ${PCAP_LIBRARIES})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzmb3%2Fcmaketest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzmb3%2Fcmaketest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzmb3%2Fcmaketest/lists"}