0

I have made a simplest reproducible problem. (ubuntu 20.04 bash)

ckim@ckim-ubuntu:~/testdir/testbashenv$ ls
doit  env.sh  go.sh  hello.c  hello.o  libhello.so  test.c
ckim@ckim-ubuntu:~/testdir/testbashenv$ cat hello.c
#include <stdio.h>
void hello()
{
    printf("Hello world!\n");
}
ckim@ckim-ubuntu:~/testdir/testbashenv$ cat test.c
#include <dlfcn.h>
extern void hello();
void *g_hello_handle;
typedef void (*hello_ptr_t)();
hello_ptr_t hello_ptr;
int main()
{
    g_hello_handle = dlopen("libhello.so", RTLD_LAZY);
    hello_ptr = dlsym(g_hello_handle, "hello");
    hello_ptr();
    return 0;
}
ckim@ckim-ubuntu:~/testdir/testbashenv$ cat env.sh
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${PWD}
ckim@ckim-ubuntu:~/testdir/testbashenv$ source env.sh
ckim@ckim-ubuntu:~/testdir/testbashenv$ echo $LD_LIBRARY_PATH
/usr/local/lib:/usr/local/lib::/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:/home/ckim/testdir/testbashenv
ckim@ckim-ubuntu:~/testdir/testbashenv$ gcc -c -fPIC hello.c -o hello.o
ckim@ckim-ubuntu:~/testdir/testbashenv$ gcc hello.o -shared -o libhello.so
ckim@ckim-ubuntu:~/testdir/testbashenv$ gcc test.c -ldl -lhello -o test
/usr/bin/ld: cannot find -lhello
collect2: error: ld returned 1 exit status
ckim@ckim-ubuntu:~/testdir/testbashenv$ export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib::/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:/home/ckim/testdir/testbashenv
ckim@ckim-ubuntu:~/testdir/testbashenv$ gcc test.c -ldl -lhello -o test
/usr/bin/ld: cannot find -lhello
collect2: error: ld returned 1 exit status
ckim@ckim-ubuntu:~/testdir/testbashenv$ gcc test.c -ldl -L/home/ckim/testdir/testbashenv -lhello -o test
ckim@ckim-ubuntu:~/testdir/testbashenv$ ./test
Hello world!

Why doesn't setting LD_LIBRARY_PATH have any effect??

ADD : There was no problem in the LD_LIBRARY_PATH setting, only in the dos-unix file difference.

Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • 1
    Ah.. this morning the build was not the problem, only the dynamic linking problem during the run time.(cannot find the .so file), so I made this question, but now I realize it became a different problem and your link seems to be the answer here. I'm so confused today.. – Chan Kim May 12 '21 at 05:26
  • So what is the actual question? – Lorinczy Zsigmond May 12 '21 at 07:27
  • @LorinczyZsigmond when you see my log there, setting LD_LIBRARY_PATH by the SourceMe.sh didn't have any effect. But if I set it in the shell, it worked. "Why doesn't it work when using the script?" was my question. Actually I had to set LIBRARY_PATH because that's the value used in the linking process(like -L option). Now, still it's strange the linking problem(during build) is gone if I set LD_LIBRARY_PATH in the shell. – Chan Kim May 12 '21 at 07:40
  • There is not `SourceMe.sh` in the question. Also `-lhello` is not needed linkage-time at all, but if still want to use it, you can use option `-L.` – Lorinczy Zsigmond May 12 '21 at 08:28
  • 1
    yes, correct. -lhello is for static linking during build. (my confusion). SourceMe.sh was the actual file used and it was from windows file so it had \r\n at the end of `export LD_LIBRARY_PATH` statement so it prevented ldopen from finding the .so file when the file really existed in one of the directories in the LD_LIBRARY_PATH(the last directory name had '\r' attached) . That was the first problem for me today.(I used dos2unix SourceMe.sh and the problem was solved). – Chan Kim May 12 '21 at 09:19

0 Answers0