合并Libsinsp与gRPC的CMakeLists.txt编译报错求助
整合Libsinsp与gRPC程序的编译冲突问题
我需要开发一个整合Libsinsp与gRPC的程序,工作流程如下:
- 通过Libsinsp收集系统调用数据
- 利用gRPC传输数据
已分别完成两个独立程序的开发,现希望将它们整合为单个程序,但合并二者的CMakeLists.txt后编译时出现大量错误。
1. 收集系统调用数据的CMakeLists.txt
include_directories("../../../common") include_directories("../../") add_executable(sinsp-example util.cpp test.cpp ) target_link_libraries(sinsp-example sinsp ) if (APPLE AND NOT MINIMAL_BUILD) # Needed when linking libcurl set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -framework Foundation -framework SystemConfiguration") endif()
2. gRPC程序的CMakeLists.txt
cmake_minimum_required(VERSION 3.5.1) project(HelloWorld C CXX) include(common.cmake) # Proto file get_filename_component(hw_proto "helloworld.proto" ABSOLUTE) get_filename_component(hw_proto_path "${hw_proto}" PATH) # Generated sources set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc") set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h") set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc") set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h") add_custom_command( OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}" COMMAND ${_PROTOBUF_PROTOC} ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}" --cpp_out "${CMAKE_CURRENT_BINARY_DIR}" -I "${hw_proto_path}" --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}" "${hw_proto}" DEPENDS "${hw_proto}") # Include generated *.pb.h files include_directories("${CMAKE_CURRENT_BINARY_DIR}") # hw_grpc_proto add_library(hw_grpc_proto ${hw_grpc_srcs} ${hw_grpc_hdrs} ${hw_proto_srcs} ${hw_proto_hdrs}) target_link_libraries(hw_grpc_proto ${_REFLECTION} ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF}) # Targets greeter_[async_](client|server) foreach(_target greeter_async_client2) add_executable(${_target} "${_target}.cc") target_link_libraries(${_target} hw_grpc_proto ${_REFLECTION} ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF}) endforeach()
3. 合并后的CMakeLists.txt
cmake_minimum_required(VERSION 3.5.1) project(HelloWorld C CXX) include_directories("../../../common") include_directories("../../") include(common.cmake) # Proto file get_filename_component(hw_proto "helloworld.proto" ABSOLUTE) get_filename_component(hw_proto_path "${hw_proto}" PATH) # Generated sources set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc") set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h") set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc") set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h") add_custom_command( OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}" COMMAND ${_PROTOBUF_PROTOC} ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}" --cpp_out "${CMAKE_CURRENT_BINARY_DIR}" -I "${hw_proto_path}" --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}" "${hw_proto}" DEPENDS "${hw_proto}") # Include generated *.pb.h files include_directories("${CMAKE_CURRENT_BINARY_DIR}") # hw_grpc_proto add_library(hw_grpc_proto ${hw_grpc_srcs} ${hw_grpc_hdrs} ${hw_proto_srcs} ${hw_proto_hdrs}) target_link_libraries(hw_grpc_proto ${_REFLECTION} ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF}) add_executable(sinsp-example util.cpp test.cpp ) target_link_libraries(sinsp-example sinsp hw_grpc_proto ${_REFLECTION} ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF})
编译报错片段
/home/jeremy/grpc/third_party/boringssl-with-bazel/linux-x86_64/crypto/fipsmodule/x86_64-mont5.S:2969: multiple definition of `bn_sqrx8x_internal'; ../../openssl-prefix/src/openssl/target/lib/libcrypto.a(x86_64-mont5.o):(.text+0x2420): first defined here /usr/bin/ld: /home/jeremy/.local/lib/libcrypto.a(x86_64-mont5.S.o): in function `bn_scatter5': /home/jeremy/grpc/third_party/boringssl-with-bazel/linux-x86_64/crypto/fipsmodule/x86_64-mont5.S:3601: multiple definition of `bn_scatter5'; ../../openssl-prefix/src/openssl/target/lib/libcrypto.a(x86_64-mont5.o):(.text+0x2e40): first defined here /usr/bin/ld: /home/jeremy/.local/lib/libcrypto.a(x86_64-mont5.S.o): in function `bn_gather5': /home/jeremy/grpc/third_party/boringssl-with-bazel/linux-x86_64/crypto/fipsmodule/x86_64-mont5.S:3610: multiple definition of `bn_gather5'; ../../openssl-prefix/src/openssl/target/lib/libcrypto.a(x86_64-mont5.o):(.text+0x2e80): first defined here /usr/bin/ld: /home/jeremy/.local/lib/libcrypto.a(engine.c.o): in function `ENGINE_new': engine.c:(.text+0x37): multiple definition of `ENGINE_new'; ../../openssl-prefix/src/openssl/target/lib/libcrypto.a(eng_lib.o):eng_lib.c:(.text+0x60): first defined here /usr/bin/ld: /home/jeremy/.local/lib/libcrypto.a(engine.c.o): in function `ENGINE_free': engine.c:(.text+0x7b): multiple definition of `ENGINE_free'; ../../openssl-prefix/src/openssl/target/lib/libcrypto.a(eng_lib.o):eng_lib.c:(.text+0x250): first defined here
解决建议
- 统一SSL库依赖:gRPC默认自带BoringSSL,而Libsinsp可能依赖系统版本的OpenSSL,导致链接时符号重复。可以二选一统一依赖:
- 编译gRPC时添加CMake参数
-DgRPC_USE_SYSTEM_OPENSSL=ON,让gRPC使用系统OpenSSL,重新编译gRPC后再合并项目。 - 修改Libsinsp的CMake配置,将其依赖的OpenSSL路径指向gRPC自带的BoringSSL目录,强制Libsinsp使用同一套SSL库。
- 编译gRPC时添加CMake参数
- 调整链接顺序:在
target_link_libraries(sinsp-example ...)中,将sinsp放在gRPC相关库之前,尝试让链接器优先使用Libsinsp依赖的符号(仅作为临时尝试,不保证解决根本问题)。 - 禁用不必要的SSL功能:检查Libsinsp的编译选项,如果有禁用SSL相关功能的开关,关闭该功能以避免引入OpenSSL依赖。
- 清理缓存重新构建:删除项目的
CMakeCache.txt和整个build目录,重新运行CMake配置,确保没有残留的旧依赖路径干扰编译。 - 临时规避(不推荐):添加链接选项
-Wl,--allow-multiple-definition强制忽略重复符号,但可能导致运行时未知问题,仅用于临时验证功能。
内容的提问来源于stack exchange,提问作者Jeremy Chong
相关产品推荐
相关产品推荐

