Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Google glog with CMake on Linux

I want to build Google glog with CMake as part of bigger project (solution, in words of Visual Studio). What I want to have as a result:

cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug
   -DCMAKE_INSTALL_PREFIX:PATH=xxx {MAIN CMakeLists.txt location}

cmake --build . --target install --config Debug

will build solution in Debug configuration and install files to xxx folder.

Ok, glog is sub project of main solution:

add_subdirectory(third_party/glog_0.3.4)

On Windows everything is ok (see CMakeLists.txt): everything works as expected.

To build glog on Linux, I need to configure .h.in files too (among other work). CMake configure_file does not works: I have .h files but they contain #undef's only. But glog's ./configure works fine, so I found that ExternalProject_Add() may help:

if(UNIX)
include(ExternalProject)

ExternalProject_Add(glog
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/configure
    CMAKE_GENERATOR 'Unix Makefiles'
    BUILD_COMMAND ${MAKE})
endif()

And cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX:PATH=xxx . works fine, but cmake --build . --target install --config Debug will give me:

make: *** No rule to make target 'install'.  Stop.

If I invoke cmake --build . --config Debug, then it will build and install glog to /usr/local/lib. Next try:

if(UNIX)
include(ExternalProject)

get_filename_component(glog_absolute_install_dir ${CMAKE_INSTALL_PREFIX} ABSOLUTE)

ExternalProject_Add(glog
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/configure --prefix=${glog_absolute_install_dir}
    CMAKE_GENERATOR 'Unix Makefiles'
    BUILD_COMMAND ${MAKE}
    INSTALL_DIR ${glog_absolute_install_dir}
    INSTALL_COMMAND "${MAKE}")
endif()

will not install files to xxx and just build it to glog-prefix/src/glog-build/.

Ok, I have no idea how to make it work.. And how to

  1. specify install dir
  2. lib build type (static/shared)
  3. configure type (Debug/Release) - not sure that now it works

On Windows, according to glog's documentation, for 2nd case I do next:

add_library(${lib_name} ${lib_type} ${src_files})
if(build_shared_lib)
    add_definitions(-DLIBGLOG_EXPORTS)
else()
    add_definitions(-DGOOGLE_GLOG_DLL_DECL=)
endif()

Thanks for any help

like image 372
grisha Avatar asked Nov 10 '22 14:11

grisha


1 Answers

I will show you by example, the below is my project structure:

enter image description here

The file FindGLog.cmake in the directory cmake is used to find glog, it contents :

# - Try to find Glog
#
# The following variables are optionally searched for defaults
#  GLOG_ROOT_DIR:            Base directory where all GLOG components are found
#
# The following are set after configuration is done:
#  GLOG_FOUND
#  GLOG_INCLUDE_DIRS
#  GLOG_LIBRARIES

include(FindPackageHandleStandardArgs)

if (NOT DEFINED GLOG_ROOT)
    message("set GLOG_ROOT========================")
    set (GLOG_ROOT /usr /usr/local /usr/include/)
endif (NOT DEFINED GLOG_ROOT)

#set(GLOG_ROOT_DIR "" CACHE PATH "Folder contains Google glog")

find_path(GLOG_INCLUDE_DIR glog/logging.h
        PATHS
        ${GLOG_ROOT_DIR}
        PATH_SUFFIXES
        src)

find_library(GLOG_LIBRARY glog libglog
        PATHS
        ${GLOG_ROOT_DIR}
        PATH_SUFFIXES
        .libs
        lib
        lib64)

find_package_handle_standard_args(GLOG DEFAULT_MSG
        GLOG_INCLUDE_DIR GLOG_LIBRARY)

if(GLOG_FOUND)
    set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
    set(GLOG_LIBRARIES ${GLOG_LIBRARY})
    message("GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIRS}===========")
    message("GLOG_LIBRARY ${GLOG_LIBRARY}===========")
endif()

The main CMakeLists.txt use the above FindGLog.cmake to find glog:

cmake_minimum_required(VERSION 3.5)
project(my_caffe)

set(CMAKE_CXX_STANDARD 11)

# find glog
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(GLog REQUIRED)

set(SOURCE_FILES main.cpp)
add_executable(my_caffe_test ${SOURCE_FILES})

# link glog
target_link_libraries(my_caffe_test
        ${GLOG_LIBRARIES}
        )

cited from:https://davidstutz.de/running-google-glog-on-travis-ci/

like image 116
Jayhello Avatar answered Nov 15 '22 06:11

Jayhello