Sunday, May 1, 2011

set LD_LIBRARY_PATH from Makefile

How do I set the LD_LIBRARY_PATH env variable from a Makefile?

I have some source code that links to a shared library that in turn links to a different shared library (more than 1). The Makefile for building the application only knows about the first shared library.

If I want to build this, I have to specify: #export LD_LIBRARY_PATH=/path/to/the/shared/libs (for bash) and that works fine.

However, I would like to do this from the Makefile itself.

From stackoverflow
  • I had tried adding:

    export LD_LIBRARY_PATH=/path/to/the/shared/libs
    

    which apparently works fine.

    I was getting errors because my /path/to/the/shared/libs was incorrect.

    Would still be good to know what others do for this and/if there is a better way.

  • Yes, "export" is the correct directive to use. It is documented in detail here. This is the same mechanism as make itself uses to propagate variables to sub-makes. The drawback is that you cannot selectively pass down the variable to some commands and not to others.

    There are two other options I can think of:

    • Using .EXPORT_ALL_VARIABLES (specify as a target somewhere), causes all variables to be exported to the environment of sub-commands.
    • Specify on the command line:

      foo: EXPORTEDVAR=somevalue gcc $< -o $@

0 comments:

Post a Comment