5

我想用 CMake 构建Google glog作为更大项目的一部分(解决方案,用 Visual Studio 的话来说)。我想要的结果是:

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

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

Debug将在配置中构建解决方案并将文件安装到xxx文件夹。

好的,glog是主要解决方案的子项目:

add_subdirectory(third_party/glog_0.3.4)

在 Windows 上一切正常(参见 CMakeLists.txt):一切正常。

要在 Linux 上构建glog,我还需要配置.h.in文件(以及其他工作)。CMakeconfigure_file不起作用:我有.h文件,但它们#undef仅包含 's。但是glog'./configure工作正常,所以我发现ExternalProject_Add()可能会有所帮助:

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()

并且cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX:PATH=xxx .工作正常,但cmake --build . --target install --config Debug会给我:

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

如果我调用cmake --build . --config Debug,那么它将构建glog 并将其安装到/usr/local/lib. 下次尝试:

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()

不会将文件安装到xxx,只是将其构建到glog-prefix/src/glog-build/.

好的,我不知道如何使它工作..以及如何

  1. 指定安装目录
  2. lib 构建类型(静态/共享)
  3. 配置类型(调试/发布) - 不确定现在是否有效

在 Windows 上,根据 glog 的文档,对于第二种情况,我接下来要做:

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

谢谢你的帮助

4

2 回答 2

1

我将通过示例向您展示,以下是我的项目结构:

在此处输入图像描述

FindGLog.cmake目录中的文件cmake用于查找glog,其内容:

# - 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()

主要CMakeLists.txt使用上面FindGLog.cmake找到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}
        )

引用自:https ://davidstutz.de/running-google-glog-on-travis-ci/

于 2018-07-24T02:25:25.727 回答
0

如今(大概这将在 glog 版本 0.3.5 中),glog包含一个CMakeLists.txt,因此不再需要 ExternalProject。

于 2017-05-04T10:04:52.863 回答