Undefined reference when reproducing examples/COUPLE/simple

Hello all,

I’ve searched high and low on this mailing list but haven’t found anyone who has posted about this specific issue before.

I’m trying to follow the example provided in examples/COUPLE/simple about how to use LAMMPS within another piece of C++ code, and am following the instructions in the readme. I’ve compiled (replacing Steve Plimpton’s directories with my own) as follows:

g++ -I/home/easun/lammps-30Jul16/src -I/usr/include/mpich -c simple.cpp # no error

g++ -I/home/easun/lammps-30Jul16/src -I/usr/include/mpich -L/home/easun/lammps-30Jul16/src simple.o -llammps -lfftw -lmpich -lmpl -lpthread -o simpleCC
simple.o: In function main': simple.cpp:(.text+0x197): undefined reference to LAMMPS_NS::LAMMPS::LAMMPS(int, char**, int)’
collect2: error: ld returned 1 exit status

I’m unclear as to why the reference is undefined, since I built LAMMPS as a library following the instructions in the readme in examples/COUPLE two different ways.

make mode=lib mpi
make mode=shlib mpi

Both of these commands seemed to work properly, but subsequent attempts to use the library (as I described above) failed.

I made sure to do a make clean-mpi in between every trial. I’ve also tried using mpicxx and mpic++ to no avail, as well as including src/Obj_mpi and src/Obj_shared_mpi (and all combinations thereof!).

Hope to hear from you if you have any insights about this.

Thanks very much for your time,
Easun Arunachalam

Have you modified simple.cpp in any way? I created a small shell script build.sh that you can run from the lammps-30Jul16/examples/COUPLE/simple directory, and it works in my case.

https://gist.github.com/andeplane/18bf333752363fa3450423e3b10cf66e

Can you try to run it (of course look at the script first so you know it doesn’t do anything bad)?

Anders

Hello all,

I've searched high and low on this mailing list but haven't found anyone who
has posted about this specific issue before.

I'm trying to follow the example provided in examples/COUPLE/simple about
how to use LAMMPS within another piece of C++ code, and am following the
instructions in the readme. I've compiled (replacing Steve Plimpton's
directories with my own) as follows:

g++ -I/home/easun/lammps-30Jul16/src -I/usr/include/mpich -c simple.cpp
# no error
g++ -I/home/easun/lammps-30Jul16/src -I/usr/include/mpich
-L/home/easun/lammps-30Jul16/src simple.o -llammps -lfftw -lmpich -lmpl
-lpthread -o simpleCC
simple.o: In function `main':
simple.cpp:(.text+0x197): undefined reference to
`LAMMPS_NS::LAMMPS::LAMMPS(int, char**, int)'
collect2: error: ld returned 1 exit status

I'm unclear as to why the reference is undefined, since I built LAMMPS as a
library following the instructions in the readme in examples/COUPLE two
different ways.

make mode=lib mpi
make mode=shlib mpi

Both of these commands seemed to work properly, but subsequent attempts to
use the library (as I described above) failed.

I made sure to do a make clean-mpi in between every trial. I've also tried
using mpicxx and mpic++ to no avail, as well as including src/Obj_mpi and
src/Obj_shared_mpi (and all combinations thereof!).

possibly your mpicxx and mpic++ belong to OpenMPI, while you are using
explicit include paths to MPICH. OpenMPI and MPICH differ in the
storage type of the communicator which would lead to the error you see
(simple.o has been compiled against the MPICH header using a different
data type, so it doesn't match what the library provides).
try only using the MPI compiler wrapper and no explicit paths and libraries:

mpicxx -I/home/easun/lammps-30Jul16/src
-L/home/easun/lammps-30Jul16/src simple.cpp -llammps_mpi -o simpleCC

since you are starting fresh, i would also urge you to download the
latest stable version with many bugfixes and specifically also
enhancements to the library interface.

axel.

Prof. Kohlmeyer,

Thank you for your speedy reply. I believe you are correct about the conflicting mpi implementations on my system. I would like to note, for the benefit of future readers, that I slightly modified the compilation instructions you suggested to avoid the error:

undefined reference to symbol ‘dlclose@@GLIBC_2.2.5
//lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from command line

Here is my working command:
mpicxx -I/home/easun/lammps-30Jul16/src -L/home/easun/lammps-30Jul16/src simple.cpp -llammps_mpi -ldl -o simpleCC

I’d also like to note that I had to compile LAMMPS statically in order to get it working.

Thank you again for your time,
Easun

Hi Anders,

I appreciate your suggestion. As it turned out, however, the issue was conflicting MPI implementations on my system (brought to the fore by my explicit inclusion of mpich’s mpi.h). A more detailed description can be found above in my reply to Prof. Kohlmeyer.

I didn’t modify simple.cpp in any way.

Thanks,
Easun

Prof. Kohlmeyer,

Thank you for your speedy reply. I believe you are correct about the
conflicting mpi implementations on my system. I would like to note, for the
benefit of future readers, that I slightly modified the compilation
instructions you suggested to avoid the error:

undefined reference to symbol 'dlclose@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from
command line

Here is my working command:
mpicxx -I/home/easun/lammps-30Jul16/src -L/home/easun/lammps-30Jul16/src
simple.cpp -llammps_mpi -ldl -o simpleCC

I'd also like to note that I had to compile LAMMPS statically in order to
get it working.

i just checked and compiling against a shared library works just as well.
however, you will have to either link with
-Wl,-rpath,/path/to/liblammps_mpi.so or set LD_LIBRARY_PATH properly
to be able to have the resulting executable find and load the shared
library. this is just how shared vs. static libraries work.

axel.