cmake的一些用法記錄

遍歷文件夾中的文件,分別編譯

# 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為可以鏈接的庫

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容