quine quine - 1 month ago 6
C++ Question

Simple makefile for LibElemental

I would like to use libElemental as a library after installing it as follows:

git clone https://github.com/elemental/Elemental
mkdir build
cd build
cmake ../Elemental
make
make install


Install is successful & examples build. I have limited
makefile
experience, but would like to avoid using
cmake
so I can easily integrate elsewhere.

I have one file:
test.cpp
:

#include <El.hpp>
#include <stdlib.h>

int main(int argc, char* argv[]) {
return 0;
}


Here is my unsuccessful
makefile
attempt:

CFLAGS = -O3 -std=gnu++11
LDFLAGS := -lEl
CC = mpicc.mpich2
CXX = mpicxx.mpich2

all: test
test: test.cpp
$(CXX) $(CFLAGS) $(LDFLAGS) test.cpp -o test


Here is a snippet of the linker errors I receive:

/tmp/ccgDsmEV.o: In function __static_initialization_and_destruction_0':
/usr/local/include/El/number_theory/lattice/LLL.hpp:316: undefined reference to El::Timer::Timer(std::string const&)'
:
:
/tmp/ccgDsmEV.o:/usr/local/include/El/number_theory/lattice/LLL.hpp:318: more undefined references to El::Timer::Timer(std::string const&)' follow
collect2: error: ld returned 1 exit status
make: *** [test] Error 1


Any help or pointers to help me resolve this is much appreciated!

Answer

Thank you to commenters @andars and @Tsyvarev for pointing out that my error was the placement of $(LDFlAGS). Other than this there just remains the matter of pointing to dynamic libraries installed by libElemental. Here is the final makefile:

CXX=mpicxx.mpich

LIB_PATHS:=-Wl,-rpath,/usr/local/lib/x86_64-linux-gnu:/usr/local/lib
CXXFLAGS:=-std=gnu++11 -O3
LDFLAGS:=-lEl

all: test
test: test.cpp
    $(CXX) $(CXXFLAGS) $(LIB_PATHS) test.cpp -o test $(LDFLAGS)

clean:
    rm -f test

I could also skip adding $(LIB_PATHS) and just set LD_LIBRARY_PATH:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/x86_64-linux-gnu:/usr/local/lib

NOTE: that since libElemental uses mpich instead of openmpi (as seen in the makefile), run executables with mpirun.mpich instead of just mpirun or you'll get undefined behavior.

Comments