First, create a directory for your project: $ mkdir my_project && cd my_project Next, you'll create the CMakeLists.txt file and declare a dependency on GoogleTest. "Hello World" as a library This example shows how to deploy the "Hello World" program as a library and how to link it with other targets. cmake_minimum_required(VERSION 3.2 FATAL_ERROR) project(OpenGLExample) Source files and libraries Now we will create two CMake variables to hold *.c/*.cpp and *.h/*.hpp source files. Building a C/C++ unit test (gtest) We use Google Test (gtest) for unit testing of C/C++ code. The latter call is needed to tell CMake that the executable testTF depends on the library libTF. Let us see how add_subdirectory is used to add a dependency. It isn't fully automated, but compile once is easy enough: add_library (l1-standalone STATIC a.cpp b.cpp) add_library (l1-shared SHARED $<TARGET_OBJECTS:l1-standalone>) set_property (TARGET l1-standalone PROPERTY POSITION_INDEPENDENT_CODE 1) This command, when placed in the root CMake script, declares a subproject test that has its own CMakeLists.txt.. After reloading the changes in both CMakeLists.txt files, CLion creates a Run/Debug configuration for the cmake_testapp . CMake Examples Introduction. The write_basic_package_version_file () function from above will create SomeLibraryConfigVersion.cmake file in the install folder. Update: now using modern cmake (version >= 3.9), since commit 46f0b93. gcc -Ifirst_dir -Isecond_dir -o my_program program.cpp Interestingly, the -I flag tells the compiler to look for files in the directory following the flag. Note that you typically do not list header files here. target_include_directories(): To tell CMake that the project directory tree contains headers.In this way, we can have headers from different directories added to each other with a relative path to the project directory. Here we have simplified syntax of add_library (<name> [STATIC | SHARED] [<source>.]). You can use INCLUDE_DIRECTORIES for header location and LINK_DIRECTORIES + TARGET_LINK_LIBRARIES for libraries INCLUDE_DIRECTORIES (your/header/dir) LINK_DIRECTORIES (your/library/dir) rosbuild_add_executable (kinectueye src/kinect_ueye.cpp) TARGET_LINK_LIBRARIES (kinectueye lib1 lib2 lib2 .) Jay_K: Apparently this is a faq: build static and dynamic libraries. cmake-example-library CMake library example that can be found using find_package (). Example # To create an build target that creates an library, use the add_library command: add_library (my_lib lib.cpp) The CMake variable BUILD_SHARED_LIBS controls whenever to build an static ( OFF) or an shared ( ON) library, using for example cmake .. -DBUILD_SHARED_LIBS=ON. Prefer to pass full absolute paths to libraries where possible, since this ensures the correct library will always be linked. Because the wrong library is used for linking the release version I get build warnings that LIBCMT and LIBCMTD are conflicting. Features The main advantage of this example is that it is auto-generated . is good practice. add_executable(): is to define app target. You can set the build up so that CMake will download the code directly from its source repo, compile the DLL, and incorporate it into your project as a library target constructed from the build directory, all as a prerequisite to compiling your code. We can extend our executable from above by linking it to our libray libtest.a. Last parameter is source. add_executable (helloworld main.cpp ) add_executable () tells CMake that we want to build an executable (so not a library) called helloworld as a target. As an example, if your library depends on Boost.Regex, your FooConfig.cmake.in will look something like this: @PACKAGE_INIT@ find_dependency (Boost 1.60 REQUIRED COMPONENTS regex) include ("$ {CMAKE_CURRENT_LIST_DIR}/FooTargets.cmake") add_executable (. Here's an example that builds a library ( image_loader) and then a gtest to test it: Now, we can use the library defined in CMakeLists.txt of libtest_project in myapp's CMakeLists.txt: If you leave this choice off, the value of BUILD_SHARED_LIBS will be used to pick between STATIC and SHARED. I have the problem that the wrong library (debug library) is picked when I create a project file for Visual Studio with CMake. Instead other targets created by add_library () or add_executable () may reference the objects using an expression of the form $<TARGET_OBJECTS:objlib> as a source, where objlib is the object library name. It can be used to support multiple native build environments including make, Apple's xcode and Microsoft Visual Studio. set the variable CMAKE_LIBRARY_PATH set (CMAKE_LIBRARY_PATH path1 path2) find_library (NAMES gtest) the reason is as flowings: Note This command is rarely necessary and should be avoided where there are other choices. Where the library name is the name of the shared library, minus the first lib part and minus the .so file extension. target_sources(): to add the source in the currrent directory, app.cpp, to app target. Also, we need to place the add_subdirectory(test) command in the root CMakeLists.txt to make our test target cmake_testapp_boost available for the main build.. $<TARGET_OBJECTS:objlib> .) Instead other targets created by add_library () or add_executable () may reference the objects using an expression of the form $<TARGET_OBJECTS:objlib> as a source, where objlib is the object library name. Make sure that you have CMake installed prior to running this example (go here for instructions). add_executable(example_exe main.cpp) Yep, just one line. You do this with linker flag: -l<library name>. For example: add_library (. add_library + target_link_libraries; add_subdirectory; In modern CMake, add_dependencies option is rarely used and hence I am not adding CMake add_dependencies to the above list. Making a library Making a library is done with add_library, and is just about as simple: add_library(one STATIC two.cpp three.h) You get to pick a type of library, STATIC, SHARED, or MODULE. There is even a helper macro for use within FooConfig.cmake, find_dependency. Here, we define our first target, example_exe. Let's start by adding the library's directory as a subdirectory to our myapp project. add_library(particles STATIC randomize.cpp randomize.h particle.cu particle.h v3.cu v3.h ) # Request that particles be built with -std=c++11 # As this is a public compile feature anything that links to # particles will also build with -std=c++11 target_compile_features(particles PUBLIC cxx_std_11) if you omit this parameter, library will be static by default. We can have more than one source file. CMake is a cross-platform open-source meta-build system which can build, test and package software. In the add_executable call, we can specify a list of sources needed to build your library or executable. add_library(test SHARED test.c) Linking libraries to executables with CMake. The best example I could find was CMake using itself to build. Rather than placing all of the source files in one directory, we can organize our project with one or more subdirectories. Set up a project CMake uses a file named CMakeLists.txt to configure the build system for a project. For example: add_library (. $<TARGET_OBJECTS:objlib> .) The top level directory has two subdirectories called ./Demo and ./Hello. For this program, we have one library (MyLibExample) with a header file and a source file, and one application, MyExample, with one source file. A simple example Here's a small example of a library that uses Boost in its headers and therefore wishes to have its clients setup those directories as well: 1 2 3 4 5 6 7 8 9 10 set (TARGET_NAME cool_lib) add_library ($ {TARGET_NAME} STATIC $<TARGET_OBJECTS:objlib> .) In this case, we will create a subdirectory specifically for our library. c++ compilation cmake shared-libraries Share Follow edited Nov 21, 2017 at 14:28 Jrme Pouiller There's also the PRIVATE keyword that can be used to avoid adding the settings to all dependent targets. For example, add_library (libcool STATIC .) Sample test/CMakeLists.txt file add_executable (loadtbb loadtbb.cpp) target_link_libraies (loadtbb $ {TBB_LIBS} ) add_test (loadtbb_test loadtbb) Creating the tbb.cmake file Create an empty file using your favorite text editor called tbb.cmake in the thirdparty directory. add_executable (. # Almost all CMake files should start with this # You should always specify a range with the newest # and oldest tested versions of CMake. You'll use this file to set up your project and declare a dependency on GoogleTest. will simply create the file "liblibcool.a" in a Linux environment. The solution is simple: When linking a shared library to your C application, you need to inform the GCC toolchain about the library you want to link. It would also be helpful if someone could just tell me a very simple library that uses cmake, so I can use this as an example. For example, you can have: add_executable(example_exe Game.cpp ResourceManager.cpp main.cpp) In the directory ./Demo, an executable is built by linking to the library. Next is type of library STATIC or SHARED which I explained before. To do so, we will use file command with GLOB_RECURSE parameter. Having it, if you now try to find your package in external project ( cmake-library-example/external-project/CMakeLists.txt) like this: find_package (SomeLibrary 0.9.2 CONFIG REQUIRED) You only need to change the project name, and add the files that need to be compiled in foo/CMakeLists.txt. In the directory ./Hello, a library is built. So it goes without saying that CMake takes care of the naming conventions and extensions for each platform. Most examples that I have found compile executables with some shared libraries but never just a plain shared library. I would start with upgrade of CMAKE version. Secondly, according to Craig Scott's CMake book, omitting the type argument in add_library (.) First variable will be called SOURCE_FILES and second HEADER_FILES. Your directory structure should look like this: The target should be built from the C++ source file main.cpp. This is a simple yet complete example of a proper CMakeLists. First is name of library for us is HelloLibrary. Say we have the same set of source/header files as in the http://www.riptutorial.com/cmake/example/22391/-hello-world--with-multiple-source-files example. cmake -DCMAKE_INSTALL_PREFIX=~/mylib/install .. There are three directories involved. To add a library in CMake, use the add_library () command and specify which source files should make up the library. $<TARGET_OBJECTS:objlib> .) For example, compiling the code in the source file program.cpp that includes the header files first_dir/first_include.h and second_dir/second_include.h needs the following command. In this example the files would be installed in the ~/mylib/install directory: Create and install the shared library's pkg-config file with CMake At this point we built the shared library and installed it system-wide, with the help of CMake.