create account

How to add FindPackage() script for libraries not already supported by cmake. by mtl1979

View this thread on: hive.blogpeakd.comecency.com
· @mtl1979 · (edited)
$0.03
How to add FindPackage() script for libraries not already supported by cmake.
Sometimes a developer wants to make sure compilation of a program doesn't fail even if user hasn't installed all the dependencies... With cmake it is easy to add optional packages that will only be built if the dependency hasn't been installed.

As an example, I will use ```libqrencode```, which uses header ```qrencode.h``` and shared library ```libqrencode.so```.

In your ```CMakeLists.txt```:

```
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

find_package(QRencode)

if(QRENCODE_FOUND)
  include_directories(SYSTEM ${QRENCODE_INCLUDE_DIRS})
else()
  include_directories(libqrencode)
  set(WITH_TESTS NO)
  set(WITH_TOOLS NO)
  add_subdirectory(libqrencode)
endif()
```


```CMAKE_MODULE_PATH``` tells cmake where to find custom cmake modules. ```find_package()``` loads ```FindQRencode.cmake``` from directory specified in ```CMAKE_MODULE_PATH```.
```QRENCODE_FOUND``` is set if ```FindQRencode.cmake``` finds ```libqrencode```. If it is not found, the ```else()``` block will add embedded ```libqrencode``` sources from directory ```libqrencode``` under ```CMAKE_CURRENT_SOURCE_DIR```. Any unnecessary build steps can be disabled before calling ```add_subdirectory()```. In this example we disabled building "tests" and "tools".

In ```cmake/FindQRencode.cmake```:

```
find_path(QRENCODE_ROOT_DIR
    NAMES include/qrencode.h
)

find_library(QRENCODE_LIBRARIES
    NAMES qrencode
    HINTS ${QRENCODE_ROOT_DIR}/lib
)

find_path(QRENCODE_INCLUDE_DIRS
    NAMES qrencode.h
    HINTS ${QRENCODE_ROOT_DIR}/include
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(QRencode DEFAULT_MSG
    QRENCODE_LIBRARIES
    QRENCODE_INCLUDE_DIRS
)

mark_as_advanced(
    QRENCODE_ROOT_DIR
    QRENCODE_LIBRARIES
    QRENCODE_INCLUDE_DIRS
)
```

```QRENCODE_ROOT``` is used to find both header and library. It can be specified in cmake command line if libqrencode is installed in non-standard location.

```QRENCODE_LIBRARIES``` contains full path of qrencode library (libqrencode.so.3 or libqrencode.so.4 on Linux).

```QRENCODE_INCLUDE_DIRS``` contains path for the header qrencode.h.

"QRencode" is the package name, as seen in ```find_package()``` and ```find_package_handle_standard_args()```.
"qrencode" is base name of the library... On Linux and most Unix-like systems it is usually prepended with "lib" and appended with ".so" (shared library) or ".a" (static library). Shared library name can also contain version number (.3 or .4 for ```libqrencode```).

```find_package_handle_standard_args()``` sets ```QRENCODE_FOUND``` variable if the specified variables have been set.

```mark_as_advanced()``` will hide the variable names unless user explicitly asks to show "advanced" variables.
👍  , , , , , ,
properties (23)
authormtl1979
permlinkhow-to-add-findpackage-script-for-libraries-not-already-supported-by-cmake
categorycmake
json_metadata{"app":"peakd/2020.05.5","format":"markdown","tags":["cmake","findpackage","programming"],"image":[]}
created2020-06-23 15:47:06
last_update2020-06-23 16:56:24
depth0
children1
last_payout2020-06-30 15:47:06
cashout_time1969-12-31 23:59:59
total_payout_value0.015 HBD
curator_payout_value0.014 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,728
author_reputation901,077,179,728
root_title"How to add FindPackage() script for libraries not already supported by cmake."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id98,134,585
net_rshares135,913,724,585
author_curate_reward""
vote details (7)
@gitplait-mod2 ·
Thanks for this resourceful content. With this others can learn to FindPackage() script for libraries not already supported by cmake.

Thanks again.

<sub> **Your post has been curated with @gitplait community account because this is the kind of publications we like to see in our community.** </sub>

Join our [Community on Hive](https://hive.blog/trending/hive-103590) and Chat with us on [Discord](https://discord.gg/CWCj3rw).

[[Gitplait-Team]](https://gitplait.tech/)

properties (22)
authorgitplait-mod2
permlinkre-mtl1979-qcdz78
categorycmake
json_metadata{"tags":["cmake"],"app":"peakd/2020.05.5"}
created2020-06-23 16:02:51
last_update2020-06-23 16:02:51
depth1
children0
last_payout2020-06-30 16:02:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length474
author_reputation28,898,670,427
root_title"How to add FindPackage() script for libraries not already supported by cmake."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,134,811
net_rshares0