Skip to content

How to build the SMTP client or integrate it in your application

Jeremy Dumais edited this page Jul 26, 2021 · 4 revisions

Generic Build Instructions

Setup

To build CPP-SMTPClient-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-SMTPClient-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-SMTPClient-library as a standalone project or it can be incorporated into an existing CMake build for another project.

Windows and OpenSSL (for CPP-SMTPClient-library version 1.1.0 and later)

CPP-SMTPClient-library require OpenSSL so you can download one the precompiled version here: https://www.npcglib.org/~stathis/blog/precompiled-openssl/.


Standalone CMake Project


Build the CPP-SMTPClient-library on Linux

Build the CPP-SMTPClient-library on Windows


Incorporating Into An Existing CMake Project

If you want to use CPP-SMTPClient-library in a project which already uses CMake, then a more robust and flexible approach is to build CPP-SMTPClient-library as part of that project directly. This is done by making the CPP-SMTPClient-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-SMTPClient-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-SMTPClient-library's source code available to the main build can be done a few different ways:

  • Download the CPP-SMTPClient-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-SMTPClient-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-SMTPClient-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-SMTPClient-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(smtpclientlibrary-download NONE)

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

Existing build's CMakeLists.txt:

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

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

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

# If you are on Windows, uncomment the 2 following lines:
#include_directories($ENV{OPENSSL_INCLUDE_DIR})
#link_directories($ENV{OPENSSL_CRYPTO_LIBRARY})

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