Create Library - nenetto/CMakeTemplates GitHub Wiki

This tutorial explain how to create a CMake structure similar to CMake tutorials but using CMakeTemplates.

IMPORTANT: This tutorial is exactly the same as executable tutorial, so images are the same. Just change Executable word by Library

Starting point

Let say you have your SuperBuild project already created. For tutorial purposes we will use the previous SuperBuild that we created in previous tutorial lesson. This project was called MyAwesomeSuperBuild, and right now has nothing useful inside.

Now you want to develop a new library that will be part of your SuperBuild project. For this purpose, we will use CMakeProject. Important: This project is NOT a template, it is a program to create your library template. The trick part is this program runs on CMake, so we will need CMake Configure in order to create our library template.

Steps

  • First, we will decide a cool name for the library , for example... MyAwesomeFirstLibrary
  • Follow the instructions on Configure & Generate
    • This time, your repository source will be the folder called CMakeProject

CMakeProjectConfigure

  • Binary folder can be anywhere in your computer. It does not matter really.
  • Before click on Configure button again, we need to set three options for our project
    1. PROJECT_TYPE: Change to Library because we want to create an library
    2. PROJECT_NAME_DEFAULT: Change to your executable name MyAwesomeFirstLibrary
    3. SOURCE_DIR_DEFAULT: This is where source template for your Library will be created. This option will be a folder with the same name of your Library MyAwesomeFirstLibrary into your SuperBuild Project where you want to include it MyAwesomeSuperBuild

NewExecutable

  • Click on Configure and close CMake, it is done!

Now you created successfully your first project for an Library. We need to do something else, in order your SuperBuild can "see" the change and add it as a subproject.

  • Go to your MyAwesomeSuperBuild folder, and there you will see a new folder with your library's name MyAwesomeFirstLibrary. Navigates inside to the CMake folder
MyAwesomeSuperBuild\MyAwesomeFirstLibrary\CMake
  • Here, copy the file TemplateCreator-internal.cmake to your Projects folder but change the name for your Library's: MyAwesomeSuperBuild\MyAwesomeFirstLibrary\CMake\TemplateCreator-internal.cmake to MyAwesomeSuperBuild\Projects\MyAwesomeFirstLibrary.cmake. This tells to your SuperBuild that the project exists and the source is in the MyAwesomeFirstLibrary folder

Now you can start working. The template has a main function and some example functions. Uses the public header and the cpp for develop your own Library. If you want to add new header files or source (c/cpp) files, do it inside your source folder MyAwesomeFirstLibrary/inc and MyAwesomeFirstLibrary/src