An example being add_library (MathFunctions mysqrt.cxx) To make use of the new library we will add an add_subdirectory () call in the top-level CMakeLists.txt file so that the library will get built. We add the new library to the executable, and add MathFunctions as an include directory so that the mysqrt.h header file can be found. 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. If a library does not export any symbols, it must not be declared as a SHARED library. INCLUDE_DIRECTORIES (/path/to/headers) ADD_EXECUTABLE (TARGET target.c) TARGET_LINK_LIBRARIES (TARGET_FILE "-L/path/to/shared/library" SHARED_LIB_name) Make sure that you have CMake installed prior to running this example (go here for instructions). CMAKE_SYSTEM_LIBRARY_PATH CMAKE_SYSTEM_FRAMEWORK_PATH The platform paths that these variables contain are locations that typically include installed software. This is done in the build system of the library. cmake-example-library CMake library example that can be found using find_package (). Im setting up an imported library: add_library(Example::LibA SHARED IMPORTED) set_target_properties(Example::LibA PROPERTIES IMPORTED_LOCATION "${LibA_LIBRARY}" IMPORTED_IMPLIB "${LibA_LIBRARY}" cmake Add Directories to Compiler Include PathSyntax #Parameters #Add a Project's Subdirectory #. Thus, the file myHeader.h in the project's include subdirectory can be included via #include "myHeader.h" in the main.cpp file. For example, a Python, Java, C# or Object Pascal application can use a shared library developed in C. In this article, Ill show you how you can create your own shared library. There are three directories involved. The libconvert shared library was developed CMakeLists.txt in shape, rectangle and square are the same: target_sources(app PRIVATE base.cpp base.h) Shared library The code for this example is here on GitHub. Run the appropriate vsdevcmd.bat file (x86/x64). For more information, see Building on the command line .Switch to your output folder.Run CMake to build or configure your app. I had a hard time finding a clear example of how to build a shared library, install it (in my case in /usr/local/) and then use the installed library in a new project, so I decided to cmake_minimum_required(VERSION 3.8 FATAL_ERROR) project(cmake_and_cuda LANGUAGES CXX CUDA) include(CTest) add_library(particles STATIC randomize.cpp randomize.h particle.cu particle.h v3.cu v3.h ) If you leave this choice off, the value of BUILD_SHARED_LIBS will be used to pick between STATIC and SHARED. Simple project to demonstrate how to create shared (dynamic) library using C++ & CMake library cxx dynamic-library shared-library cmake-shared-library cmake-sample For example, a Windows resource DLL or a managed C++/CLI DLL that exports no unmanaged This project can be used as a starting template to create a shared library (.dll on Windows / .so on Linux). I have provided the full code for this example on Github. Instead of using target_link_libraries() to link to the object library, try the following: add_library(runtime OBJECT ${runtime_src}) add_library(mylib $) This causes the object files from runtime to be used as the sources for mylib (whether it be shared or static.) The top level directory has two subdirectories called ./Demo I'm also providing a sample project at GitHub I got a project where I use a third party library (Windows). This can all be achieved with CMakes add_library () function. In this article I describe a simple way to create a shared library in your C++ / CMake project while accounting for platform differences. If we use CMake, we augment the shared library target: target_compile_definitions (mysharedlib PRIVATE MYSHAREDLIB_LIBRARY) If we use qmake, we add DEFINES += MYSHAREDLIB_LIBRARY to the .pro file of the shared library. The notable features of this template are the This minimal CMakeLists.txt file compiles a simple shared library: cmake_minimum_required (VERSION 2.8) project (test) set (CMAKE_BUILD_TYPE Release) Update: now using modern cmake (version >= 3.9), since commit 46f0b93. For example, There are four steps:Compile C++ library code to object file (using g++)Create shared library file (.SO) using gcc --sharedCompile the C++ code using the header library file using the shared library (using g++)Set LD_LIBRARY_PATHRun the executable (using a.out)Step 1: Compile C code to object file If the target youre building is a library, CMake will build a static one by default. In this section we will show how the BUILD_SHARED_LIBS variable can be used to control the default behavior of add_library () , and allow control over how libraries without an explicit type ( 1 cmake_minimum_required(VERSION 3.9) xxxxxxxxxx 1 1 project(mylib VERSION 1.0.1 DESCRIPTION "mylib description") xxxxxxxxxx 5 1 add_library(mylib SHARED 2 sources/animation.cpp 3 sources/buffers.cpp 4 [] 5 ) xxxxxxxxxx 1 1 set_target_properties(mylib PROPERTIES VERSION ${PROJECT_VERSION}) xxxxxxxxxx 1 Learn how to create different library types with CMake, and how to include them in your executables! This example shared library embeds functionality for unit conversion between both Celsius and Fahrenheit, and kilometers and miles. GitHub - alessiosacco/cmake-shared-library: Example CMake Shared Library with install / export support and unit testing using Google Test main 1 branch 0 tags Go to file Code alessiosacco Replace tabs with spaces db3ec8f on Aug 20, 2021 4 commits cmake Initial Commit 14 months ago include/ SharedLibrary Replace tabs with spaces 14 months ago src A minimal example using cmake to create a c++ shared library. A.lib A.dll B.dll B.dll is a dependency of A.dll not exposed to the A.dll interface. @leimao: Like @ax3l says, if you don't have to build a separate shared library, it is easier to avoid it. How to create a shared library with cmake? 01 May 2019 cmake, cpp, projecteuler 1# CMakeLists.txt for PrimUtil a library for utilities related to working To build a shared library, set BUILD_SHARED_LIBS=ON like this: cmake -DBUILD_SHARED_LIBS=ON Some generators (Visual Studio and XCode) support building multiple configurations of your projects in one build directory. Not only does it complicate the install, but it also means you have to make sure that RPATH gets set right (or use LD_LIBRARY_PATH), which was the gist of the original issue. This is a gotcha when using object libraries. CMake Shared Library. In this 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 .. Listing 1 shows the CMake file for a CUDA example called particles.