Can I pull in LAMMPS as an external dependency using CMake?

Hi LAMMPS developers,

Is there any way for me to find_package(LAMMPS) in a CMakeLists.txt and get back the path to the LAMMPS shared library and header files?

Currently, I can run find_package(LAMMPS REQUIRED) without errors (as long as I’ve run make install and the prefix folder is in my $PATH), but it only sets $LAMMPS_FOUND. $LAMMPS_LIBRARY, $LAMMPS_INCLUDE_DIR, etc, are all empty. For example, the following CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(chromatin C CXX Fortran)
find_package(LAMMPS REQUIRED)
    message("Found LAMMPS")

Produces the following output:

-- Found MPI: TRUE (found version "3.1") found components: CXX 

-- Configuring done

I can provide the whole CMakeLists.txt, output, logs, etc, but it’ll have to be in a pastebin as I can’t upload files.


Misc info:

  • OS: Pop!_OS 21.04
  • CMake version: 3.18.4
  • LAMMPS version: current master branch

Importing LAMMPS via CMake is not (yet) an officially supported feature. We need to make some changes to the layout to the core LAMMPS code and define a list of which headers are considered part of the C++ API and which are not and make some modifications that only exposes the intended interface.

It all depends on how you call LAMMPS. If you are using the C library interface you really only need access to lammps/src/library.h and assuming that you compiled LAMMPS as a shared library. This is strongly recommended, as the shared library is linked to all dependent libraries, so you don’t need to list/import them like you need for linking the static LAMMPS library.

Forgot to add a link. If you have not already done so, please check out: 1. LAMMPS Library Interfaces — LAMMPS documentation

In that case I think I’m trying to do the wrong thing. I’ve inherited some code that implements a few new pair and bond styles. Right now it requires dumping files in the LAMMPS source folder and manually appending the CMakeLists file to make them compile. I want to convert it to a LAMMPS plugin, which I assumed I could build outside of the LAMMPS source. Should all plugins be built inside LAMMPS instead, like the example plugins are? Thanks again!

In the first case, if you just copy the files to, say, src/EXTRA-PAIR and src/EXTRA-MOLECULE, respectively, and re-run CMake, this should include the files in the build, when you include those two packages.

Second, if you want to build your code as plugin, you can start from the CMakeLists.txt and the LAMMPSInterfaceCXX.cmake files in the examples/plugins folder and modify the first file to include your files as one or more plugin objects.

For as long as you are not compiling on Windows (but in that case you would need to use the latest development tree snapshot anyway), this can be compiled from anywhere with:

cmake -S . -B build -DLAMMPS_HEADER_DIR=<path/to>/lammps/src
cmake --build build

I suggest you just copy the plugins folder from the LAMMPS distribution and give it a try.