You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux下无法从其他模块访问共享库自定义全局函数

Troubleshooting "Undefined Reference" for Custom Function in Libgcrypt

Let’s work through why your custom global function in libgcrypt shows up in nm/objdump but still throws an undefined reference error when linking other modules, plus fixes for each likely issue:

1. Fix Symbol Visibility (Most Common Root Cause)

Libgcrypt uses -fvisibility=hidden by default to restrict which symbols get exported from the shared library. Even if nm labels your function as global, it might still be hidden from dynamic linking unless you explicitly mark it as visible.

  • In your /src/visibility.c, update both the function definition and any header declarations with the visibility attribute:
    __attribute__((visibility("default")))
    void your_custom_function(void) {
        // Your custom logic here
    }
    
  • If other modules use a header to reference this function, add the same attribute to the header declaration too. Without it, the compiler may treat the function as hidden during your module’s linking phase.

Linker order is critical here. When compiling your external module, make sure you specify your custom libgcrypt after your source files, and point to your built library’s directory:

gcc -o your_module your_module.c -L/path/to/your/custom/libgcrypt -lgcrypt

If you place -lgcrypt before your source file, the linker may skip resolving symbols from the library because it hasn’t encountered any references yet.

Also, confirm you’re not accidentally linking against the system’s default libgcrypt. Run ldd your_module to check which libgcrypt.so your binary is using—if it’s the system path, either set LD_LIBRARY_PATH=/path/to/your/custom/libgcrypt temporarily or add -Wl,-rpath=/path/to/your/custom/libgcrypt to your compile command to embed the custom path in the binary.

3. Check Function Declaration Consistency

Ensure the function declaration in your external module exactly matches the definition in visibility.c—this includes return type, parameters, and calling convention. A tiny mismatch (like int vs void return, or missing parameters) can trigger an undefined reference even if the symbol exists in the library.

4. Validate Dynamic Export Status

Run this command to check if your function is actually marked for dynamic export (not just a global symbol in the object file):

objdump -T /path/to/your/custom/libgcrypt.so | grep your_custom_function

The -T flag only shows dynamically exported symbols. If your function doesn’t appear here, it confirms the visibility attribute issue from point 1—nm shows all symbols, but only those marked with visibility("default") get exported for linking.


内容的提问来源于stack exchange,提问作者bholanath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:48:32