

The actual filename of the library or executable is based on this name but depends on the native platform. It is a logical target name and must be unique within the project. is the name of the target to be built from the list of specified sources.
#Cmake add library code#
The initial folder structure looks like this:Ĭopy Code add_library( source1 )Īdd_executable( source1 ) It is statically linked with the dynamic linked library called sharedmod ( pisym.h and pisym.cpp), which in turn is linked with the main application called theapp ( main.cpp). The static library is called libutil and contains utility functions ( random.h and random.cpp).
#Cmake add library how to#
Throughout the rest of this article, we will discuss how to build C++ code that is grouped in several projects: a static library, a dynamic library and an application. When you build the CMake scripts, VC++ project files are created stored in a temporary location, which are then used by Visual Studio to build, run and debug the code. Different commands, such as building and debugging the code, are also available from the context menu for the CMakeLists.txt files.

The main menu contains an additional item called CMake with different commands, such as building the scripts, managing the CMake cache, changing the CMake settings and others. When you open a folder, you can see the contents of that folder, including the CMakeLists.txt file(s) in the solution explorer. To open the code from such a directory, go to File > Open > Folder. If your code is split across multiple projects and sub-directories, you need to have one in each project directory.

For that, you need to have a CMake script, always called CMakeLists.txt, in the same location with your sources. In Visual Studio 2017, you can open C++ code by using the open folder feature. For an in-depth discussion of the support, see CMake support in Visual Studio. Overview of Support in Visual Studio 2017Īlthough this article is an introduction to CMake, it makes sense to start with a brief walk-through of the support for it in Visual Studio.
#Cmake add library series#
This article provides an introduction to CMake through a series of examples to help you, as VC++ developers, to create and maintain CMake projects. This means it is possible to create, edit, build, run and debug C++ code in Visual Studio 2017 without the need to explicitly generate VC++ projects, as that is taken care behind the scenes by the system. Although CMake has had support for all versions of Visual Studio, starting with version 6, Visual Studio 2017 is the first version to support CMake. CMake uses scripts (called CMakeLists.txt) to generate environment specific build files, such as Visual Studio projects, XCode projects, make scripts and others. CMake is a cross-platform open-source tool for defining the build process for native applications independently of compilers and environments. Pass the name of the library target to target_link_libraries to use the respective library.One of the important changes in Visual Studio 2017 for native development is support for CMake. The target names start with Qt5::, followed by the module name. Imported library targetsĮach Qt module that is loaded defines a CMake library target. For more details, see CMake AUTOMOC documentation. The CMAKE_AUTOMOC setting runs moc automatically when required. Set the Qt5_DIR in the CMake cache to the location of the Qt5Config.cmake file.Set your CMAKE_PREFIX_PATH environment variable to the Qt 5 installation prefix.Target_link_libraries(helloworld Qt5::Widgets)įor find_package to be successful, CMake must find the Qt installation in one of the following ways: Project(helloworld VERSION 1.0.0 LANGUAGES CXX)įind_package(Qt5 COMPONENTS Widgets REQUIRED)
