3

Trying to compile and use OpenFST on Ubuntu 13.10 leads to link errors like "undefined references to dlopen". How does one fix this? Searching online suggests including -ldl in the gcc command line, but that is not sufficient.

Braiam
  • 66,947
  • 30
  • 177
  • 264
Prateek
  • 2,531
  • 2
  • 24
  • 32

2 Answers2

9

Compile as follows:

./configure LDFLAGS=-Wl,--no-as-needed
make
sudo make install

To compile your a.cpp which uses the library, do

g++ -I /usr/local/include a.cpp /usr/local/lib/libfst.so -Wl,--no-as-needed -ldl

It is important the -ldl appears after -Wl,--no-as-needed.

Running your program works as you'd expect from the README provided by OpenFST, you just need to have /usr/local/lib in your LD_LIBRARY_PATH. For example,

LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib" ./a.out
Prateek
  • 2,531
  • 2
  • 24
  • 32
4

As can be seen in the Catalogue of Built-In Rules:

Linking a single object file

n is made automatically from n.o by running the linker (usually called ld) via the C compiler. The precise recipe used is:

$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)

and Variables Used by Implicit Rules:

LDFLAGS

Extra flags to give to compilers when they are supposed to invoke the linker, ld, such as -L. Libraries (-lfoo) should be added to the LDLIBS variable instead.

So in this case -ldl should be set or added to LDLIBS, not LDFLAGS.