![]() ![]() ![]() Linking CXX static library libSomeLibrary.a Building CXX object CMakeFiles/SomeLibrary.dir/src/ Scanning dependencies of target SomeLibrary Configuring done - Generating done - Build files have been written to: /Users/YOURNAME/code/cpp/someLibrary/build Will set it to /Users/YOURNAME/code/cpp/someLibrary/install Detecting CXX compile features - done - This project is a top-level one Detecting CXX compiler ABI info - done - Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped Detecting C compile features - done - Detecting CXX compiler ABI info Detecting C compiler ABI info - done - Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped The CXX compiler identification is AppleClang 12.0032 The C compiler identification is AppleClang 12.0032 ![]() Having it, if you now try to find your package in external project ( cmake-library-example/external-project/CMakeLists.txt) like this: The write_basic_package_version_file() function from above will create SomeLibrar圜onfigVersion.cmake file in the install folder. Knowing that, here’s how you can check if you are in the top-level project or not: This variable persists across all the nested projects, and so calling project command from nested projects will not change CMAKE_PROJECT_NAME, but will set another variable called PROJECT_NAME. It stores the top-level project name that you set with project command. In CMake projects there is a variable called CMAKE_PROJECT_NAME. So let’s gradually create a CMakeLists.txt for the library project. will you need to have it as DLL on WindowsĮverything from this list is handled by CMake.when its sources are nested in their source tree. ![]() when they are using it pre-built as an external dependency.how other applications can find the library.where install artifacts should be placed.what artifacts should the library produce at install step.Here are some of the things you need to take care of: But doing just that would be too easy, wouldn’t it. Making a library with CMake is not that different from making an application - instead of add_executable you call add_library. In case you haven’t seen them, my previous posts on CMake are:ĭesigning a big project needs a good understanding of namespaces, see my post on how namespaces are used in big projects.ĭid you know CMake is supported by Visual Studio code? have a look at my essential list of VS code extensions for C++.#include const std ::string someString = "some string" CMakeLists Now you can compile and run your program, in the source directory mkdir build You can run any customized task with this command, read CMake Manual here. add_custom_command: I used it here for copying a file after build.This is because on Windows, the location of shared libraries, dll files, is not coded into the executable that uses them. : this block is only for Windows to copy dll files from the package installed directory to the executable directory. target_link_libraries: remember to use the namespace you set for your package, here geo::geo.find_package(geo): asking CMake to include the package and its targets.This is the address of the installed config files. ): (nameOfpackage)_DIR is where CMake looks for a package. The key lines for importing geo library are It is a shared library with the file structure below: - geometryĬmake_minimum_required ( VERSION 3.23 ) project ( geometry LANGUAGES CXX VERSION 5.4.3 ) if ( MSVC ) set ( CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON ) endif () add_library ( geo SHARED ) target_include_directories ( geo PRIVATE "$ /Release ) endif () This post is the third on CMake, I assume you had a look at the previous ones : CMake programming and build with CMake. In this post, with an example, I show how to createįor a library so it can be easily imported to other projects. We as the developers of the library can add several boilerplate lines to our CMake script to make everything get imported by the user with a simple line of find_package(). This extra task for the user of our library can be a pain. However, a user of the library needs to write some CMake code to find the include headers, shared/static libraries, and executables. We write CMake for our library and it compiles correctly. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |