0

I'm testing a small program my colleague wrote. To slightly improve the program setup, I fixed a line but it doesn't work. Here is the phenomenon. In directory ~/vect_run, there are two scripts go.sh and SourceMe.sh (I very much simplified it for this post).

go.sh:

#!/bin/bash
cd run
source ../SourceMe.sh
baremetal

SourceMe.sh:

#!/bin/bash
  export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${PWD%/*}
  echo "LD_LIBRARY_PATH="$LD_LIBRARY_PATH

The baremetal program is in ~/vect_add/run directory and during the run, it needs test.so file which is in ~/vect_add directory. The go.sh sources SourceMe.sh to set the environment LD_LIBRARY_PATH and it calls the program baremetal. By the echo command in the SourceMe.sh I can see the LD_LIBRARY_PATH is set correct (in contains ~/vect_add directory). But the baremetal program complains it can't find test.so. Even at this failed stage, if I echo $LD_LIBRARY_PATH it is correctly set. If I explicitly do export LD_LIBRARY=... (with the same value), baremetal program runs ok. I tried LD_LIBRARY_PATH=... without export. What is wrong in the script??
Here is how it looks (simplified for this post)

ckim@ckim-ubuntu:~/vect_add$ go.sh
LD_LIBRARY_PATH=/usr/local/lib:/home/ckim/vect_add
Load test.so Failed
    test.so: cannot open shared object file: No such file or directory
ckim@ckim-ubuntu:export LD_LIBRARY_PATH=/usr/local/lib:/home/ckim/vect_add
Found test.so.  using dynamic library test.so..
Program ran ok!

ADD 1 : Since exporting a variable is in effect in subshells, I tried removing the export in LD_LIBRARY_PATH. Didn't work.
ADD 2 : Also tried removing #!/bin/bash in the SourceMe.sh (because it creates a sub-shell, and I want the variable to set set in the current shell). Bit didn't work either.
ADD 3 : I tried sourcing SourceMe.sh separately and running baremetal under run. I didn't work either.

ADD 4

: I have made a new simplest reproducible question, please see : setting LD_LIBRARY_PATH doesn't have any effect in bash, why?

Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • 2
    Check your files for DOS style carriage returns. Can you also make sure to copy-paste exactly from your terminal (and not simplify the output between running and posting) since [even tiny changes in punctuation](https://meta.stackoverflow.com/questions/359146/why-should-i-post-complete-errors-why-isnt-the-message-itself-enough) can give vital hints? – that other guy May 12 '21 at 03:19
  • If your files both in directory `~/vect_run`, why are there two leadings dots in `../SourceMe.sh`? – Cyrus May 12 '21 at 04:22
  • @Cyrus because I'm running it under `run` directory. – Chan Kim May 12 '21 at 04:37
  • I have made a new simplest reproducible question, please see : https://stackoverflow.com/questions/67497408/setting-ld-library-path-doesnt-have-any-effect-in-bash-why – Chan Kim May 12 '21 at 04:37
  • 1
    @thatotherguy Hi, I checked you comment again. The SourceMe.sh was a DOS file! So doing `dos2unix SourceMe.sh` solved the problem. Thanks! (If you write a simple answer-check SourceMe.sh file-, I'll choose you answer.) I think people will experience this case sometimes, so it's worth leaving this question alive. – Chan Kim May 12 '21 at 07:41

0 Answers0