I can not load plugins and don't get why

I would like to write a LAMMPS plugin. To learn how plugins work, I decided to compile the example plugins and load them with a LAMMPS binary compiled on the same system (Fedora Linux 39). However, when I e.g. issue the command plugin load helloplugin.so, I always get the error Open of file helloplugin.so failed: helloplugin.so: cannot open shared object file: No such file or directory. Now, this error seems very self-explanatory, and maybe I’m being stupid, but I am sure that the file is there and that the working directory is correct. If I install LAMMPS from conda-forge, I get the same error.

Here are the steps that reproduce the error for me:

git clone https://github.com/lammps/lammps.git
cd lammps
mkdir build
cd build
cmake ../cmake/ -DPKG_PLUGIN=yes
cmake --build . -j 8
cd ../examples/plugins/
mkdir build
cd build
cmake ..
cmake --build . -j 8
../../../build/lmp
plugin load helloplugin.so
# Open of file helloplugin.so failed: helloplugin.so: cannot open shared object file: No such file or directory

The plugin files are all there though:

(lammps_src) [niklas@nxps build]$ ls -al
total 264
drwxr-xr-x. 1 niklas niklas   234 24. Feb 10:25 ./
drwxr-xr-x. 1 niklas niklas   824 24. Feb 10:23 ../
-rw-r--r--. 1 niklas niklas 18882 24. Feb 10:23 CMakeCache.txt
drwxr-xr-x. 1 niklas niklas   516 24. Feb 10:24 CMakeFiles/
-rw-r--r--. 1 niklas niklas  1665 24. Feb 10:23 cmake_install.cmake
-rwxr-xr-x. 1 niklas niklas 23360 24. Feb 10:24 helloplugin.so*
-rw-r--r--. 1 niklas niklas   369 24. Feb 10:45 log.lammps
-rw-r--r--. 1 niklas niklas 16656 24. Feb 10:23 Makefile
-rwxr-xr-x. 1 niklas niklas 66736 24. Feb 10:24 morse2plugin.so*
-rwxr-xr-x. 1 niklas niklas 45088 24. Feb 10:24 nve2plugin.so*
-rwxr-xr-x. 1 niklas niklas 77408 24. Feb 10:24 zero2plugin.so*

Here is the cmake output for LAMMPS:

(lammps_src) [niklas@nxps build]$ cmake ../cmake/ -DPKG_PLUGIN=yes
-- The CXX compiler identification is GNU 13.2.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Git: /usr/bin/git (found version "2.43.0") 
-- Running check for auto-generated files from make-based build system
-- Checking for module 'mpi-cxx'
--   Package 'mpi-cxx', required by 'virtual:world', not found
-- Looking for C++ include omp.h
-- Looking for C++ include omp.h - found
-- Found OpenMP_CXX: -fopenmp (found version "4.5") 
-- Found OpenMP: TRUE (found version "4.5") found components: CXX 
-- Found GZIP: /usr/bin/gzip  
-- Found FFMPEG: /usr/bin/ffmpeg  
-- Looking for C++ include cmath
-- Looking for C++ include cmath - found
-- Generating style headers...
-- Generating package headers...
-- Generating lmpinstalledpkgs.h...
-- Found Python3: /usr/bin/python3.12 (found version "3.12.1") found components: Interpreter 
-- Could NOT find ClangFormat (missing: ClangFormat_EXECUTABLE) (Required is at least version "8.0")
-- The following tools and libraries have been found and configured:
 * Git
 * OpenMP
 * Python3

-- <<< Build configuration >>>
   LAMMPS Version:   20240207 patch_7Feb2024_update1-89-g6c3c724f9c
   Operating System: Linux Fedora 39
   CMake Version:    3.28.3
   Build type:       RelWithDebInfo
   Install path:     /home/niklas/.local
   Generator:        Unix Makefiles using /usr/bin/gmake
-- Enabled packages: PLUGIN
-- <<< Compilers and Flags: >>>
-- C++ Compiler:     /usr/bin/c++
      Type:          GNU
      Version:       13.2.1
      C++ Flags:     -O2 -g -DNDEBUG
      Defines:       LAMMPS_SMALLBIG;LAMMPS_MEMALIGN=64;LAMMPS_OMP_COMPAT=4;LAMMPS_GZIP;LAMMPS_FFMPEG;LMP_PLUGIN
-- <<< Linker flags: >>>
-- Executable name:  lmp
-- Static library flags:    
-- Configuring done (3.6s)
-- Generating done (0.1s)
-- Build files have been written to: /home/niklas/src/lammps/build

And here is the cmake output for the plugins:

(lammps_src) [niklas@nxps build]$ cmake ..
-- The CXX compiler identification is GNU 13.2.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Checking for module 'mpi-cxx'
--   Package 'mpi-cxx', required by 'virtual:world', not found
-- Looking for C++ include omp.h
-- Looking for C++ include omp.h - found
-- Found OpenMP_CXX: -fopenmp (found version "4.5") 
-- Found OpenMP: TRUE (found version "4.5")  
-- Configuring done (0.7s)
-- Generating done (0.0s)
-- Build files have been written to: /home/niklas/src/lammps/examples/plugins/build

Why can I not load the plugins? Am I missing something obvious?

I just learned here that the operating system must be made aware of shared library files as well as LAMMPS itself. I got closer using export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd) before starting LAMMPS. Now, instead I get the error Open of file helloplugin.so failed: helloplugin.so: undefined symbol: _ZN9LAMMPS_NS5Error3allERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiS8_. Referring to the LAMMPS docs, I guess this can be caused if compilation settings differ between compiling LAMMPS and the plugins. I am still a bit puzzled though, as I used all cmake files as-is, without specifying any custom compilation options.

Please try compiling LAMMPS with the option -DBUILD_SHARED_LIBS=yes and try again.

1 Like

That would not be needed if you use a “full” path, e.g. plugin load ./helloplugin.so.

I’ve made some changes to the CMake script files that should help with this issue when using a static LAMMPS library. You can find the branch here: GitHub - akohlmey/lammps at collected-small-changes

git clone --depth 1 -b collected-small-changes https://github.com/akohlmey/lammps.git
cd lammps
cmake -S cmake -B build -DPKG_PLUGIN=yes
cmake --build build --parallel 8
cmake -S examples/plugins -B build-plugins
cmake --build build-plugins

./build/lmp 
LAMMPS (7 Feb 2024 - Development - e7b0c8b)
  using 1 OpenMP thread(s) per MPI task
plugin load build-plugins/helloplugin.so
Loading plugin: Hello world command v1.1 by Axel Kohlmeyer ([email protected])
hello world
Hello, world!
Total wall time: 0:00:30
1 Like

That’s good to know, thanks! With this and -DBUILD_SHARED_LIBS=yes, I can load helloplugin.so, nve2plugin.so, and zero2plugin.so with LAMMPS built from the develop and release branches, as well as the prebuilt conda-forge package. Just FYI, loading morse2plugin.so still fails with undefined symbols, maybe it uses some uncommon packages?

This is expected. The plugin contains two pair styles, morse2 and morse2/omp. The latter requires functionality from the OPENMP package, so it will only load if OPENMP has been installed when compiling LAMMPS.

1 Like

This also works for me. :+1:

1 Like