Importing Executables To start, we will walk through a simple example that creates an IMPORTED executable target and then references it from the add_custom_command () command. Please see that other file for the full documentation # of the example. Add the following lines to the CMakeLists.txt file that defines the library: 1 2 3 4 set(INSTALL_CMAKE_DIR c: / Packages / DemoLibraryClient) Use add_library (IMPORTED) to declare an imported target. You only need to change the project name, and add the files that need to be compiled in foo/CMakeLists.txt. The key part is add_library (IMPORTED) 2, to tell the library is not generated by CMake system and use the library located outside the project. Import your targets inside your Config.cmake So it goes without saying that CMake takes care of the naming conventions and extensions for each platform. I declared the libraries IMPORTED because the libraries' own build system consists of a complex mix of Python, CMake and macros which I found difficult to integrate into . will simply create the file "liblibcool.a" in a Linux environment. Secondly, according to Craig Scott's CMake book, omitting the type argument in add_library (.) Our Solution: In the first package's CMakeLists.txt that contains the pre-compiled shared object that you want to share, you need the following. : export (EXPORT foobarLibTargets FILE "$ {CMAKE_CURRENT_BINARY_DIR}/foobarLibTargets.cmake") creates a Targets.cmake referencing the build tree. is good practice. For executables this is the location of the executable file. The end result is roughly the same to # the end-user. CMake 3.21: I'm trying to wrap fmod with a SHARED IMPORTED target and support Windows builds. For STATIC libraries and modules this is the location of the library or module. Click to expand output results of configuring projects on CMake 3.19.2 it works fine. > > This used to work in 3.0.2, since it allowed me to specify multiple > libraries separated by a semicolon (see . In the Cmake documentation this seems pretty straightforward; to import the .o file: add_library (bar OBJECT IMPORTED GLOBAL) set_property (TARGET bar PROPERTY IMPORTED_LOCATION $ {EXTERNAL_DIR}/bar.o) And to use it: add_executable (foo $<TARGET_OBJECTS:bar> foo.cpp) But when the build runs, I get all missing symbols for the stuff that's . Only after instrumenting the source of cmake itself was I finally able to track this down. For example, add_library (libcool STATIC .) IMPORTED_LOCATION IMPORTED_LOCATION Full path to the main file on disk for an IMPORTED target. A header-only library is imported with add_library (INTERFACE). For example, IMPORTED_LOCATION contains the full path to the target on disk. You can set this property, and like many properties in CMake, it gets it's default value from a CMAKE_CXX_STANDARD variable if it is set, but there is no INTERFACE version - you cannot force a CXX_STANDARD via a target. Use ExternalProject_Add to obtain, configure, and build the library. If you don't specify commands like BUILD_COMMAND or INSTALL_COMMAND, CMake will look for a CMakeLists.txt in the external project and execute it. add_library supports the import of already built libraries as well - we just have to set its IMPORTED_LOCATION . While IMPORTED_LOCATION is empty. If need be, I can > specify a use case for this. After configuring the projects, as we can see, ONLY IMPORTED_LOCATION_<CONFIG> of IMPORTED targets are populated. The .lib name depends on bitness and build type, as does the dll, but the IMPORTED_IMPLIB and IMPORTED_LOCATION properties don't appear to take generator expressions. Set this to the location of an IMPORTED target file on disk. Now we will change the library project to be exported to a package. Set this to the location of an IMPORTED target file on disk. INTERFACE libraries Using header-only libraries in CMake is a breeze. # Example-FindMyPackage-UsingImportedTargets.cmake # # This module does the same thing as Example-FindMyPackage.cmake # except that rather than passing along full path names for libraries, # it creates imported targets. CMake is a cross-platform software for building projects written in C, C++, Fortran, CUDA and so on. Example 2: C++ standard There is a C++ standard property - CXX_STANDARD. These group together properties. Follow this tutorial to create a basic CMake library for Raspberry Pi and import it manually via add_subdirectory (). # include path that has headers required by target project include_directories ($ {install_dir}/include) # import librarues from external project required by target project add_library (lmytls shared imported) set_target_properties (lmytls properties imported_location $ {install_dir}/lib/libmytls.so) add_library (lmyxdot509 shared imported) JRR(John Rocha) Usage in an existing CMake project Having defined all of the above CMake is able to find the required parts of a library except where to find the find.cmake file itself. CMake utilizes build-systems such as Ninja, Linux make, Visual Studio, and Xcode. For quite some time CMake has supported what they call imported targets. Imported targets were originally designed for importing from an existing installation of some external package so installing did not make sense at the time. I was so excited, I thought I had a working solution using the generator expression for IMPORTED_LOCATIONbased on my Windows Visual Studio test, but then it failed on my Linux tests. The export and install commands are both capable of generating cmake files for targets. Hello, My CMake project uses some third party libraries which I declared as IMPORTED then I set the IMPORTED_IMPLIB or IMPORTED_LOCATION properties (depending on compiler and type of library) to the full path and file name. IMPORTED) CMakeCMake IMPORTED_LOCATION IMPORTED_LOCATION_<CONFIG> IMPORTED_LOCATION set_target_properties(foo PROPERTIES IMPORTED_LOCATION /path/to/real/library/libfoo.so) The IMPORTED_LOCATION target property (or its per-configuration variant IMPORTED_LOCATION_<CONFIG>) specifies the location of the main library file on disk: For a SHARED library on most non-Windows platforms, the main library file is the .so or .dylib file used by both linkers and dynamic loaders. It compiles projects with compilers like GCC, Clang, Intel, MS Visual C++. Yes, this was found in Visual Studio. on CMake 3.5.1 (that I must support as it is the default version on Ubuntu 16.04), when configuring , I have the following error: Target . In our case we want to make use of the luajit library which is built using a Makefile. The current CMake integration seems to just create variables to the directories (include, lib, bin, ) for each dependency. For STATIC libraries and modules this is the location of the library or module. IMPORTED_LOCATION $ {LIBIMAGEPIPELINE_LIBRARY} is the location of the library file provided by find_library. By using the NAMESPACE argument, we tell CMake to prepend the prefix JSONUtils:: to all targets imported. Update: now using modern cmake (version >= 3.9), since commit 46f0b93. When declaring dependency to in , I wrap it into third::third. This will install the import script JSONUtilsTargets.cmake that, when included in other scripts, will load the targets defined in the export jsonutils-export. Autogenetared library version file: #include <foo/version.h> FOO_DEBUG added on Debug. CMakeLists.txt for Go is based on the post in Stackoverflow 1 . However, because the library is already built, you need to use the IMPORTED flag to tell CMake that you only want to import the library into your project: add_library ( imported-lib SHARED IMPORTED ) You then need to specify the path to the library using the set_target_properties () command as shown below. > > I thought I could simply declare this target as > > add_library(Foo::Foo UNKNOWN IMPORTED) > > and then configure its dependencies via the target property > IMPORTED_LOCATION. # I will see about creating a simple example to prove and file an issue as suggested. However you can simplify your command using the following syntax: if (CMAKE_BUILD_TYPE) get_target_property (PY_LIB_ADDRESS python IMPORTED_LOCATION_$ {CMAKE_BUILD_TYPE}) else () get_target_property (PY_LIB_ADDRESS python IMPORTED_LOCATION) endif () And to finish, as far as I know, get_filename_component () does not support generator expressions. Link the consumer to the dependency. The export command e.g. Features The main advantage of this example is that it is auto-generated . With IMPORTED_LOCATION and INTERFACE_INCLUDE_DIRECTORIES, CMake can detect where the library (.a or .so) and headers (.h) are. application depends privately on a home-made (static)library which depends privately on a third-party library . CMake is frequently used in compiling open-source and commercial projects. For example: I would have expected conan to declare proper imported targets for each dependency. For bundles on macOS this is the location of the executable file inside Contents/MacOS under the application bundle folder. For executables this is the location of the executable file. We'll need to do some setup to get started. The following is the example CMakeLists.txt code using OpenCV.