Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conflict with gflags already exists #251

Closed
Wsine opened this issue Oct 13, 2017 · 5 comments
Closed

conflict with gflags already exists #251

Wsine opened this issue Oct 13, 2017 · 5 comments

Comments

@Wsine
Copy link

Wsine commented Oct 13, 2017

I have a project which depends on both gflags and glog. I include them in my CMakeLists.txt file as below.

cmake_minimum_required(VERSION 3.0)
project(Test)

add_subdirectory(thirdparty/gflags)
add_subdirectory(thirdparty/glog)

add_executable(Test main.cc)
target_link_libraries(Test pcanbasic gflags glog)

this comes an error:

$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include unistd.h
-- Looking for C++ include unistd.h - found
-- Looking for C++ include stdint.h
-- Looking for C++ include stdint.h - found
-- Looking for C++ include inttypes.h
-- Looking for C++ include inttypes.h - found
-- Looking for C++ include sys/types.h
-- Looking for C++ include sys/types.h - found
-- Looking for C++ include sys/stat.h
-- Looking for C++ include sys/stat.h - found
-- Looking for C++ include fnmatch.h
-- Looking for C++ include fnmatch.h - found
-- Looking for C++ include stddef.h
-- Looking for C++ include stddef.h - found
-- Check size of uint32_t
-- Check size of uint32_t - done
-- Looking for strtoll
-- Looking for strtoll - found
CMake Error at /usr/local/lib/cmake/gflags/gflags-targets.cmake:34 (message):
  Some (but not all) targets in this export set were already defined.

  Targets Defined: gflags_nothreads_static

  Targets not yet defined: gflags_static

Call Stack (most recent call first):
  /usr/local/lib/cmake/gflags/gflags-config.cmake:10 (include)
  thirdparty/glog/CMakeLists.txt:54 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/weizhengyuan/Workspace/CanBusTest/build/CMakeFiles/CMakeOutput.log".

are there any solution?
BR

@Wsine
Copy link
Author

Wsine commented Oct 13, 2017

Should it be a if(NOT TARGET gflags) judgement arround glog/CMakeLists.txt:54?

@sergiud
Copy link
Collaborator

sergiud commented Oct 13, 2017

Take a look at CMake's ExternalProject module. This is the proper way of integrating local dependencies.

@EZ4NO1
Copy link

EZ4NO1 commented Mar 10, 2021

have you solved the problem yet

@Bill0412
Copy link

Bill0412 commented Apr 8, 2021

add_subdirectory(third/gflags)
if(NOT TARGET gflags)
        add_subdirectory(third/glog)
endif()

This solves the problem

@grawert
Copy link

grawert commented Jun 5, 2021

Using ExternalProject solves the problem well. It allows defining dependencies between the projects.

  • Define external projects
# Build gflags as an external project.
set(GFLAGS_INSTALL_DIR ${CMAKE_BINARY_DIR}/libs/gflags)
set(GFLAGS_INCLUDE_DIR ${GFLAGS_INSTALL_DIR}/include)
set(GFLAGS_LIB_DIR ${GFLAGS_INSTALL_DIR}/lib)
ExternalProject_Add(gflags_external_project
    SOURCE_DIR  ${CMAKE_SOURCE_DIR}/libs/gflags
    PREFIX      ${GFLAGS_INSTALL_DIR}
    INSTALL_DIR ${GFLAGS_INSTALL_DIR}
    CMAKE_ARGS  -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
                -DCMAKE_CXX_FLAGS=${EXTERNAL_PROJECT_CMAKE_CXX_FLAGS}
                -DCMAKE_INSTALL_PREFIX:PATH=${GFLAGS_INSTALL_DIR}
                -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
                )
include_directories(BEFORE SYSTEM ${GFLAGS_INCLUDE_DIR})
link_directories(${GFLAGS_LIB_DIR})
# Build glog as an external project.
set(GLOG_INSTALL_DIR ${CMAKE_BINARY_DIR}/libs/glog)
set(GLOG_INCLUDE_DIR ${GLOG_INSTALL_DIR}/include)
set(GLOG_LIB_DIR ${GLOG_INSTALL_DIR}/lib)
ExternalProject_Add(glog_external_project
    SOURCE_DIR  ${CMAKE_SOURCE_DIR}/libs/glog
    PREFIX      ${GLOG_INSTALL_DIR}
    INSTALL_DIR ${GLOG_INSTALL_DIR}
    CMAKE_ARGS  -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
                -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
                -DCMAKE_CXX_FLAGS=${EXTERNAL_PROJECT_CMAKE_CXX_FLAGS}
                -DCMAKE_INSTALL_PREFIX:PATH=${GLOG_INSTALL_DIR}
                -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
    DEPENDS     gflags_external_project
    )
include_directories(BEFORE SYSTEM ${GLOG_INCLUDE_DIR})
link_directories(${GLOG_LIB_DIR})
  • Define the dependencies for your target and link against the libraries
add_dependencies(my_program
    gflags_external_project
    glog_external_project
    )
target_link_libraries(my_program pthread gflags glog)

As taken from the fuchsia/cobalt project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants