Hind Forsum
Hind Forsum

Reputation: 10507

How to use cmake to check if some libraries exists before build our sources?

Usually, open source packages will have cmake to check if some headers or libraries exists or not, I wish my own project has this same functionality.

So I wish to know if cmake provides some command to check if some ".so"/".a"/".h" files exists in the current linux system or not, if not cmake will give me some hint to install them?

How does cmake support this?

Upvotes: 0

Views: 4629

Answers (3)

J.Adler
J.Adler

Reputation: 1463

You need to provide -config.cmake files to tell other projects where your libraries and headers are. Here, you will found what are you looking for.

Upvotes: 0

Damian
Damian

Reputation: 4631

Usually one would use the find_package(ABC REQUIRED) and then refer to it in your project. This will ensure that the dependent library is installed and cmake will fail if it is not.
You can find lot's of example on how this works in your cmake installation, for example C:\Program Files\CMake\share\cmake-3.13\Modules\FindZLIB.cmake will search for the zlib library by looking in the file system for normal places where this library would be installed and if it finds it will set these variables accordingly:

#   ZLIB_INCLUDE_DIRS   - where to find zlib.h, etc.
#   ZLIB_LIBRARIES      - List of libraries when using zlib.
#   ZLIB_FOUND          - True if zlib found.

To achieve this header files are found using the cmake command find_path, and libraries (static and shared) are found using find_library.

Upvotes: 3

arrowd
arrowd

Reputation: 34401

To search for arbitrary library you can use find_library() command. Same task for headers is accomplished by find_file(). You can also search for executables with find_program().

As @Damian said in his naswer, many libraries provide "config" files, like FindBoost.cmake. Such libraries can be found by calling find_package(Boost) command. This command would locate and load config file and set corresponding variables.

Upvotes: 2

Related Questions