cmake例子:subproject

example:

cmake_minimum_required(VERSION 3.5)
project (tensorflow) # Set the project name
set(CMAKE_BUILD_TYPE Debug)# Debug Release
# use default compiler or specific one
set(CMAKE_CXX_COMPILER /opt/rocm/bin/hipcc)# 指定编译器
# global define
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPROFILING"
include(CheckCXXCompilerFlag)

CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
# check results and add flag
if(COMPILER_SUPPORTS_CXX11)#
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)#
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
    message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

#Generate the static library from the library sources
add_library(comm_libarary STATIC 
    src/Hello.cpp
) # libcomm_libarary.a or libcomm_libarary.so if SHARED
# Public let main.cpp know the header when compiling by set the link libraries.
target_include_directories(comm_libarary
    Public
        ${PROJECT_SOURCE_DIR}/include
) # INTERFACE, Public, Private
target_compile_definitions(comm_libarary
    PRIVATE ENABLE_FUSION
) # define ENABLE_FUSION

# third-party library according to FindXXX.cmake
find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)
# check if boost was found using exported variables: Boost_FOUND, Boost_INCLUDE_DIRS, 
if(Boost_FOUND)
    message ("boost found")
else()
    message (FATAL_ERROR "Cannot find Boost")
endif()
# let tensorflow include the boost headers
target_include_directories(tensorflow
    PRIVATE ${Boost_INCLUDE_DIRS}
)

# no need to include ${sublibrary1_INCLUDE_DIRS}, just link
add_subdirectory(sublib1)

# compile the executor with header.
# main.cpp contains #include "sublib1/sublib1.h"
add_executable(tensorflow main.cpp) #main.cpp will call hello.cpp
target_include_directories(tensorflow
    PRIVATE 
        ${Boost_INCLUDE_DIRS}
        ${PROJECT_SOURCE_DIR}/include
)
# set link libraries, main.cpp will know the include of comm_libarary
target_link_libraries(tensorflow
    PRIVATE 
        comm_libarary
        ${Boost_SYSTEM_LIBRARY}
        ${Boost_FILESYSTEM_LIBRARY}
        sub::lib1
) # # modern cmake: third-party library sub::lib1, just link

install (TARGETS tensorflow
    DESTINATION bin) # ${CMAKE_INSTALL_PREFIX}/bin/
install (TARGETS comm_libarary
    LIBRARY DESTINATION lib) # ${CMAKE_INSTALL_PREFIX}/lib/
# for developing
install (TARGETS ${sublibrary1_SOURCE_DIR}/include/
    DESTINATION include/) # ${CMAKE_INSTALL_PREFIX}/include/sublib1/
# PROJECT_SOURCE_DIR: the top level project
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/
    DESTINATION include) # ${CMAKE_INSTALL_PREFIX}/include/
# Install a configuration file to the destination
install (FILES comm_library.conf
    DESTINATION etc) # ${CMAKE_INSTALL_PREFIX}/etc
# @file: sublib1/*
# sublib1/include/sublib1/sublib1.h, sublib1/src/sublib1.cpp
# Set the project name
project (sublibrary1)
# Add a library with the above sources
add_library(${PROJECT_NAME} src/sublib1.cpp)
add_library(sub::lib1 ALIAS ${PROJECT_NAME})
target_include_directories( ${PROJECT_NAME}
    PUBLIC ${PROJECT_SOURCE_DIR}/include
)
# @file: ver.h.in
#ifndef __VER_H__
#define __VER_H__
// version variable that will be substituted by cmake
// This shows an example using the $ variable type
const char* ver = "${TF_VERSION}";
#endif
mkdir build && cd build
cmake ..  -DCMAKE_INSTALL_PREFIX=/install/location -DTF_VERSION=0.1
make -j VERBOSE=1
make install
sudo xargs rm < install_manifest.txt

你可能感兴趣的:(cmake例子:subproject)