kezunlin · 2019年12月05日

ubuntu 16.04上源码编译glog和gflags 编写glog-config.cmake

本文首发于个人博客https://kezunlin.me/post/977f5125/,欢迎阅读!

compile glog and glags on ubuntu 16.04

Series

Guide

version

wget https://github.com/schuhschuh/gflags/archive/v2.2.1.tar.gz
wget https://github.com/google/glog/archive/v0.3.5.tar.gz

gflags

cd gflags
mkdir build
cd build
cmake-gui ..

with options

BUILD_SHARED_LIBS ON 
INSTALL_SHARED_LIBS ON
INSTALL_STATIC_LIBS OFF
CMAKE_CONFIGURATION_TYPES Release
REGISTER_INSTALL_PREFIX OFF

#NAMESPACE google;gflags
NAMESPACE google

compile and install

 
make -j8
sudo make install

gflags-config.cmake

comes from caffe/cmake/Modules/FindGFlags.cmake

# - Try to find GFLAGS
#
# The following variables are optionally searched for defaults
#  GFLAGS_ROOT_DIR:            Base directory where all GFLAGS components are found
#
# The following are set after configuration is done:
#  GFLAGS_FOUND
#  GFLAGS_INCLUDE_DIRS
#  GFLAGS_LIBRARIES
#  GFLAGS_LIBRARYRARY_DIRS

include(FindPackageHandleStandardArgs)

set(GFLAGS_ROOT_DIR "" CACHE PATH "Folder contains Gflags")

# We are testing only a couple of files in the include directories
if(WIN32)
    find_path(GFLAGS_INCLUDE_DIR gflags/gflags.h
        PATHS ${GFLAGS_ROOT_DIR}/src/windows)
else()
    find_path(GFLAGS_INCLUDE_DIR gflags/gflags.h
        PATHS ${GFLAGS_ROOT_DIR})
endif()

if(MSVC)
    find_library(GFLAGS_LIBRARY_RELEASE
        NAMES libgflags
        PATHS ${GFLAGS_ROOT_DIR}
        PATH_SUFFIXES Release)

    find_library(GFLAGS_LIBRARY_DEBUG
        NAMES libgflags-debug
        PATHS ${GFLAGS_ROOT_DIR}
        PATH_SUFFIXES Debug)

    set(GFLAGS_LIBRARY optimized ${GFLAGS_LIBRARY_RELEASE} debug ${GFLAGS_LIBRARY_DEBUG})
else()
    find_library(GFLAGS_LIBRARY gflags)
endif()

find_package_handle_standard_args(GFlags DEFAULT_MSG GFLAGS_INCLUDE_DIR GFLAGS_LIBRARY)


if(GFLAGS_FOUND)
    set(GFLAGS_INCLUDE_DIRS ${GFLAGS_INCLUDE_DIR})
    set(GFLAGS_LIBRARIES ${GFLAGS_LIBRARY})
    message(STATUS "Found gflags  (include: ${GFLAGS_INCLUDE_DIR}, library: ${GFLAGS_LIBRARY})")
    mark_as_advanced(GFLAGS_LIBRARY_DEBUG GFLAGS_LIBRARY_RELEASE
                     GFLAGS_LIBRARY GFLAGS_INCLUDE_DIR GFLAGS_ROOT_DIR)
endif()

copy gflags-config.cmake to /usr/local/lib/cmake/gflags/

glog

cd glog
mkdir build
cd build
cmake-gui ..

with options

WITH_GFLAGS ON 
CMAKE_CONFIGURATION_TYPES Release

BUILD_SHARED_LIBS ON  # new by hand

compile and install

 
make -j8
sudo make install

glog-config.cmake

comes from caffe/cmake/Modules/FindGFlags.cmake

# - 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
#  GLOG_LIBRARYRARY_DIRS

include(FindPackageHandleStandardArgs)

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

if(WIN32)
    find_path(GLOG_INCLUDE_DIR glog/logging.h
        PATHS ${GLOG_ROOT_DIR}/src/windows)
else()
    find_path(GLOG_INCLUDE_DIR glog/logging.h
        PATHS ${GLOG_ROOT_DIR})
endif()

if(MSVC)
    find_library(GLOG_LIBRARY_RELEASE libglog_static
        PATHS ${GLOG_ROOT_DIR}
        PATH_SUFFIXES Release)

    find_library(GLOG_LIBRARY_DEBUG libglog_static
        PATHS ${GLOG_ROOT_DIR}
        PATH_SUFFIXES Debug)

    set(GLOG_LIBRARY optimized ${GLOG_LIBRARY_RELEASE} debug ${GLOG_LIBRARY_DEBUG})
else()
    find_library(GLOG_LIBRARY glog
        PATHS ${GLOG_ROOT_DIR}
        PATH_SUFFIXES lib lib64)
endif()

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(STATUS "Found glog    (include: ${GLOG_INCLUDE_DIR}, library: ${GLOG_LIBRARY})")
  mark_as_advanced(GLOG_ROOT_DIR GLOG_LIBRARY_RELEASE GLOG_LIBRARY_DEBUG
                                 GLOG_LIBRARY GLOG_INCLUDE_DIR)
endif()

copy glog-config.cmake to /usr/local/lib/cmake/glog/

Example Code

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

project(glog_proj)
 
# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

find_package(GFLAGS REQUIRED) 
include_directories(${GFLAGS_INCLUDE_DIRS})

find_package(GLOG REQUIRED) 
include_directories(${GLOG_INCLUDE_DIRS})

# for windows
#add_definitions( -DGLOG_NO_ABBREVIATED_SEVERITIES ) 

MESSAGE( [Main] " GFLAGS_FOUND = ${GFLAGS_FOUND}")
MESSAGE( [Main] " GFLAGS_INCLUDE_DIRS = ${GFLAGS_INCLUDE_DIRS}")
MESSAGE( [Main] " GFLAGS_LIBRARIES = ${GFLAGS_LIBRARIES}")

MESSAGE( [Main] " GLOG_FOUND = ${GLOG_FOUND}")
MESSAGE( [Main] " GLOG_INCLUDE_DIRS = ${GLOG_INCLUDE_DIRS}")
MESSAGE( [Main] " GLOG_LIBRARIES = ${GLOG_LIBRARIES}")

add_executable(demo glog_main.cpp)
target_link_libraries (demo  ${GLOG_LIBRARIES} ${GFLAGS_LIBRARIES})

main.cpp

#include <gflags/gflags.h>
#include <glog/logging.h>

int main(int argc, char **argv)
{
    /*
    FLAGS_logtostderr = true;  
    FLAGS_alsologtostderr = true;  
    FLAGS_colorlogtostderr = true;  
    FLAGS_log_prefix = true;  
    
    FLAGS_logbufsecs = 0;  //0 means realtime
    FLAGS_max_log_size = 10;  // MB
    */
    google::InitGoogleLogging(argv[0]); // init google logging
    google::SetLogDestination(google::GLOG_FATAL, "../log/log_fatal_"); 
    google::SetLogDestination(google::GLOG_ERROR, "../log/log_error_"); 
    google::SetLogDestination(google::GLOG_WARNING, "../log/log_warning_");
    google::SetLogDestination(google::GLOG_INFO, "../log/log_info_"); 

    LOG(INFO) << "Hello GLOG";

    return 0;
}

Reference

History

  • 20180223: created.

Copyright

推荐阅读
关注数
2
文章数
52
[链接] C++,Python. Computer Vision and Deep Learning.
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息