Changes between Version 4 and Version 5 of CMakeAddingCompiledLibrary
- Timestamp:
- Jun 12, 2007, 4:25:55 PM (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
CMakeAddingCompiledLibrary
v4 v5 39 39 When 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. 40 40 41 == Linking to Other Libraries ==41 == Linking to Other Boost Libraries == 42 42 Some 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`: 43 43 … … 56 56 Sometimes, 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. 57 57 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:58 CMake 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: 59 59 {{{ 60 60 include(FindPNG) 61 61 }}} 62 62 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 providea 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`: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 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`: 64 64 65 65 {{{ … … 71 71 }}} 72 72 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., 73 CMake 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 75 The include directory should be added via the CMake `include_directories` macro, e.g., 74 76 75 77 {{{ 76 include_directories(${PNG_ PNG_INCLUDE_DIR})78 include_directories(${PNG_INCLUDE_DIR}) 77 79 }}} 78 80 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: 81 The `PNG_DEFINITIONS` value should be added to the `COMPILE_FLAGS` and 82 the `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: 80 83 81 84 {{{ … … 90 93 mysrc1.cpp mysrc2.cpp 91 94 ${EXTRA_SOURCES} 92 COMPILE_FLAGS "-DBUILDING_BOOST_LIBNAME=1 "93 LINK_LIBS "${PNG_LIBRAR Y}"95 COMPILE_FLAGS "-DBUILDING_BOOST_LIBNAME=1 ${PNG_DEFINITIONS}" 96 LINK_LIBS "${PNG_LIBRARIES}" 94 97 SHARED_COMPILE_FLAGS "-DBOOST_LIBNAME_DYN_LINK=1" 95 98 DEPENDS boost_filesystem