【背景】
部门依赖特定框架开发代码,框架中已经集成了protobuf,但是用户却有可能在开发机上胡乱安装protobuf。而业务链接到的版本是框架的protobuf,但cmake中find_package(Protobuf)默认的是用系统protobuf中的protoc编译描述文件,当系统中pb和框架pb版本不同时,就会造成编译错误。
【构思】
研究了下Cmake中的FindProtobuf.cmake文件,_protobuf_find_libraries|_protobuf_find_libraries|_protobuf_find_libraries中查找lib,include,bin均是通过find_path,find_program,find_library来完成的,只要控制查找路径即可。
1 2 3 |
_protobuf_find_libraries(Protobuf protobuf) _protobuf_find_libraries(Protobuf_LITE protobuf-lite) _protobuf_find_libraries(Protobuf_PROTOC protoc) |
于是,框架的xxx-config.cmake中添加一下两行解决问题。业务依赖框架时find_pakcage(xxx)就会自动设置cmake的路径变量,最终达到控制查询结果的目的。
1 2 |
list(APPEND CMAKE_PROGRAM_PATH "${EXTLIB_INSTALL_PREFIX}/bin") list(APPEND CMAKE_FRAMEWORK_PATH "${EXTLIB_INSTALL_PREFIX}/lib" "${EXTLIB_INSTALL_PREFIX}/include") |