Makefile make install not executing multiple line commands

6,823

make is almost certainly executing both lines of commands. However, the line export LD_LIBRARY_PATH=$< (or whatever make expands it) gets executed by a shell that's a child process of the make process. When you type echo $LD_LIBRARY_PATH, you get whatever the environment variable LD_LIBRARY_PATH had in it. Your shell didn't execute the "export", a sub-process of make executed it. You'll have to do the export in your .bashrc or .zshrc or whatever file, or type it in manually.

Share:
6,823

Related videos on Youtube

tonga
Author by

tonga

Updated on September 18, 2022

Comments

  • tonga
    tonga almost 2 years

    I have a Makefile that contains an install section to allow me to make install after building the source code. The install section contains two lines of shell commands:

    install: /usr/local/lib
        cp $(LIB_OBJ) $<
        export LD_LIBRARY_PATH=$< 
    

    So the first line copies the libxxx.so to /usr/local/lib and the second line sets the environmental variable LD_LIBRARY_PATH to /usr/local/lib so that final executable can find the libxxx.so file.

    But when I run make install from the command line and then type:

    echo $LD_LIBRARY_PATH
    

    It shows empty content rather than /usr/local/lib as intended. So why is the second line of command in the Makefile not executed?

  • tonga
    tonga over 9 years
    Thanks. So there is no way to put export LD_LIBRARY_PATH= in Makefile so that when user type make install, all libraries built will be in the path? If I export it in .bashrc, it will be permanently set. So I don't want to do that. If I need to manually export it, then make install is not automatic.
  • Admin
    Admin over 9 years
    There might be some weird trick. After all you can re-TTY a process with "reptyr" (github.com/nelhage/reptyr). There's probably some gdb stunt you could do to change LD_LIBRARY_PATH, but I don't think you should do those stunts on a regular basis.
  • tonga
    tonga over 9 years
    I put the export command before install but it didn't work. I even put the two commands in one line but not working either.
  • tonga
    tonga over 9 years
    So what is the rationale that the multiline command is executed by a sub-process of make, instead of by the make process itself? Is this designed intentionally in make utility?
  • eyoung100
    eyoung100 over 9 years
    Are you "piping" LIB_OBJ into LD_LIBRARY_PATH? I've never seen $< as in your OP.
  • tonga
    tonga over 9 years
    No, $< is just an automatic variable used by Makefile to denote the first argument in the rule, which is /usr/local/lib.
  • Admin
    Admin over 9 years
    I don't think the distinction is multiline vs single line, I think it's if the command(s) contain a shell metacharacter (>, &, ;, etc) then make shells out rather than just forking and execing. The rationale here is probably, "it's really hard to parse shell command lines correctly, let's just hand all the hard work off to the shell."