Windows下使用redis-plus-plus库出现链接错误的技术求助
Windows下redis++链接已安装库失败问题
环境信息
- 通过vcpkg安装了
redis-plus-plus:x64-windows 1.3.13和hiredis:x64-windows 1.2.0,执行.\vcpkg list可正常查询到 - 头文件路径:
C:\vcpkg\installed\x64-windows\include\hiredis、C:\vcpkg\installed\x64-windows\include\sw\redis - 使用MinGW64编译,CMake版本3.16+
CMakeLists.txt配置
cmake_minimum_required(VERSION 3.16) project(redis_test) set(CMAKE_CXX_STANDARD 17) set(CMAKE_TOOLCHAIN_FILE "C:/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file") find_package(redis++ CONFIG REQUIRED) find_package(hiredis CONFIG REQUIRED) add_executable(redis_sample redis_sample.cpp ) target_link_libraries(redis_sample redis++::redis++ hiredis::hiredis )
测试代码
#include <sw/redis++/redis++.h> #include <iostream> #include <unordered_set> using namespace sw::redis; int main() { auto redis = Redis("tcp://127.0.0.1:6379"); redis.set("key", "val"); }
链接错误信息
编译阶段正常,但执行make -C D:\redis_test/build/时出现链接失败:
E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/redis_sample.dir/objects.a(redis_sample.cpp.obj): in function `main': D:/redis_test/sample/redis_sample.cpp:11:(.text+0xd4): undefined reference to `sw::redis::Redis::set(sw::redis::StringView const&, sw::redis::StringView const&, std::chrono::duration<long long, std::ratio<1ll, 1000ll> > const&, sw::redis::UpdateType)' E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/redis_sample.dir/objects.a(redis_sample.cpp.obj): in function `sw::redis::Redis::Redis(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': C:/vcpkg/installed/x64-windows/include/sw/redis++/redis.h:64:(.text$_ZN2sw5redis5RedisC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN2sw5redis5RedisC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x2e): undefined reference to `sw::redis::Uri::Uri(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/vcpkg/installed/x64-windows/include/sw/redis++/redis.h:64:(.text$_ZN2sw5redis5RedisC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN2sw5redis5RedisC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x41): undefined reference to `sw::redis::Redis::Redis(sw::redis::Uri const&)' collect2.exe: error: ld returned 1 exit status make[2]: *** [sample/CMakeFiles/redis_sample.dir/build.make:120: sample/redis_sample.exe] Error 1 make[2]: Leaving directory 'D:/redis_test/build' make[1]: *** [CMakeFiles/Makefile2:98: sample/CMakeFiles/redis_sample.dir/all] Error 2 make[1]: Leaving directory 'D:/redis_test/build' make: *** [Makefile:91: all] Error 2 make: Leaving directory 'D:/redis_test/build'
自行编译redis++的额外问题
尝试手动编译redis++时,执行D:\redis-plus-plus-master\build>cmake ..出现错误:
Could not find HIREDIS_HEADER using the following files: hiredis
解决方向
工具链与库的ABI匹配问题(核心):vcpkg默认安装的
x64-windows包是针对MSVC编译的,而你使用的是MinGW64,两者C标准库ABI不兼容(MSVC的STL和MinGW的libstdc/libc++无法混用)。需要安装MinGW版本的依赖:- 执行
vcpkg install redis-plus-plus:x64-mingw-static hiredis:x64-mingw-static(若需动态库则替换为x64-mingw-dynamic) - 重新配置CMake时指定目标三元组:
- 方法1:设置环境变量
VCPKG_TARGET_TRIPLET=x64-mingw-static - 方法2:在CMake命令行添加参数
-DVCPKG_TARGET_TRIPLET=x64-mingw-static
- 方法1:设置环境变量
- 执行
CMake配置验证:
- 在CMakeLists.txt中添加调试输出,确认库路径是否正确:
message(STATUS "Found redis++ libraries: ${REDIS++_LIBRARIES}") message(STATUS "Found hiredis libraries: ${HIREDIS_LIBRARIES}") - 若使用动态库版本,需在编译时定义
REDIS_PLUS_PLUS_SHARED宏,可在CMake中添加:target_compile_definitions(redis_sample PRIVATE REDIS_PLUS_PLUS_SHARED)
- 在CMakeLists.txt中添加调试输出,确认库路径是否正确:
手动编译redis++的修正:
- 手动编译时需明确指定hiredis的头文件和库路径,执行cmake命令:
cmake .. -DHIREDIS_HEADER_DIR=C:/vcpkg/installed/x64-mingw-static/include -DHIREDIS_LIB_DIR=C:/vcpkg/installed/x64-mingw-static/lib
(替换路径为你安装的MinGW版本hiredis的实际路径)
- 手动编译时需明确指定hiredis的头文件和库路径,执行cmake命令:
内容的提问来源于stack exchange,提问作者Charlotte
相关产品推荐
相关产品推荐

