0%

compile glog and glags on ubuntu 16.04

Series

Guide

download source

1
2
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

1
2
3
4
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

1
2
make -j8
sudo make install

gflags-config.cmake

comes from caffe/cmake/Modules/FindGFlags.cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# - 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

1
2
3
4
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

1
2
make -j8
sudo make install

glog-config.cmake

comes from caffe/cmake/Modules/FindGFlags.cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# - 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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.