set LD_LIBRARY_PATH from Makefile

为君一笑 提交于 2019-12-04 17:05:25

问题


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.


回答1:


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 $@
    



回答2:


If you don't want to export the LD_LIBRARY_PATH variable within the makefile (e.g. because you have recursive Makefiles which all add to the variable), you can keep it bound to all calls to your compiler and linker.

Either you add it directly to all gcc and ld calls within your target rules, e.g.

my_target: my_target.o
    LD_LIBRARY_PATH=/my/library/path gcc -o my_target my_target.o

or you set the global make variables that define the compilers include the path, e.g.:

 CC=LD_LIBRARY_PATH=/my/library/path gcc
 CPP=LD_LIBRARY_PATH=/my/library/path gcc
 CXX=LD_LIBRARY_PATH=/my/library/path gcc

I chose gcc as compiler but of course you can use any compiler you like.




回答3:


I had the same problem, I had to export LD_LIBRARY_PATH as you did:

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

My friend showed me an alternative when LD_LIBRARY_PATH only applies to one command, notice no semicolon below.

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

This article explains more.




回答4:


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.



来源:https://stackoverflow.com/questions/787684/set-ld-library-path-from-makefile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!