Linking with LAMMPS as a static library [Example/COUPLE/simple]

Dear Lammps group,

I am working on a project that aims to call LAMMPS as a library from another code. Since I am pretty new at this I tried working though examples/COUPLE/simple (Am using version released on 7th August 2019). Now I have run into few problems (I have tried to go through some posts regarding this matter but was of little help - my naivitte on this matter may be issue :)). These are essentially compilation issues

Below are the documentation of the issues I have faced:

when I type g++ -I…/…/…/src -I…/…/…/src/STUBS -c simple.cpp
I get simple.o file

After that using the command g++ simple.o -L…/…/…/src/ -llammps -o simpleCC
gives

Undefined symbols for architecture x86_64:

“_MPI_Abort”, referenced from:

_main in simple.o

“_MPI_Barrier”, referenced from:

_main in simple.o

“_MPI_Bcast”, referenced from:

_main in simple.o

“_MPI_Comm_free”, referenced from:

_main in simple.o

“_MPI_Comm_rank”, referenced from:

_main in simple.o

“_MPI_Comm_size”, referenced from:

_main in simple.o

“_MPI_Comm_split”, referenced from:

_main in simple.o

“_MPI_Finalize”, referenced from:

_main in simple.o

“_MPI_Init”, referenced from:

_main in simple.o

“LAMMPS_NS::LAMMPS::LAMMPS(int, char**, int)”, referenced from:

_main in simple.o

ld: symbol(s) not found for architecture x86_64

I have even tried g++ simple.o -L…/…/…/src/ -llammps -lfftw3 -o simpleCC
and got

Undefined symbols for architecture x86_64:

“_MPI_Abort”, referenced from:

_main in simple.o

“_MPI_Barrier”, referenced from:

_main in simple.o

“_MPI_Bcast”, referenced from:

_main in simple.o

“_MPI_Comm_free”, referenced from:

_main in simple.o

“_MPI_Comm_rank”, referenced from:

_main in simple.o

“_MPI_Comm_size”, referenced from:

_main in simple.o

“_MPI_Comm_split”, referenced from:

_main in simple.o

“_MPI_Finalize”, referenced from:

_main in simple.o

“_MPI_Init”, referenced from:

_main in simple.o

“LAMMPS_NS::LAMMPS::LAMMPS(int, char**, int)”, referenced from:

_main in simple.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Can someone give me some guidelines or direct me to an appropropriate post where (which I may have missed)

Thank you so much for your time and patience.

Deep

Since you are not using MPI compiler wrappers, but the STUBS MPI dummy library and regular compilers, you need to compile and link to this MPU stub library as well.
Thus in the “src” folder you need to do “make mpi-stubs” and then in the examples/COUPLE/simple folder you do:

g++ simple.o -L…/…/…/src/ -L…/…/…/src/STUBS -llammps -lmpi_stubs -o simpleCC

The mpi_stubs library should satisfy the missing symbols beginning with _MPI…

Axel.

Axel,

Thank you so much for taking the time to respond,

Sincerely,

Deep

Sent from a mobile device.

Dear Axel,

This time I have used MPI compiler wrappers and the methodology suggested by you (in the last email). I keep running into problems - please let me describe

First of all, I forgot to mention earlier few items (sincerest apologies for that)

  1. Am using MacBook Pro
  2. And have modified the simple.cpp file (please see attached I changed the path to the header files)

Now, I have been able to make the “simple.o” file using the the two options

1a. g++ -I…/…/…/src -I/opt/local/include/mpich-mp/ -c simple.cpp

2a. g++ -I…/…/…/src -I…/…/…/src/STUBS -c simple.cpp

Then I used the following commands

1b. (After using, make mpi-stubs) g++ -I…/…/…/src -I/opt/local/include/mpich-mp/ -c simple.cpp

2b. g++ simple.o -L…/…/…/src/ -L…/…/…/src/STUBS -llammps -lmpi_stubs -o simpleCC

However, I keep getting the following error

ndefined symbols for architecture x86_64:
“LAMMPS_NS::LAMMPS::LAMMPS(int, char**, int)”, referenced from:
_main in simple.o
“_ompi_mpi_byte”, referenced from:
LAMMPS_NS::Image::merge() in liblammps.a(image.o)
“_ompi_mpi_char”, referenced from:
LAMMPS_NS::Input::file() in liblammps.a(input.o)
LAMMPS_NS::ReadRestart::command(int, char**) in liblammps.a(read_restart.o)
LAMMPS_NS::ReadRestart::magic_string() in liblammps.a(read_restart.o)
LAMMPS_NS::ReadRestart::read_string() in liblammps.a(read_restart.o)
LAMMPS_NS::Comm::ring(int, int, void*, int, void ()(int, char, void*), void*, void*, int) in liblammps.a(comm.o)
LAMMPS_NS::Comm::rendezvous_all2all(int, char*, int, int, int*, int ()(int, char, int&, int*&, char*&, void*), int, char*&, int, void*, int) in liblammps.a(comm.o)
LAMMPS_NS::Comm::read_lines_from_file(__sFILE*, int, int, char*) in liblammps.a(comm.o)

