Skip to content

Creating mahi lib based projects

Nathan B Dunkelberger edited this page Nov 17, 2020 · 4 revisions

Setting Up your Project Directory and CMakeLists.txt

Note: This explains the process using mahi-util, but the same process can be used for any of the other mahi-lib projects

Now you have the mahi-util library built and installed, it's time to create your own project which uses it. Here's a typical C++ folder structure and CMakeLists.txt you can start with:

Note: You can save yourself some time by downloading the provided project template.

MyProject             # top level of your project
├── CMakeLists.txt    # your top-level CMakeLists.txt
├── build             # conventionally where building occurs
├── include           # where your header files live
    ├── MyClass.hpp   # a class header file
    ├── ...           # more header files
├── src               # where your source files live
    ├── MyClass.cpp   # a class source file
    ├── ...           # more source files
    ├── my_app.cpp    # an application file defining main()

CMakeLists.txt would then look like this:

# set minimum needed CMake version (always required)
cmake_minimum_required(VERSION 3.13)

# create your C++ project
project(MyProject VERSION 0.1.0 LANGUAGES CXX)

# add mahi-util to the project and make it available for this file to use
FetchContent_Declare(mahi-util GIT_REPOSITORY https://github.com/mahilab/mahi-util.git)
FetchContent_MakeAvailable(mahi-util)

# add your include directories
include_directories("include")

# create an app from your source files
add_executable(my_app "include/MyClass.hpp" "src/MyClass.cpp" "src/my_app.cpp")

# link your app to mahi-lib libraries
target_link_libraries(my_app mahi::util)

Note that we don't necessarily need to link to all libraries that we use. If the first library includes a second library as a dependency, you only need to link the first libary. For example, mahi::com depends on mahi::util, so you only need to link mahi::com and you will be able to use everything from mahi::com and mahi::util:

target_link_libraries(my_app mahi::com) # don't need mahi::util

Building your Project

To generate your project build files, open a command prompt at the top level of your project directory and run:

> mkdir build                      # make build directory if it doesn't exist
> cd build                         # change directory to ./build
> cmake .. -G "GENERATOR STRING"   # call cmake with same generator used for mahi-util

Now you can proceed to compile your project:

> cmake --build . --config Release

You should be able to immediately run your executable!

Clone this wiki locally