Intel Catalina Mac使用Libsodium时出现x86_64未定义符号错误求助
Hey there, let's get this libsodium linker error sorted out for you! The error you're seeing means your code compiles fine (the compiler can find libsodium's header files), but the linker can't locate the actual precompiled function implementations from the libsodium library. Here's how to fix it:
Common Root Cause
You're missing the critical linker flag that tells clang to link against the libsodium library. Unlike system libraries (like libc), third-party libraries like libsodium need to be explicitly referenced during the linking step.
Solution 1: If You Installed libsodium via Homebrew
Homebrew installs libsodium to standard macOS paths (/usr/local/include for headers, /usr/local/lib for libraries), so you just need to add the -lsodium flag to your compile command.
Full Compile Command:
clang sodium-test.c -o sodium-test -lsodium
If you run into issues where clang still can't find the library, explicitly specify the include and library paths to be safe:
clang -I/usr/local/include -L/usr/local/lib sodium-test.c -o sodium-test -lsodium
Solution 2: If You Compiled libsodium Manually
If you built libsodium from source and installed it to a custom path (e.g., ~/libsodium), you need to point clang to both the header files and the compiled library files:
Example Compile Command (adjust paths to match your installation):
clang -I~/libsodium/include -L~/libsodium/lib sodium-test.c -o sodium-test -lsodium
Extra Step for Runtime (if needed):
After compiling, you might hit a "library not found" error when running the binary. Fix this temporarily by telling macOS where to find the libsodium library:
export DYLD_LIBRARY_PATH=~/libsodium/lib ./sodium-test
Or make the path permanent by adding it to a system paths file:
echo "~/libsodium/lib" | sudo tee /etc/paths.d/libsodium
Verify the Fix
Once you use the correct compile command, your code should build and run without linker errors. The sodium_init() and crypto_generichash() symbols will now be resolved from the libsodium library.
内容的提问来源于stack exchange,提问作者Woodstock

