Understanding FindArmadillo: A CMake Module for Armadillo Integration


  • Implementation

    • The FindArmadillo.cmake module searches for the Armadillo include directory by default in locations like $ENV{ProgramFiles}/Armadillo/include (on Windows).
    • If found, it tries to extract version information by parsing the armadillo_version.hpp header file.
  • Usage
    In your CMakeLists.txt, you'll typically use the following commands:

    find_package(Armadillo REQUIRED)  # Find Armadillo (report error if not found)
    include_directories(${ARMADILLO_INCLUDE_DIRS})  # Add Armadillo include paths
    # ... (your code using Armadillo)
    add_executable(your_program your_program.cpp)  # Create your executable
    target_link_libraries(your_program ${ARMADILLO_LIBRARIES})  # Link against Armadillo libraries
    
    • Searches for Armadillo on your system.
    • Sets essential variables if found:
      • ARMADILLO_FOUND: Indicates successful discovery (TRUE/FALSE).
      • ARMADILLO_INCLUDE_DIRS: Paths to include directories containing Armadillo headers.
      • ARMADILLO_LIBRARIES: Names of libraries to link against for using Armadillo.
      • ARMADILLO_VERSION_*: Variables holding Armadillo's version information (major, minor, patch, string).

Points to Consider

  • This module is included in the core CMake distribution (since version 3.18). You might not need to download it separately.


CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

# Find Armadillo (report error if not found)
find_package(Armadillo REQUIRED)

# Include Armadillo headers
include_directories(${ARMADILLO_INCLUDE_DIRS})

# Simple program using Armadillo
add_executable(arma_test arma_test.cpp)

# Link against Armadillo libraries
target_link_libraries(arma_test ${ARMADILLO_LIBRARIES})

arma_test.cpp

#include <armadillo>

int main() {
  arma::mat A = arma::randu<arma::mat>(3, 3);
  arma::vec b = arma::randu<arma::vec>(3);

  arma::vec x = arma::solve(A, b);

  std::cout << "Solution:" << std::endl << x << std::endl;

  return 0;
}
  1. CMakeLists.txt
    • Sets the minimum required CMake version (adjust as needed).
    • Uses find_package(Armadillo REQUIRED) to locate Armadillo.
    • Includes Armadillo headers using include_directories.
    • Defines an executable arma_test from arma_test.cpp.
    • Links the executable with Armadillo libraries using target_link_libraries.
  2. arma_test.cpp
    • Includes the Armadillo header <armadillo>.
    • Creates a random matrix A and vector b using arma::randu.
    • Solves the linear system Ax = b using arma::solve.
    • Prints the solution vector x.
  1. Create a directory for your project and place the above files (CMakeLists.txt and arma_test.cpp) inside.
  2. Open a terminal in the project directory.
  3. Run cmake . to generate build files.
  4. Run your preferred build command (e.g., make on Linux/macOS, platform-specific build tools on Windows) to create the executable.
  5. Run the executable (./arma_test on Linux/macOS, adjust for Windows) to see the solution printed to the console.


    • This involves manually specifying the include directories and library paths for Armadillo in your CMakeLists.txt file.
    • You'll need to locate these paths on your system, which can be less convenient and more error-prone compared to using FindArmadillo.
  1. Download and Build Armadillo

    • Instead of relying on a pre-installed Armadillo, you can download the source code and build it yourself within your project.
    • CMake offers functionalities like ExternalProject_Add to manage external dependencies like Armadillo.
    • This approach gives you more control over the build process but requires additional setup and maintenance.
  2. Third-party Find Modules

    • Some projects might provide their own FindArmadillo.cmake module that might have additional features or bug fixes compared to the one included in CMake.
    • Caution
      Be cautious when using third-party modules, ensure they are well-maintained and come from a trusted source.
MethodProsCons
FindArmadilloEasy to use, integrates with core CMakeRelies on pre-installed Armadillo
Manual ConfigurationNo external dependenciesRequires manual path management, error-prone
Build ArmadilloMore control over build processRequires additional setup and maintenance
Third-party Find ModulesPotentially more features/bug fixesRequires trust in the source, maintenance burden

Recommendation

For most projects, using the built-in FindArmadillo module is the recommended approach. It's easy to use, integrates seamlessly with CMake, and avoids extra configuration overhead.