MacOS Ventura13.4下如何用CMake安装rapidjson C++库?
在MacOS Ventura 13.4上通过CMake安装rapidjson的解决方案
你遇到的编译错误是因为MacOS的Clang编译器默认将sprintf标记为废弃函数,而rapidjson的CMake配置默认开启了-Werror(把警告当作错误处理),导致示例代码里的sprintf触发编译失败。以下是两种解决办法,以及完整的CMake安装流程:
方法一:跳过编译示例项目(推荐)
安装rapidjson核心库时,示例代码并非必需,直接禁用示例编译即可:
git clone https://github.com/Tencent/rapidjson.git git submodule update --init cd rapidjson mkdir build && cd build # 禁用示例编译 cmake .. -DRAPIDJSON_BUILD_EXAMPLES=OFF make # 安装到系统目录(需要sudo权限) sudo make install
方法二:忽略废弃函数警告
如果确实需要编译示例,可以通过CMake参数关闭deprecated-declarations的警告错误:
git clone https://github.com/Tencent/rapidjson.git git submodule update --init cd rapidjson mkdir build && cd build # 添加编译选项忽略废弃函数警告 cmake .. -DCMAKE_CXX_FLAGS="-Wno-deprecated-declarations" make sudo make install
安装后在自己的CMake项目中使用
安装完成后,在你的项目CMakeLists.txt中可以这样引入rapidjson:
find_package(RapidJSON REQUIRED) include_directories(${RAPIDJSON_INCLUDE_DIRS}) add_executable(your_app main.cpp)
内容的提问来源于stack exchange,提问作者mascai
相关产品推荐
相关产品推荐

