描述问题
标题编译过程没有出问题,当项目连接生成的protobuf库出的问题
报错的原因来判断原因是因为protobuf3里面用到的多线程的库pthread 尝试在target
link上面增加-lpthread,编译还是一样的问题
编译错误图
问题是因为没有link pthread库造成的错误
在cmakelist配置文件中追加
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(main Threads::Threads)
理论上来说 FindProtobuf 应该也要负责把链接 pthread这个机制处理好,而不是交由使用者去另外连接pthread。
让项目连接上thread库
解决方案和分析
应该是在连接protobuf库时出现问题
target_link_libraries(${PROJECT_NAME} Protocol_Vehicle Protocol_Base ${Protobuf_LIBRARY})
只连接了protobuf本身,而没有完整的把其他需要的东西引入,将其改成target_link_libraries(${PROJECT_NAME} Protocol_Vehicle Protocol_Base ${Protobuf_LIBRARIES})
就把其他的库引入了,
官方文档写了:
https://cmake.org/cmake/help/v3.14/module/FindProtobuf.html
PROTOBUF_LIBRARIES — The protobuf libraries
连接的是protobuf和其依赖的库
PROTOBUF_LIBRARY — The protobuf library
连接的是单纯的protobuf的库
把CMakeList中的library改成libraries就行了