Skip to content

How to build the DateTime library or integrate it in your application

Jeremy Dumais edited this page Sep 22, 2019 · 1 revision

Generic Build Instructions

Setup

To build CPP-DateTime-library, you need to tell your build system where to find its headers and source files. The exact way to do it depends on which build system you use, and is usually straightforward.

Build with CMake

CPP-DateTime-library comes with a CMake build script ( CMakeLists.txt) that can be used on a wide range of platforms ("C" stands for cross-platform.). If you don't have CMake installed already, you can download it for free from http://www.cmake.org/.

CMake works by generating native makefiles or build projects that can be used in the compiler environment of your choice. You can either build CPP-DateTime-library as a standalone project or it can be incorporated into an existing CMake build for another project.

Standalone CMake Project

When building CPP-DateTime-library as a standalone project, the typical workflow starts with:

mkdir build       # Create a directory to hold the build output.
cd build
cmake ..  		  # Generate native build scripts.

If you are on a *nix system, you should now see a Makefile in the current directory. Just type 'make' to build CPP-DateTime-library.

If you use Windows and have Visual Studio installed, just click File -> Open -> Folder... and the select the folder where you have downloaded CPP-DateTime-library. You can then build them using Visual Studio.

Incorporating Into An Existing CMake Project

If you want to use CPP-DateTime-library in a project which already uses CMake, then a more robust and flexible approach is to build CPP-DateTime-library as part of that project directly. This is done by making the CPP-DateTime-library source code available to the main build and adding it using CMake's add_subdirectory() command. This has the significant advantage that the same compiler and linker settings are used between CPP-DateTime-library and the rest of your project, so issues associated with using incompatible libraries (eg debug/release), etc. are avoided. This is particularly useful on Windows. Making CPP-DateTime-library's source code available to the main build can be done a few different ways:

  • Download the CPP-DateTime-library source code manually and place it at a known location. This is the least flexible approach and can make it more difficult to use with continuous integration systems, etc.
  • Embed the CPP-DateTime-library source code as a direct copy in the main project's source tree. This is often the simplest approach, but is also the hardest to keep up to date. Some organizations may not permit this method.
  • Add CPP-DateTime-library as a git submodule or equivalent. This may not always be possible or appropriate. Git submodules, for example, have their own set of advantages and drawbacks.
  • Use CMake to download CPP-DateTime-library as part of the build's configure step. This is just a little more complex, but doesn't have the limitations of the other methods.

The last of the above methods is implemented with a small piece of CMake code in a separate file (e.g. CMakeLists.txt.in) which is copied to the build area and then invoked as a sub-build during the CMake stage. That directory is then pulled into the main build with add_subdirectory(). For example:

New file CMakeLists.txt.in:

cmake_minimum_required(VERSION 3.10)

project(datetimelibrary-download NONE)

include(ExternalProject)
ExternalProject_Add(datetimelibrary
  GIT_REPOSITORY    https://github.com/jeremydumais/CPP-DateTime-library.git
  GIT_TAG           master
  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/datetimelibrary-src"
  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/datetimelibrary-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)

Existing build's CMakeLists.txt:

# Download and unpack datetimelibrary at configure time
configure_file(CMakeLists.txt.in datetimelibrary-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/datetimelibrary-download )
if(result)
  message(FATAL_ERROR "CMake step for datetimelibrary failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/datetimelibrary-download )
if(result)
  message(FATAL_ERROR "Build step for datetimelibrary failed: ${result}")
endif()

#Add datetimelibrary directly to our build. This defines
#the datetime target.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/datetimelibrary-src
                 ${CMAKE_CURRENT_BINARY_DIR}/datetimelibrary-build
                 EXCLUDE_FROM_ALL)

include_directories(${CMAKE_CURRENT_BINARY_DIR}/datetimelibrary-src/src)

# Now simply link against datetime as needed. Eg
add_executable(example example.cpp)
target_link_libraries(example datetime)