lowqert.blogg.se

Cmake target
Cmake target






(starting in CMake 3.11), and they can have :: in their name. INTERFACE libraries, they can be built and modified using the same syntax as other libraries Imported library in CMake, and uses the keyword IMPORTED. The second situation is if you have a pre-built library that you want to use. Now you can set INTERFACE properties on this only Notice you didn’t need to add any source files. These are called interface libraries in CMake and youĪdd_library (some_header_only_lib INTERFACE ) Where the target language is useful, but you need some extra flexibility over what we’ve covered.įirst, you might have a library that conceptually should be a target, but doesn’t actually have anyīuilt components - a “header-only” library. That’s great! However, you’ll quickly run into two more situations You might be really excited by targets and are already planning out how you can describe your target_link_options: General link flags (CMake 3.13+).

Cmake target full#

  • target_link_directories: Don’t use, give full paths instead (CMake 3.13+).
  • target_compile_options: More general compile flags.
  • target_compile_definitions: Definitions.
  • target_compile_features: The compiler features you need activated, like cxx_std_11.
  • target_include_directories: Include directories.
  • target_link_libraries: Other targets can also pass library names directly.
  • , and # it should be expanded to the current source dir target_include_directories (simple_lib PUBLIC " $ " ) # Adding definitions target_compile_definitions (simple_lib PUBLIC MYLIB_PUBLIC ) target_compile_definitions (simple_lib PRIVATE MYLIB_PRIVATE ) # Require a C++ feature (here: at least C++11) target_compile_features (simple_lib PUBLIC cxx_std_11 ) # Now add the executable add_executable (simple_example simple_example.cpp ) # Adding the all-important link to simple-lib target_link_libraries (simple_example PUBLIC simple_lib ) Things you can set on targets target_compile_features can fillĬOMPILE_FEATURES and INTERFACE_COMPILE_FEATURES, just like directories in example 1.Ĭmake_minimum_required (VERSION 3.15.3.25 ) project (MyExample01 LANGUAGES CXX ) # This is the library Including the headers is not required, but is nice for # users add_library (simple_lib simple_lib.cpp simple_lib.hpp ) # The above line *did not* set the includes - we need to We can also set. Will compile with at least the highest level specified, unless CXX_STANDARD is set (and that’sĪ nice, clear error if you set CXX_STANDARD too low). To compile a target the cxx_std_11 and similar meta-features are perfect for this - your target What would youĭo if you had a C++11 interface target and a C++14 interface target and linked to both?īy the way, there is a way to handle this - you can specify the minimum compile features you need Properties in CMake, it gets it’s default value from a CMAKE_CXX_STANDARD variable if it is set,īut there is no INTERFACE version - you cannot force a CXX_STANDARD via a target. There is a C++ standard property - CXX_STANDARD. PUBLIC, then both properties are appended to at the same time. INTERFACE instead, then INTERFACE_INCLUDE_DIRECTORIES is appended to, instead. The INCLUDE_DIRECTORIES property of TargetA has mydir appended. When you run target_include_directories(TargetA PRIVATE mydir), then The PUBLIC keyword fills both propertyįields at the same time. Tell targets linked to this one what to do when building. “private” properties control what happens when you build that target, and the “interface” properties There are two collections of properties on every target that can be filled with values the Sees through mylibrary the private library will not affect it. myprogram will build the three libraries it P(Public Library) - > |PUBLIC| M((Main Library))įigure 1: Example of PUBLIC, PRIVATE, and INTERFACE. Maybe you do require users have C++14, but your library can compile with any version of C++.
  • users don’t need to be forced into C++14 mode.
  • Only the implementations inside my_lib.cpp require C++14, then this is a PRIVATE requirement However, if the header is valid in all versions of C++, and If the header contains C++14, this is a PUBLIC requirement - both If you then add my_exe, and it needs my_lib, should that force my_exe to compile You have a library, my_lib, made from my_lib.hpp and my_lib.cpp. This keyword when making a library! CMake goes into an old compatibility mode for this target that

    cmake target cmake target

    Target_link_libraries and a keyword one of PUBLIC, PRIVATE, and INTERFACE. Once you have several targets, you can describe the relationship between them with More on that later, once we see what we can do with targets. To make the default is sort-of an “auto” library that is user selectable with BUILD_SHARED_LIBS. You can add the keywords STATIC, SHARED, or MODULE if you know what kind of library you want






    Cmake target