Visual Studio下C项目使用hiredis遇方法未定义问题求助
sdsempty() Undefined Reference Error in hiredis C Project Hey there! I’ve run into this exact issue when setting up hiredis for C projects, so let’s get this sorted out quickly. The sdsempty() (and related sds functions) are part of hiredis’s built-in string library—they’re not in hiredis.c itself, which is why you’re seeing the undefined reference error. Here’s how to fix it:
1. Compile with All Required Hiredis Source Files
If you’re compiling manually without installing hiredis system-wide, you can’t just link against hiredis.c alone. You need to include the sds.c file (where sdsempty() and other sds utilities live) in your compile command:
gcc your_client_code.c hiredis.c sds.c -o redis_client
This ensures the compiler picks up all the necessary function definitions from both hiredis and its sds sub-library.
2. Install Hiredis System-Wide (Recommended)
The easiest way to avoid missing dependencies is to install hiredis properly on your system. Here’s how:
- Navigate to the extracted hiredis source directory you downloaded.
- Run these commands:
make sudo make install sudo ldconfig - Now compile your client code by linking against the installed hiredis library:
gcc your_client_code.c -o redis_client -lhiredis
This method handles all dependencies (including sds) automatically, so you won’t have to worry about missing source files.
3. Verify Version Compatibility
Double-check that the hiredis version you downloaded matches the example code you’re using. While sdsempty() is a standard sds function, very old versions might have named it differently (though this is rare). If you’re using an outdated release, grab the latest stable version and try again.
4. Confirm Header File Paths
Make sure your code is including the hiredis headers correctly. If you’re using local source files (not system-installed), add the path to the hiredis headers with the -I flag during compilation:
gcc your_client_code.c hiredis.c sds.c -I./path/to/hiredis -o redis_client
Replace ./path/to/hiredis with the actual directory where hiredis.h and sds.h are located.
Quick Debug Tip
If you’re still stuck, open the sds.h file in your hiredis source—you should see a declaration for sdsempty(). Then check sds.c to confirm the function is implemented there. If either is missing, you might have a corrupted download or incomplete source package.
内容的提问来源于stack exchange,提问作者Irminrics