“_ompi_mpi_comm_null”, referenced from:
LAMMPS_NS::ReadDump::ReadDump(LAMMPS_NS::LAMMPS*) in liblammps.a(read_dump.o)
“_ompi_mpi_comm_world”, referenced from:
_lammps_open_no_mpi in liblammps.a(library.o)
“_ompi_mpi_double”, referenced from:
_lammps_gather_atoms in liblammps.a(library.o)
_lammps_gather_atoms_concat in liblammps.a(library.o)
_lammps_gather_atoms_subset in liblammps.a(library.o)
LAMMPS_NS::CreateAtoms::add_lattice() in liblammps.a(create_atoms.o)
LAMMPS_NS::ReadRestart::command(int, char**) in liblammps.a(read_restart.o)
LAMMPS_NS::ReadRestart::header(int) in liblammps.a(read_restart.o)
LAMMPS_NS::ReadRestart::type_arrays() in liblammps.a(read_restart.o)

“_ompi_mpi_double_int”, referenced from:
LAMMPS_NS::ComputeReduce::compute_vector() in liblammps.a(compute_reduce.o)
“_ompi_mpi_int”, referenced from:
LAMMPS_NS::Input::file() in liblammps.a(input.o)
LAMMPS_NS::Input::shell() in liblammps.a(input.o)
LAMMPS_NS::LAMMPS::LAMMPS(int, char**, ompi_communicator_t*) in liblammps.a(lammps.o)
_lammps_gather_atoms in liblammps.a(library.o)
_lammps_gather_atoms_concat in liblammps.a(library.o)
_lammps_gather_atoms_subset in liblammps.a(library.o)
LAMMPS_NS::CreateAtoms::command(int, char**) in liblammps.a(create_atoms.o)

“_ompi_mpi_long_long_int”, referenced from:
LAMMPS_NS::LAMMPS::LAMMPS(int, char**, ompi_communicator_t*) in liblammps.a(lammps.o)
_lammps_create_atoms in liblammps.a(library.o)
LAMMPS_NS::CreateAtoms::command(int, char**) in liblammps.a(create_atoms.o)
LAMMPS_NS::CreateBonds::many() in liblammps.a(create_bonds.o)
LAMMPS_NS::DeleteAtoms::command(int, char**) in liblammps.a(delete_atoms.o)
LAMMPS_NS::DeleteAtoms::recount_topology() in liblammps.a(delete_atoms.o)
LAMMPS_NS::DeleteBonds::command(int, char**) in liblammps.a(delete_bonds.o)

“_ompi_mpi_op_max”, referenced from:
LAMMPS_NS::Input::shell() in liblammps.a(input.o)
LAMMPS_NS::CreateAtoms::command(int, char**) in liblammps.a(create_atoms.o)
LAMMPS_NS::CreateAtoms::add_lattice() in liblammps.a(create_atoms.o)
LAMMPS_NS::WriteRestart::write(char*) in liblammps.a(write_restart.o)
LAMMPS_NS::Min::fnorm_inf() in liblammps.a(min.o)
LAMMPS_NS::atom::tag_check() in liblammps.a(atom.o)
LAMMPS_NS::atom::tag_extend() in liblammps.a(atom.o)

“_ompi_mpi_op_maxloc”, referenced from:
LAMMPS_NS::ComputeReduce::compute_vector() in liblammps.a(compute_reduce.o)
“_ompi_mpi_op_min”, referenced from:
LAMMPS_NS::atom::tag_check() in liblammps.a(atom.o)
LAMMPS_NS::atom::tag_consecutive() in liblammps.a(atom.o)
LAMMPS_NS::Comm::rendezvous_stats(int, int, int, int, int, int, long long) in liblammps.a(comm.o)
LAMMPS_NS::Output::memory_usage() in liblammps.a(output.o)
LAMMPS_NS::Balance::bisection(int) in liblammps.a(balance.o)
LAMMPS_NS::Replicate::command(int, char**) in liblammps.a(replicate.o)
LAMMPS_NS::FixDtReset::end_of_step() in liblammps.a(fix_dt_reset.o)

“_ompi_mpi_op_minloc”, referenced from:
LAMMPS_NS::ComputeReduce::compute_vector() in liblammps.a(compute_reduce.o)
“_ompi_mpi_op_sum”, referenced from:
_lammps_gather_atoms in liblammps.a(library.o)
_lammps_gather_atoms_subset in liblammps.a(library.o)
_lammps_create_atoms in liblammps.a(library.o)
LAMMPS_NS::CreateAtoms::command(int, char**) in liblammps.a(create_atoms.o)
LAMMPS_NS::CreateBonds::many() in liblammps.a(create_bonds.o)
LAMMPS_NS::CreateBonds::single_bond() in liblammps.a(create_bonds.o)
LAMMPS_NS::CreateBonds::single_angle() in liblammps.a(create_bonds.o)

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Once again thank you so much for taking the time respond to these questions. I really appreciate you help.

Thanks,

Deep

simple.cpp (4.8 KB)

image001.jpg

Dear Axel,

This time I have used MPI compiler wrappers and the methodology suggested by you (in the last email). I keep running into problems - please let me describe

First of all, I forgot to mention earlier few items (sincerest apologies for that)

  1. Am using MacBook Pro
  2. And have modified the simple.cpp file (please see attached I changed the path to the header files)

Now, I have been able to make the “simple.o” file using the the two options

When you use the MPI compiler wrapper to build the library (e.g. by using mpicxx through “make mode=lib mpi”), then you MUST also use them to compile and link the coupled executable. the compiler wrapper will automatically add the necessary include and library paths and library names. it can be rather tricky and it is very specific to the specific MPI library and its version that you are using, what the corresponding flag are to be used with g++. any reference to STUBS is definitely wrong in this context and will result in errors as you have seen.

axel.

Axel,

Many many thanks,

Deep

Sent from a mobile device.