Jeremy Chong
Jeremy Chong

Reputation: 41

Combining two different CMakefiles (gRPC and libsinsp) into a same program

I need to create a consolidated program with Libsinsp and gRPC.

How the program works?

  1. Collects the syscall data with Libsinsp
  2. Transfer the data with gRPC

I have created both programs, and would like to consolidate them into a single program.

I have a problem combining the two CMakeLists.txt into a consolidated file and it prompts alot of error messages while compiling.

Would anyone be able to give me advice?

CMakeLists.txt file for collecting syscall data

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()

CMakeLists.txt file for collecting gRPC

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()

Combined CMakefileLists.txt file

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})

I tried compiling using the combined CMakefile. But it didn't work.

Snippet of error

/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

Upvotes: 1

Views: 75

Answers (1)

Esun Kim
Esun Kim

Reputation: 219

From the error messages, I guess this is because the application tries to pull both BoringSSL and OpenSSL, which is not allowed as they define the same symbols. You may want to try to build it with cmake option, -DgRPC_SSL_PROVIDER=package making gRPC to use OpenSSL instead of BoringSSL, which might be helpful.

Upvotes: 0

Related Questions