Skip to content

Building with Cmake Fetch Content Fails when including gRPC #3971

@ctriley

Description

@ctriley

What language and solver does this apply to?
C++ / all

Describe the problem you are trying to solve.
I want to build a super-project that includes absl, gRPC and ortools. However, I'm running into dependency conflicts like:

CMake Error at cmake-build-debug/_deps/absl-src/CMake/AbseilHelpers.cmake:318 (add_library):
add_library cannot create target "atomic_hook" because another target with
the same name already exists. The existing target is an interface library
created in source directory

It looks like a similar issue occurred a few years ago #2219

Here is my cmakelists.txt

cmake_minimum_required(VERSION 3.26)
project(google_example)

set(CMAKE_CXX_STANDARD 17)

SET(ABSL_PROPAGATE_CXX_STD ON)
SET(ABSL_BUILD_TESTING OFF)
set(CMAKE_INCLUDE_CURRENT_DIR OFF)
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(CMAKE_USE_WIN32_THREADS_INIT 0)
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)
include(FetchContent)
set(FETCHCONTENT_QUIET OFF)
set(ABSL_ENABLE_INSTALL ON)
set(gRPC_SSL_PROVIDER package)
FetchContent_Declare(
        gRPC
        GIT_REPOSITORY https://github.com/grpc/grpc
        GIT_TAG        v1.59.2
)
FetchContent_MakeAvailable(gRPC)
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
set(_REFLECTION grpc++_reflection)
set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
set(_GRPC_GRPCPP grpc++)
if(CMAKE_CROSSCOMPILING)
    find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
else()
    set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
endif()

message("Neither Gurobi nor Cplex was found." )
FetchContent_Declare(
        ortools
        GIT_REPOSITORY https://github.com/google/or-tools.git
        GIT_TAG        stable
)
set(BUILD_SHARED_LIBS OFF)
set(BUILD_DEPS ON)
set(BUILD_SAMPLES OFF)
set(BUILD_EXAMPLES OFF)
FetchContent_MakeAvailable(ortools)
add_definitions(-DORTOOLS)
include_directories(${ortools_SOURCE_DIR})
include_directories(${ortools_BINARY_DIR})
include_directories(${absl_SOURCE_DIR})
include_directories(${protobuf_SOURCE_DIR}/../src)

add_executable(google_example main.cpp)
target_link_libraries(google_example absl::string gRPC ortools::ortools)

Describe the solution you'd like
Ideally, before trying to build the required libraries, we would check builddir/_deps/ for existing targets.

Describe alternatives you've considered
I could install the packages outside of the configure/build step, but this isn't as nice as having the configure/build step do everything.

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions