Changes between Version 4 and Version 5 of CMakeAddingCompiledLibrary


Ignore:
Timestamp:
Jun 12, 2007, 4:25:55 PM (15 years ago)
Author:
troy d. straszheim
Comment:

Cleanup usage of FindPNG and include use of compile flags returned by a supplied CMake module

Legend:

Unmodified
Added
Removed
Modified
  • CMakeAddingCompiledLibrary

    v4 v5  
    3939When building a shared library, the `SHARED_COMPILE_FLAGS` options will be combined with the `COMPILE_FLAGS` options. When building a static library, the `SHARED_COMPILE_FLAGS` options will be ignored. There are other options that can be specified per-feature, such as `LINK_FLAGS` and `LINK_LIBS`; refer to the [wiki:CMakeAddLibrary boost_add_library macro reference] for more information.
    4040
    41 == Linking to Other Libraries ==
     41== Linking to Other Boost Libraries ==
    4242Some Boost libraries depends on other Boost libraries. For example, perhaps our library uses the Boost.Filesystem library under the hood. We can use the `DEPENDS` feature of the [wiki:CMakeAddLibrary boost_add_library macro] to state which libraries our library depends on. In this example, we'll link against `boost_filesystem`:
    4343
     
    5656Sometimes, Boost libraries need to link against other libraries supplied by the system. The primary challenge in linking against these libraries is ``finding`` those libraries, and their associated headers, on the system. If the library is found, we usually want to pass some extra compilation flags to our library and add in additional sources. Otherwise, we just skip these extra sources.
    5757
    58 Often, CMake already contains a module that will search for a common system library or tool; search the [http://www.cmake.org/HTML/Documentation.html CMake Documentation] for existing modules that do what you need. For example, say we want to link against the system's `PNG` library for portable network graphics. We can use the supplied `FindPNG` module by adding the following early in our `CMakeLists.txt` file:
     58CMake already contains modules that search for many common system libraries and tools; search the [http://www.cmake.org/HTML/Documentation.html CMake Documentation] for existing modules that do what you need. For example, say we want to link against the system's `PNG` (portable network graphics) library. We can use the supplied `FindPNG` module by adding the following early in our `CMakeLists.txt` file:
    5959{{{
    6060include(FindPNG)
    6161}}}
    6262
    63 Documentation for CMake modules is typically found in the module file itself. Look into the `Modules` subdirectory of your CMake installation, either in `Program Files\CMake` (on Windows) or `/usr/share/cmake-version` (on Unix variants) to find the module of the same name. The module will typically provide a variable that indicates whether the library was found. For the `FindPNG` module, this variable is called `PNG_FOUND`. We can use this variable to optionally add sources to a variable `EXTRA_SOURCES`:
     63Documentation for CMake modules is typically found in the module file itself. Look into the `Modules` subdirectory of your CMake installation, either in `Program Files\CMake` (on Windows) or `/usr/share/cmake-version` (on Unix variants) to find the module of the same name. The module will typically set a variable that indicates whether the library was found. For the `FindPNG` module, this variable is called `PNG_FOUND`. We can use this variable to optionally add sources to a variable `EXTRA_SOURCES`:
    6464
    6565{{{
     
    7171}}}
    7272
    73 CMake modules also typically define macros specifying the include directories needed for the library and linking information for the library binary. For the `FindPNG` module, these variables are called `PNG_PNG_INCLUDE_DIR` and `PNG_LIBRARY`, respectively. The include directory should be added via the CMake `include_directories` macro, e.g.,
     73CMake modules also typically define macros specifying the include directories needed for the library, any compile-time definitions required to use the library, and linking information for the library binary. For the `FindPNG` module, these variables are called `PNG_INCLUDE_DIR`, `PNG_DEFINITIONS` and `PNG_LIBRARY`, respectively.
     74
     75The include directory should be added via the CMake `include_directories` macro, e.g.,
    7476
    7577{{{
    76 include_directories(${PNG_PNG_INCLUDE_DIR})
     78include_directories(${PNG_INCLUDE_DIR})
    7779}}}
    7880
    79 while the `PNG_LIBRARY` value should be added to the `LINK_LIBS` option to the [wiki:CMakeAddLibrary boost_add_library macro]. Using these features together, we can search for the PNG library on the system and optionally include PNG support into our library:
     81The `PNG_DEFINITIONS` value should be added to the `COMPILE_FLAGS` and
     82the `PNG_LIBRARIES` value to the `LINK_LIBS` option to the [wiki:CMakeAddLibrary boost_add_library macro]. Using these features together, we can search for the `PNG` library on the system and optionally include PNG support into our library:
    8083
    8184{{{
     
    9093  mysrc1.cpp mysrc2.cpp
    9194  ${EXTRA_SOURCES}
    92   COMPILE_FLAGS "-DBUILDING_BOOST_LIBNAME=1"
    93   LINK_LIBS "${PNG_LIBRARY}"
     95  COMPILE_FLAGS "-DBUILDING_BOOST_LIBNAME=1 ${PNG_DEFINITIONS}"
     96  LINK_LIBS "${PNG_LIBRARIES}"
    9497  SHARED_COMPILE_FLAGS "-DBOOST_LIBNAME_DYN_LINK=1"
    9598  DEPENDS boost_filesystem