cmake的一些用法记录

2017-10-13  本文已影响0人  Crowley

遍历文件夹中的文件,分别编译

# save all file names in src folder into var {srcs}
file(GLOB_RECURSE srcs src/*.cpp)
# build and link each one of them, source is the loop var
foreach (source ${srcs})
    # get the file name, and save in {name}
    get_filename_component(name ${source} NAME_WE)
    add_executable(${name} ${source})
    target_link_libraries(${name} otherlib)
endforeach ()

添加glog

glog 为用来链接的库,使用的时候包含头文件.

#include <glog/logging.h>
find_path(GLOG_INCLUDE_DIR glog/logging.h
        PATHS ${GLOG_ROOT_DIR}
        NO_DEFAULT_PATH)
find_path(GLOG_INCLUDE_DIR glog/logging.h
        PATHS ${GLOG_ROOT_DIR})

find_library(GLOG_LIBRARY glog
        PATHS ${GLOG_ROOT_DIR}
        PATH_SUFFIXES lib lib64
        NO_DEFAULT_PATH)
find_library(GLOG_LIBRARY glog
        PATHS ${GLOG_ROOT_DIR}
        PATH_SUFFIXES lib lib64)

添加protobuf,以caffe为例

find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIRS})

include_directories(${CMAKE_CURRENT_BINARY_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS src/caffe.proto)
add_library(caffeproto STATIC ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(caffeproto ${PROTOBUF_LIBRARIES})

caffeproto为可以链接的库

上一篇下一篇

猜你喜欢

热点阅读