Ubuntu 16.0.2下64位共享库dlopen加载失败求助
Alright, let's figure out why your dlopen call is failing even after you've copied the 64-bit shared library to /usr/local/lib and updated the linker cache with ldconfig. I'll walk you through step-by-step troubleshooting tailored to your Ubuntu 16.04.2 system:
dlopen The most critical step is to know why dlopen is failing—right now we're guessing. Add a call to dlerror() in your code to print the detailed error:
#include <dlfcn.h> #include <stdio.h> int main() { void* lib_handle = dlopen("foobar.so", RTLD_LAZY); if (!lib_handle) { fprintf(stderr, "dlopen failed: %s\n", dlerror()); return 1; } // Success! Clean up dlclose(lib_handle); return 0; }
Compile this test program with the dynamic linker library:
gcc -o test_dlopen test_dlopen.c -ldl
Run it and note the error message—this will point us directly to the root cause (e.g., missing dependencies, architecture mismatch, permission issues).
You mentioned compiling 64-bit libraries, but let's confirm everything lines up:
- Check your system's architecture:
It should outputuname -mx86_64for a 64-bit system. - Check the library's architecture:
Look forfile /usr/local/lib/foobar.soELF 64-bit LSB shared object, x86-64in the output. If it says32-bit, you accidentally compiled a 32-bit library, which won't load on a 64-bit system by default.
ldconfig actually added the library to its cache Even if you ran ldconfig, it's worth verifying the library is in the system linker cache:
- Search the cache for your library:
If you don't seeldconfig -p | grep foobarfoobar.soin the results, do these checks:- Ensure
/usr/local/libis listed in one of the.conffiles referenced byld.so.conf:
Most Ubuntu systems include this path incat /etc/ld.so.conf.d/*.conf | grep "/usr/local/lib"/etc/ld.so.conf.d/libc.confby default. - Re-run
ldconfigin verbose mode to see if it processes your library:
Look for a line likesudo ldconfig -vfoobar.so -> foobar.sounder the/usr/local/libsection. If it's missing, double-check the library's permissions (next step).
- Ensure
The linker needs read access to the library:
- Check the library's permissions:
You should see permissions likels -l /usr/local/lib/foobar.so-rwxr-xr-x(read/execute for all users). If not, fix them with:sudo chmod 755 /usr/local/lib/foobar.so - Ensure the file isn't owned by a user/group that restricts access (e.g., a private user account).
root:rootor your user account is fine, as long as permissions allow global read access.
Try using the full path to the library in dlopen to rule out path resolution issues:
dlopen("/usr/local/lib/foobar.so", RTLD_LAZY);
If this works but the relative name fails, check the library's SONAME:
- View the library's SONAME (the name the linker uses to reference it):
If there's no SONAME output, you forgot to set it during compilation. Recompile with the linker flag to set the SONAME:readelf -d /usr/local/lib/foobar.so | grep SONAME
Re-copy the library togcc -shared -fPIC -Wl,-soname,foobar.so -o foobar.so your_source_files.c/usr/local/liband re-runsudo ldconfig—this should make the library visible via its short name.
Sometimes dlopen fails because the library itself depends on other libraries that aren't found. Use ldd to check:
ldd /usr/local/lib/foobar.so
Look for any lines marked not found. If you see these, you'll need to install those missing libraries (via apt) or copy them to a path the linker recognizes (like /usr/local/lib) and run sudo ldconfig again.
Ubuntu 16.04 uses AppArmor by default, which can sometimes block programs from loading libraries in certain paths. Test this by temporarily stopping AppArmor:
sudo systemctl stop apparmor
Run your program again—if it works now, you'll need to update your AppArmor profile to allow loading libraries from /usr/local/lib.
内容的提问来源于stack exchange,提问作者rohitsan

