0

I copy pasted the code from How would a loaded library function call a symbol in the main application? to help me understand how loaded libraries work. But when I tried to run it, it says it cannot find the file, although the file is right there in the current directory when I do ls

@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ gcc -shared -olibdlo.so dlo.c
    /usr/bin/ld: /tmp/ccpGxAlo.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /tmp/ccpGxAlo.o: error adding symbols: Bad value
    collect2: error: ld returned 1 exit status
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ gcc -shared -fPIC -olibdlo.so dlo.c
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ gcc -ldl -rdynamic main.c
    /tmp/ccHtUgDf.o: In function `main':
    main.c:(.text+0x2b): undefined reference to `dlopen'
    main.c:(.text+0x3b): undefined reference to `dlerror'
    main.c:(.text+0x66): undefined reference to `dlerror'
    main.c:(.text+0x7b): undefined reference to `dlsym'
    main.c:(.text+0x83): undefined reference to `dlerror'
    main.c:(.text+0xc7): undefined reference to `dlclose'
    collect2: error: ld returned 1 exit status
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ gcc -Wl,--no-as-needed -ldl -rdynamic main.c
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ ls
    AI_A.c  AI_B.c  AI_C.c  a.out  dlo.c  Game.c  Game.h  libdlo.so  main.c  runGame.c
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ ./a.out
    libdlo.so: cannot open shared object file: No such file or directory
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$

libdlo.so: cannot open shared object file: No such file or directory

why couldn't the program find the file libdlo.so? How do I fix this issue? Thanks!

Community
  • 1
  • 1
Friedpanseller
  • 654
  • 3
  • 16
  • 31
  • Your local directory is not one of folders where Linux look for libraries. Move your lib into `/usr/lib` for example or take a look at [this SO answer](http://stackoverflow.com/questions/13428910/how-to-set-the-environmental-variable-ld-library-path-in-linux) – LPs May 02 '17 at 06:30
  • In your dlopen call you need to specify relative path if you want to load the library from current directory. EX: `./libdlo.so` – Ankur May 02 '17 at 06:34
  • Reorder you linking command: `gcc -rdynamic main.c -ldl` – Lorinczy Zsigmond May 02 '17 at 08:39

2 Answers2

2

OS does not look for libraries in your local directory, either

1) Move your lib into /usr/lib

2) In the dlopen call specify the relative path "./libdlo.so"

.

Thanks to lps and ankur for the answers

Friedpanseller
  • 654
  • 3
  • 16
  • 31
0

See my answer in this post. I recently encounted the same probelm and it will definitely help.

After reading that, I recommend you to use the -rpath option.

Just for fun, you can also use ldconfig $(pwd) to solve this, and I think sometimes this method is better.

For more information, please read the linux how-to documentation on library, which tells a lot about static library, shared library and dynamically linked library.

For more and more information, please read Better understanding Linux secondary dependencies solving with examples, which explores deeper in libraries that are linked by dynamic linker, that is , shared library and dynamically linked library.

Han XIAO
  • 1,148
  • 9
  • 20