#win32->linux注意事项
#linux fprintf 需要加入#include <stdio.h> 头文件,而win32不需要
#--linux关键字
cmake_minimum_required(VERSION 3.5.1) #版本号
project(Gate) #工程名
add_executable(Gate Gate.cpp) #生成exe文件
add_library(AddFunction AddFunction.cpp) #add_library 生成lib库
set ( HelloCMake_VERSION_MAJOR 1 ) #定义c++的宏,类似于define HelloCMake_VERSION_MAJOR 1
configure_file( #根据配置文件生成C++用的头文件
"${PROJECT_SOURCE_DIR}/HelloCMakeConfig.h.in" #(全路径/src) PROJECT_SOURCE_DIR 是工程源文件所在的目录
"${PROJECT_BINARY_DIR}/HelloCMakeConfig.h" #(全路径/build)
)
#PROJECT_SOURCE_DIR 和 PROJECT_BINARY_DIR区别 http://blog.csdn.net/sukhoi27smk/article/details/46388711
#define HelloCMake_VERSION_MAJOR @HelloCMake_VERSION_MAJOR@ //定义在HelloCMakeConfig.h.in, 生成的头文件 HelloCMakeConfig.h 内容#define HelloCMake_VERSION_MAJOR 1
Include_directories ("${PROJECT_BINARY_DIR}") #路径(是工程源文件所在的目录)加入到工程头文件的搜索路径中去,这样工程编译链接时就能够找到HelloCMakeConfig.h这个头文件。
Include_directories 例子:
INCLUDE_DIRECTORIES(
${VTK_INCLUDE_DIRS_BUILD_TREE}
${VTK_INCLUDE_DIRS_SOURCE_TREE}
${VTK_INCLUDE_DIRS_BUILD_TREE_CXX}
${VTK_INCLUDE_DIRS_SYSTEM}
)
#define HelloCMake_VERSION_MAJOR @HelloCMake_VERSION_MAJOR@ #放在.h.in文件,@X@对X取值,取你宏定义的值 = define HelloCMake_VERSION_MAJOR 1,
if (USE_AddFunction) #if/endif语句块必须要成对出现,if和endif后面所带的参数也必须一致。
include_directories ("${PROJECT_SOURCE_DIR}/AddFunction")
#add_subdirectory (AddFunction) #为构建添加一个子路径 CMake:add_subdirectory(...)隐含的用法 http://www.cnblogs.com/Need4Speak/p/5418818.html
set (EXTRA_LIBS ${EXTRA_LIBS} AddFunction) #设置变量EXTRA_LIBS的值为AddFunction.lib
endif (USE_AddFunction) #USE_AddFunction cmake宏定义 .h.in文件 #cmakedefine USE_AddFunction
#完整例子
cmake_minimum_required (VERSION 2.8.3)
project (HelloCMake)
option(USE_AddFunction "Use our Add Function" ON) #option是CMake里的命令,它的作用就是在CMake GUI上增加一个选项
set ( HelloCMake_VERSION_MAJOR 1 )
set ( HelloCMake_VERSION_MINOR 0 )
configure_file(
"${PROJECT_SOURCE_DIR}/HelloCMakeConfig.h.in"
"${PROJECT_BINARY_DIR}/HelloCMakeConfig.h"
)
Include_directories ("${PROJECT_BINARY_DIR}")
if (USE_AddFunction)
include_directories ("${PROJECT_SOURCE_DIR}/AddFunction")
#add_subdirectory (AddFunction)
set (EXTRA_LIBS ${EXTRA_LIBS} AddFunction)
endif (USE_AddFunction)
add_library(AddFunction AddFunction.cpp)
add_executable(HelloCMake HelloCMake.cpp)
target_link_libraries (HelloCMake ${EXTRA_LIBS})