Cmake系列:如何使用CMake编译自己项目

举个例子来说,假如想编译自己写的基于OpenCV的程序那么
如果你是使用的是ubuntu的话,就很方便。

如果你使用gflags和glog的话那么执行安装:
sudo apt-get install libgoogle-glog-dev libflags-dev

如果你还使用protobuf那么执行安装
sudo apt-get install protobuf-compiler libprotobuf-dev

如果你使用boost
sudo apt-get install libboost-all-dev

如果你使用opencv
sudo apt-get install libopencv-dev

接下来安装正角:
sudo apt-get install cmake cmake-qt-gui

然后自己写自己的CMakeLists.txt,内容如下:
cmake_minimum_required(VERSION 2.8)
project(Disp)

set(LINKER_LIBS "")

# opencv
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
list(APPEND LINKER_LIBS ${OpenCV_LIBS})

# glog
# 这是从caffe中获取的
include("cmake/FindGlog.cmake")
#find_package(lib_glog REQUIRED)
include_directories(SYSTEM ${GLOG_INCLUDE_DIRS})
list(APPEND LINKER_LIBS ${GLOG_LIBRARIES})

# gflags
# 这是从caffe中获取的
include("cmake/FindGFlags.cmake")
#find_package(GFlags REQUIRED)
include_directories(SYSTEM ${GFLAGS_INCLUDE_DIRS})
list(APPEND LINKER_LIBS ${GFLAGS_LIBRARIES})

# 加入你自己编译成可执行文件的名称以及对应的代码
add_executable(1 1.cpp)
# 链接时候的库文件
target_link_libraries(1 ${LINKER_LIBS})



此时呢,你需要将caffe目录下的cmake/Modules/中的
FindGlog.cmake和FindGFlags.cmake都复制到自己的项目中的cmake文件夹下
这样cmake就能自动找到glog以及gflags的头文件以及库文件了。
这里假设自己的代码文件就一个文件:1.cpp

然后执行cmke-gui
选择你自己的项目的目录configure一下。
勾选cmake界面的Grouped和Advanced
然后找到CMAKE下面的CMAKE_BUILD_TYPE,填入Debug即可设置项目为Debug模式,这样在你导入到eclipse的时候可以进行调试
如果不设置成Debug还可以设置为Release等模式。
当然如果你不喜欢图形界面也可以用cmake -DCMAKE_BUILD_TYPE=Debug /your/project/path 来生成makefile文件。

最后放出
FindGFlags.cmake和 FindGlog.cmake的代码,这样你就不需要下载这两个文件了,其实caffe里面给出很多可用的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()
  
将以上代码保存为 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()
  


将以上代码保存为 FindGlog.cmake


通过CMake管理自己的项目的编译是不是觉得很方便呢,自动生成makefile文件,而且还能够处理头文件以及库文件的问题,感觉世界清爽多了,可以安静地写自己的程序了。

注:这些都是作者自己日常记录而已。

你可能感兴趣的:(c/c++,神经网络,图像处理)