每次都想走捷径,最后还是得倒回来,认认真真学习这些鸟语法。 This project aims to facilitate the conversion of Visual Studio to CMake projects. CMake Converter
mkdir build && cd build
cmake ..
cmake .. -G "Visual Studio 16 2019" -A Win32
cmake .. -G "Visual Studio 16 2019" -A x64
cmake .. -G "Visual Studio 16 2019" -A Win32 -DCMAKE_GENERATOR_TOOLSET=v141
Android CMake DOC CMake 命令的官方文档
cmake_minimum_required(VERSION 3.4.1)
# 指定一个本地头文件路径。
include_directories(src/main/cpp/include/)
# 编译构建
add_library( native-lib # 指定 lib 名称。
SHARED # 设置为 shared lib。
# 源代码位置。
src/main/cpp/native-lib.cpp )
# 构建一个 STATIC lib。
add_library( app-glue
STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
# 检索一个 lib。
find_library( log-lib # 定义一个 NDK lib 名字。
# CMake 去查找 CMake lib 的名字。
log )
# 导入一个 lib。
add_library( imported-lib # 导入一个 lib
SHARED
IMPORTED )
set_target_properties( imported-lib # 导入 lib 名字。
PROPERTIES IMPORTED_LOCATION # 需要的参数
imported-lib/src/${ANDROID_ABI}/libimported-lib.so )
# 连接一个静态 lib。
target_link_libraries( native-lib imported-lib app-glue ${log-lib} )
# 设置路径
set( lib_src_DIR ../gmath )
set( lib_build_DIR ../gmath/outputs )
file(MAKE_DIRECTORY ${lib_build_DIR}) # 创建路径。
# 子构建目录依赖。
add_subdirectory( ${lib_src_DIR} # CMakeLists.txt 文件夹
# 指定 build 文件夹。
${lib_build_DIR} )
add_library( lib_gmath STATIC IMPORTED )
set_target_properties( lib_gmath PROPERTIES IMPORTED_LOCATION
${lib_build_DIR}/${ANDROID_ABI}/lib_gmath.a )
include_directories( ${lib_src_DIR}/include )
target_link_libraries( native-lib ... lib_gmath )
target_link_libraries 为库或二进制可执行文件添加库链接,要用在 add_executable 之后。 上述指令中的 target 是指通过 add_executable() 和 add_library() 指令生成已经创建的目标文件。
Configuration | CMAKE_CXX_FLAGS_<CONFIG> | Runtime Library | Symbol Table | Optimize | Inline | Assert |
---|---|---|---|---|---|---|
Debug | /MDd /Zi /Ob0 /Od /RTC1 | debug | include | none | none | enable |
Release | /MD /O2 /Ob2 /DNDEBUG | non-debug | exclude | speed | any | disable |
RelWithDebInfo | /MD /Zi /O2 /Ob1 /DNDEBUG | non-debug | include | speed | declared | disable |
MinSizeRel | /MD /O1 /Ob1 /DNDEBUG | non-debug | exclude | size | declared | disable |
Android.mk & Application.mk
PROJECT(T1)
SET(SRC_LIST main.cpp)
MESSAGE(STATUS "This is the binary dir: " ${PROJECT_BINARY_DIR})
MESSAGE(STATUS "This is the source dir: " ${PROJECT_SOURCE_DIR})
# 此处的 t1 跟 PROJECT 定义的 T1 没什么关系
ADD_EXECUTABLE(t1 ${SRC_LIST})
系统信息
开关选项
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(T3)
# 若变量 BUILD_SHARED_LIBS 没有设置,并且在 ADD_LIBRARY 时没有指定库类型
# 默认生成的都是静态库
# SET(BUILD_SHARED_LIBS on)
ADD_SUBDIRECTORY(lib_hello)
AUX_SOURCE_DIRECTORY(. SRC_LIST)
ADD_LIBRARY(hello_shared SHARED ${SRC_LIST}) # lib 的名字不能重复
ADD_LIBRARY(hello_static STATIC ${SRC_LIST})
SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME "hello")
SET_TARGET_PROPERTIES(hello_shared PROPERTIES OUTPUT_NAME "hello")
# 设置动态库的版本号
SET_TARGET_PROPERTIES(hello_shared PROPERTIES VERSION 1.2 SOVERSION 1)
# 如果发现后来生成的 lib 清除了同名的早先生成的 lib,(只有一个静态库或动态库生成)
# 试下下面两个命令
# 较新版本的 cmake 不再需要下面两个命令就可以同时生成 hello.so hello.a
#SET_TARGET_PROPERTIES(hello_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1)
#SET_TARGET_PROPERTIES(hello_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
指令 (arg1 arg2…) 参数使用括弧括起,参数之间使用空格或分号分开。
以 ADD_EXECUTABLE 指令为例:
ADD_EXECUTABLE(hello main.c func.c) 或者
ADD_EXECUTABLE(hello main.c;func.c)
PROJECT(projectname [CXX] [C] [Java])
SET(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]])
MESSAGE([SEND_ERROR | STATUS | FATAL_ERROR] "message to display" ...)
ADD_EXECUTABLE(bin_file_name ${SRC_LIST})
ADD_LIBRARY(libname [SHARED | STATIC | MODULE] [EXCLUDE_FROM_ALL] SRC_LIST)
CMAKE_MINIMUM_REQUIRED(VERSION version_number [FATAL_ERROR])
ADD_SUBDIRECTORY(src_dir [binary_dir] [EXCLUDE_FROM_ALL])
INCLUDE_DIRECTORIES([AFTER | BEFORE] [SYSTEM] dir1 dir2 ... )
LINK_DIRECTORIES(dir1 dir2 ...)
TARGET_LINK_LIBRARIES(target lib1 lib2 ...)
ADD_DEPENDENCIES(target-name depend-target1 depend-target2 ...)
AUX_SOURCE_DIRECTORY(dir VAR)
EXEC_PROGRAM(Executable [dir where to run] [ARGS <args>][OUTPUT_VARIABLE <var>] [RETURN_VALUE <value>])
用于在指定目录运行某个程序(默认为当前 CMakeLists.txt 所在目录),通过 ARGS 添加参数,通过 OUTPUT_VARIABLE 和 RETURN_VALUE 获取输出和返回值,如下示例
# 在 src 中运行 ls 命令,在 src/CMakeLists.txt 添加
EXEC_PROGRAM(ls ARGS "*.c" OUTPUT_VARIABLE LS_OUTPUT RETURN_VALUE LS_RVALUE)
IF (not LS_RVALUE)
MESSAGE(STATUS "ls result: " ${LS_OUTPUT}) # 缩进仅为美观,语法无要求
ENDIF (not LS_RVALUE)
FIND_LIBRARY(libX X11 /usr/lib)
IF (NOT libx)
MESSAGE(FATAL_ERROR "libX not found")
ENDIF (NOT libX)
语法:
IF (expression)
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
ELSE (expression)
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
ENDIF (expression) # 一定要有 ENDIF 与 IF 对应
# 当给定的变量或者字符串能够匹配正则表达式 regex 时为真。比如:
IF ("hello" MATCHES "ell")
MESSAGE("true")
ENDIF ("hello" MATCHES "ell")
数字比较表达式
按照字母表顺序进行比较
一个小例子,用来判断平台差异:
IF (WIN32)
MESSAGE(STATUS “This is windows.”)
ELSE (WIN32)
MESSAGE(STATUS “This is not windows”)
ENDIF (WIN32)
上述代码用来控制在不同的平台进行不同的控制,但是,阅读起来却并不是那么舒服,
ELSE(WIN32) 之类的语句很容易引起歧义。
可以 SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
这时候就可以写成:
IF (WIN32)
ELSE ()
ENDIF ()
配合 ELSEIF 使用,可能的写法是这样:
IF (WIN32)
# do something related to WIN32
ELSEIF (UNIX)
# do something related to UNIX
ELSEIF (APPLE)
# do something related to APPLE
ENDIF (WIN32)
语法:
WHILE (condition)
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
ENDWHILE (condition)
其真假判断条件可以参考 IF 指令
FOREACH 指令的使用方法有三种形式:
语法:
FOREACH(loop_var arg1 arg2 ...)
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
ENDFOREACH(loop_var)
示例:
AUX_SOURCE_DIRECTORY(. SRC_LIST)
FOREACH(F ${SRC_LIST})
MESSAGE(${F})
ENDFOREACH(F)
FOREACH(loop_var RANGE total)
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
ENDFOREACH(loop_var)
从 0 到 total 以 1 为步进
FOREACH(VAR RANGE 10)
MESSAGE(${VAR})
ENDFOREACH(VAR)
输出:
0
1
2
3
4
5
6
7
8
9
10
语法:
FOREACH(loop_var RANGE start stop [step])
COMMAND1(ARGS ...)
COMMAND2(ARGS ...)
...
ENDFOREACH(loop_var)
从 start 开始到 stop 结束,以 step 为步进, 注意: 直到遇到 ENDFOREACH 指令,整个语句块才会得到真正的执行。
FOREACH(A RANGE 5 15 3)
MESSAGE(${A})
ENDFOREACH(A)
输出:
5
8
11
14
参考 ADD_LIBRARY 和 SET_TARGET_PROPERTIES 用法
# 指定生成目标
add_executable(Demo main.cc)
# 添加链接库
target_link_libraries(Demo MathFunctions)
# 生成链接库
add_library (MathFunctions ${DIR_LIB_SRCS})
参考 INCLUDE_DIRECTORIES, LINK_DIRECTORIES, TARGET_LINK_LIBRARIES 用法
t4 示例使用动态库或静态库
AUX_SOURCE_DIRECTORY(. SRC_LIST)
LINK_DIRECTORIES(/home/carl/cmake/t3/build/lib) # 这行一定要在 ADD_EXECUTABLE 前面
ADD_EXECUTABLE(t4 ${SRC_LIST})
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(CMAKE_PROJECT_DIR /home/carl/cmake) # cmake 文件夹的位置
INCLUDE_DIRECTORIES(${CMAKE_PROJECT_DIR}/t3/lib_hello)
TARGET_LINK_LIBRARIES(t4 hello) # 链接动态库
#TARGET_LINK_LIBRARIES(t4 libhello.a) # 链接静态库
t5 示例如何使用 cmake 预定义的 cmake 模块(以 FindCURL.cmake 为例演示)
AUX_SOURCE_DIRECTORY(. SRC_LIST)
ADD_EXECUTABLE(curltest ${SRC_LIST})
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
#INCLUDE_DIRECTORIES(/usr/include)
#TARGET_LINK_LIBRARIES(curltest curl)
FIND_PACKAGE(CURL)
IF (CURL_FOUND)
MESSAGE(STATUS "CURL_INCLUDE_DIR: " ${CURL_INCLUDE_DIR} " CURL_LIBRARY: " ${CURL_LIBRARY})
INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(curltest ${CURL_LIBRARY})
ELSE(CURL_FOUND)
MESSAGE(FATAL_ERROR "CURL library not found")
ENDIF (CURL_FOUND)
# 对于系统预定义的 Find<name>.cmake 模块,使用方法一般如上所示:
# 每一个模块都会定义以下几个变量
# • <name>_FOUND 通过此变量判断模块是否被找到
# • <name>_INCLUDE_DIR or <name>_INCLUDES
# • <name>_LIBRARY or <name>_LIBRARIES
t6 示例如何使用自定义的 cmake 模块(编写了自定义的 FindHELLO.cmake)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(HELLO)
MESSAGE(STATUS "此 Project 演示了如何使用自定义的 cmake 模块 (FindHELLO.cmake)")
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
ADD_SUBDIRECTORY(src)
# FindHELLO.cmake
# 注意此 cmake 文件中的 3 个变量名
# HELLO_INCLUDE_DIR -- <name>_INCLUDE_DIR
# HELLO_LIBRARY -- <name>_LIBRARY
# HELLO_FOUND -- <name>_FOUND
# 惯例需要定义这 3 个变量,命名规则如上,以便与 cmake 预定义的 cmake 模块中的变量名统一
# FIND_PATH 和 FIND_LIBARY 的 path 部分,一定要是 .h 文件和 lib 文件所在的当前目录,否则找不到
FIND_PATH(HELLO_INCLUDE_DIR hello.h /home/carl/cmake/t3/lib_hello)
MESSAGE(STATUS "HELLO_INCLUDE_DIR: " ${HELLO_INCLUDE_DIR})
FIND_LIBRARY(HELLO_LIBRARY hello /home/carl/cmake/t3/build/lib)
MESSAGE(STATUS "HELLO_LIBRARY: " ${HELLO_LIBRARY})
IF (HELLO_INCLUDE_DIR AND HELLO_LIBRARY)
SET(HELLO_FOUND TRUE)
ENDIF (HELLO_INCLUDE_DIR AND HELLO_LIBRARY)
IF (HELLO_FOUND)
# 若 FIND_PACKAGE 命令没有指定 QUIET 参数,那下面的 IF 语句就为真
IF (NOT HELLO_FIND_QUIETLY)
MESSAGE(STATUS "Found Hello: ${HELLO_LIBRARY}")
ENDIF (NOT HELLO_FIND_QUIETLY)
ELSE (HELLO FOUND)
# 如果 FIND_PACKAGE 命令指定了 REQUIRED 参数,说明这个库是此 Project 必须的
# 对应下面的 HELLO_FIND_REQUIRED 变量
IF (HELLO_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find hello library")
ENDIF (HELLO_FIND_REQUIRED)
ENDIF (HELLO_FOUND)
AUX_SOURCE_DIRECTORY(. SRC_LIST)
ADD_EXECUTABLE(t6 ${SRC_LIST})
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# 没有指定 QUIET 参数,HELLO_FIND_QUIETLY 为假
# 指定 REQUIRED 参数,HELLO_FIND_REQUIRED 为真,意味着此 lib 为该 Project 必须
FIND_PACKAGE(HELLO REQUIRED)
# 下面的 HELLO_FOUND HELLO_INCLUDE_DIR HELLO_LIBRARY
# 3 个变量在 FindHELLO.cmake 中定义
IF (HELLO_FOUND)
INCLUDE_DIRECTORIES(${HELLO_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(t6 ${HELLO_LIBRARY})
ENDIF (HELLO_FOUND)
注意 读 t5 和 t6 的 CMakeLists.txt 和 FindHELLO.cmake 中的注释部分。
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
上面的两条命令通常紧跟 ADD_EXECUTABLE 和 ADD_LIBRARY,与其写在同一个 CMakeLists.txt 即可
AUX_SOURCE_DIRECTORY(. SRC_LIST)
ADD_EXECUTABLE(t2 ${SRC_LIST})
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
建议 :在 Project 根目录先建立 build,然后在 build 文件夹内运行 cmake..,这样就不会污染源代码,如果不想要这些自动生成的文件了,只要简单的删除 build 文件夹就可以。
cmake_minimum_required(VERSION 3.4.1)
# 等价的变量 PROJECT_BINARY_DIR 和 PROJECT_SOURCE_DIR。
# 它会引入两个变量 demo_BINARY_DIR 和 demo_SOURCE_DIR。
project(demo)
# 设置编译类型
add_executable(demo demo.cpp) # 生成可执行文件
add_library(common STATIC util.cpp) # 生成静态库
add_library(common SHARED util.cpp) # 生成动态库或共享库
# add_library 默认生成是静态库,通过以上命令生成文件名字,
# 在 Linux 下是:demo libcommon.a libcommon.so
# 在 Windows 下是:demo.exe common.lib common.dll
# 指定编译包含的源文件
# 1. 明确指出包含哪些源文件
add_library(demo demo.cpp test.cpp util.cpp)
# 2. 搜索所有的 cpp 文件
aux_source_directory(dir VAR) # 发现一个目录 (dir) 下所有的源代码文件并将列表存储在一个变量 (VAR) 中。
aux_source_directory(. SRC_LIST) # 搜索当前目录下的所有 .cpp 文件
add_library(demo ${SRC_LIST})
# 3. 自定义搜索规则
file(GLOB SRC_LIST "*.cpp" "protocol/*.cpp")
add_library(demo ${SRC_LIST})
# 或者
file(GLOB SRC_LIST "*.cpp")
file(GLOB SRC_PROTOCOL_LIST "protocol/*.cpp")
add_library(demo ${SRC_LIST} ${SRC_PROTOCOL_LIST})
# 或者
aux_source_directory(. SRC_LIST)
aux_source_directory(protocol SRC_PROTOCOL_LIST)
add_library(demo ${SRC_LIST} ${SRC_PROTOCOL_LIST})
# 设置包含的目录
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
)
# 加入一个配置头文件,用于处理 CMake 对源码的设置
configure_file (
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
# 是否使用自己的 MathFunctions 库
option (USE_MYMATH
"Use provided math implementation" ON) # 设置为 ON,或 OFF
# 是否加入 MathFunctions 库
if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/math")
add_subdirectory (math)
set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 指定生成目标
add_executable(Demo ${DIR_SRCS})
target_link_libraries (Demo ${EXTRA_LIBS})
config.h.in:
// 这样 CMake 会自动根据 CMakeLists 配置文件中的设置自动生成 config.h 文件。
#cmakedefine USE_MYMATH
// the configured options and settings for Tutorial
#define Demo_VERSION_MAJOR @Demo_VERSION_MAJOR@
#define Demo_VERSION_MINOR @Demo_VERSION_MINOR@
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
# 指定 MathFunctions 库的安装路径
install (TARGETS MathFunctions DESTINATION bin)
install (FILES MathFunctions.h DESTINATION include)
# 指定安装路径
install (TARGETS Demo DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)
ROOT -- CMakeLists.txt addlib build main.cpp
+-- CMakeLists.txt library.cpp library.h
cmake_minimum_required(VERSION 3.10)
project(cppproject)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(acclib)
add_executable(cppproject main.cpp) # 生成一个可执行的文件
target_link_libraries(cppproject accliblibrary)
cmake_minimum_required(VERSION 3.10)
project(accliblibrary)
set(CMAKE_CXX_STANDARD 11)
add_library(accliblibrary SHARED library.cpp library.h) # 诉生成一个库文件。
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
// 指定一些编译选项
cppFlags "-std=c++11 -frtti -fexceptions"
...
// 也可以使用下面这种语法向变量传递参数:
// arguments "-DVARNAME=ARGV".
arguments "-DANDROID_ARM_NEON=TRUE",
// 使用下面这种语法向变量传递多个参数(参数之间使用空格隔开):
// arguments "-DVARNAME=arg1 arg2"
"-DANDROID_CPP_FEATURES=rtti exceptions"
// 指定 ABI
abiFilters "armeabi-v7a", "arm64-v8a"
}
}
}
buildTypes {...}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
CMAKE_C_FLAGS =
CMAKE_C_FLAGS_DEBUG = -g
CMAKE_C_FLAGS_MINSIZEREL = -Os -DNDEBUG
CMAKE_C_FLAGS_RELEASE = -O3 -DNDEBUG
CMAKE_C_FLAGS_RELWITHDEBINFO = -O2 -g -DNDEBUG
CMAKE_CXX_FLAGS =
CMAKE_CXX_FLAGS_DEBUG = -g
CMAKE_CXX_FLAGS_MINSIZEREL = -Os -DNDEBUG
CMAKE_CXX_FLAGS_RELEASE = -O3 -DNDEBUG
CMAKE_CXX_FLAGS_RELWITHDEBINFO = -O2 -g -DNDEBUG
# https://github.com/juj/MathGeoLib/blob/master/CommonOptions.cmake
# Add the global _DEBUG flag from WIN32 platform to all others, which is universally used in MGL to
# perform debug-mode-specific compilation.
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
IF (WIN32 AND NOT CYGWIN)
SET(CMAKE_C_FLAGS_DEBUG "-D_DEBUG")
ENDIF ()
可以在代码里面检查:
// check that exactly one of NDEBUG and _DEBUG is defined
#if !defined(NDEBUG) ^ defined(_DEBUG)
#error Exactly one of NDEBUG and _DEBUG needs to be defined!
#endif
#ifdef CMAKE_INTDIR
#if defined(_MSC_VER) && !defined(NDEBUG)
#define _DEBUG
#endif // defined(_MSC_VER) && !defined(NDEBUG)
#endif // CMAKE_INTDIR
/*! `NDEBUG` or `_DEBUG` mean not build on `DEBUG` mode. */
#ifndef NDEBUG
#ifndef _DEBUG
#define _DEBUG
#endif /* _DEBUG */
#endif /* NDEBUG */
// https://apache.github.io/xalan-c/api/PlatformDefinitions_8hpp_source.html
#if defined(_DEBUG) && defined(NDEBUG)
#error NDEBUG must not be defined when _DEBUG is defined.
#elif !defined(_DEBUG) && !defined(NDEBUG)
#error NDEBUG must be defined when _DEBUG is not defined.
#endif
cmake_minimum_required(VERSION 3.4)
message(STATUS "CMAKE_C_FLAGS = " ${CMAKE_C_FLAGS})
message(STATUS "CMAKE_C_FLAGS_DEBUG = " ${CMAKE_C_FLAGS_DEBUG})
message(STATUS "CMAKE_C_FLAGS_MINSIZEREL = " ${CMAKE_C_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_C_FLAGS_RELEASE = " ${CMAKE_C_FLAGS_RELEASE})
message(STATUS "CMAKE_C_FLAGS_RELWITHDEBINFO = " ${CMAKE_C_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_CXX_FLAGS = " ${CMAKE_CXX_FLAGS})
message(STATUS "CMAKE_CXX_FLAGS_DEBUG = " ${CMAKE_CXX_FLAGS_DEBUG})
message(STATUS "CMAKE_CXX_FLAGS_MINSIZEREL = " ${CMAKE_CXX_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_CXX_FLAGS_RELEASE = " ${CMAKE_CXX_FLAGS_RELEASE})
message(STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO = " ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_EXE_LINKER_FLAGS = " ${CMAKE_EXE_LINKER_FLAGS})
message(STATUS "CMAKE_EXE_LINKER_FLAGS_DEBUG = " ${CMAKE_EXE_LINKER_FLAGS_DEBUG})
message(STATUS "CMAKE_EXE_LINKER_FLAGS_MINSIZEREL = " ${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_EXE_LINKER_FLAGS_RELEASE = " ${CMAKE_EXE_LINKER_FLAGS_RELEASE})
message(STATUS "CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO = " ${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_MODULE_LINKER_FLAGS = " ${CMAKE_MODULE_LINKER_FLAGS})
message(STATUS "CMAKE_MODULE_LINKER_FLAGS_DEBUG = " ${CMAKE_MODULE_LINKER_FLAGS_DEBUG})
message(STATUS "CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL = " ${CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_MODULE_LINKER_FLAGS_RELEASE = " ${CMAKE_MODULE_LINKER_FLAGS_RELEASE})
message(STATUS "CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO = " ${CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_SHARED_LINKER_FLAGS = " ${CMAKE_SHARED_LINKER_FLAGS})
message(STATUS "CMAKE_SHARED_LINKER_FLAGS_DEBUG = " ${CMAKE_SHARED_LINKER_FLAGS_DEBUG})
message(STATUS "CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL = " ${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_SHARED_LINKER_FLAGS_RELEASE = " ${CMAKE_SHARED_LINKER_FLAGS_RELEASE})
message(STATUS "CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO = " ${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_STATIC_LINKER_FLAGS = " ${CMAKE_STATIC_LINKER_FLAGS})
message(STATUS "CMAKE_STATIC_LINKER_FLAGS_DEBUG = " ${CMAKE_STATIC_LINKER_FLAGS_DEBUG})
message(STATUS "CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL = " ${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL})
message(STATUS "CMAKE_STATIC_LINKER_FLAGS_RELEASE = " ${CMAKE_STATIC_LINKER_FLAGS_RELEASE})
message(STATUS "CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO = " ${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO})
C:\chenchang\VMs\share\test\bld_win32>cmake -G "Visual Studio 14 2015 Win64" ..
-- CMAKE_C_FLAGS = /DWIN32 /D_WINDOWS /W3
-- CMAKE_C_FLAGS_DEBUG = /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
-- CMAKE_C_FLAGS_MINSIZEREL = /MD /O1 /Ob1 /D NDEBUG
-- CMAKE_C_FLAGS_RELEASE = /MD /O2 /Ob2 /D NDEBUG
-- CMAKE_C_FLAGS_RELWITHDEBINFO = /MD /Zi /O2 /Ob1 /D NDEBUG
-- CMAKE_CXX_FLAGS = /DWIN32 /D_WINDOWS /W3 /GR /EHsc
-- CMAKE_CXX_FLAGS_DEBUG = /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
-- CMAKE_CXX_FLAGS_MINSIZEREL = /MD /O1 /Ob1 /D NDEBUG
-- CMAKE_CXX_FLAGS_RELEASE = /MD /O2 /Ob2 /D NDEBUG
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO = /MD /Zi /O2 /Ob1 /D NDEBUG
-- CMAKE_EXE_LINKER_FLAGS = /machine:x64
-- CMAKE_EXE_LINKER_FLAGS_DEBUG = /debug /INCREMENTAL
-- CMAKE_EXE_LINKER_FLAGS_MINSIZEREL = /INCREMENTAL:NO
-- CMAKE_EXE_LINKER_FLAGS_RELEASE = /INCREMENTAL:NO
-- CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO = /debug /INCREMENTAL
-- CMAKE_MODULE_LINKER_FLAGS = /machine:x64
-- CMAKE_MODULE_LINKER_FLAGS_DEBUG = /debug /INCREMENTAL
-- CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL = /INCREMENTAL:NO
-- CMAKE_MODULE_LINKER_FLAGS_RELEASE = /INCREMENTAL:NO
-- CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO = /debug /INCREMENTAL
-- CMAKE_SHARED_LINKER_FLAGS = /machine:x64
-- CMAKE_SHARED_LINKER_FLAGS_DEBUG = /debug /INCREMENTAL
-- CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL = /INCREMENTAL:NO
-- CMAKE_SHARED_LINKER_FLAGS_RELEASE = /INCREMENTAL:NO
-- CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO = /debug /INCREMENTAL
-- CMAKE_STATIC_LINKER_FLAGS = /machine:x64
-- CMAKE_STATIC_LINKER_FLAGS_DEBUG =
-- CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL =
-- CMAKE_STATIC_LINKER_FLAGS_RELEASE =
-- CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO =
-- Configuring done
-- Generating done
-- Build files have been written to: C:/chenchang/VMs/share/test/bld_win32
root@chanchen-VirtualBox:/mnt/share/test/bld_lnx# cmake ..
-- CMAKE_C_FLAGS =
-- CMAKE_C_FLAGS_DEBUG = -g
-- CMAKE_C_FLAGS_MINSIZEREL = -Os -DNDEBUG
-- CMAKE_C_FLAGS_RELEASE = -O3 -DNDEBUG
-- CMAKE_C_FLAGS_RELWITHDEBINFO = -O2 -g -DNDEBUG
-- CMAKE_CXX_FLAGS =
-- CMAKE_CXX_FLAGS_DEBUG = -g
-- CMAKE_CXX_FLAGS_MINSIZEREL = -Os -DNDEBUG
-- CMAKE_CXX_FLAGS_RELEASE = -O3 -DNDEBUG
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO = -O2 -g -DNDEBUG
-- CMAKE_EXE_LINKER_FLAGS =
-- CMAKE_EXE_LINKER_FLAGS_DEBUG =
-- CMAKE_EXE_LINKER_FLAGS_MINSIZEREL =
-- CMAKE_EXE_LINKER_FLAGS_RELEASE =
-- CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO =
-- CMAKE_MODULE_LINKER_FLAGS =
-- CMAKE_MODULE_LINKER_FLAGS_DEBUG =
-- CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL =
-- CMAKE_MODULE_LINKER_FLAGS_RELEASE =
-- CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO =
-- CMAKE_SHARED_LINKER_FLAGS =
-- CMAKE_SHARED_LINKER_FLAGS_DEBUG =
-- CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL =
-- CMAKE_SHARED_LINKER_FLAGS_RELEASE =
-- CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO =
-- CMAKE_STATIC_LINKER_FLAGS =
-- CMAKE_STATIC_LINKER_FLAGS_DEBUG =
-- CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL =
-- CMAKE_STATIC_LINKER_FLAGS_RELEASE =
-- CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO =
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/share/test/bld_lnx
cmake_minimum_required(VERSION 3.4.1)
# https://github.com/juj/MathGeoLib/blob/master/CommonOptions.cmake
# Add the global _DEBUG flag from WIN32 platform to all others, which is universally used in MGL to
# perform debug-mode-specific compilation.
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
add_definitions(-DUNICODE -D_UNICODE)
set(OGL3RD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../opengl-3rd")
set(FAKEDRIVER_DIR "E:/cfwpe/irrlicht/fakedriver")
#[[
# fakedriver fakedriverAdapter
add_subdirectory(${FAKEDRIVER_DIR}/fakedriverAdapter fakedriverAdapter.build)
add_subdirectory(${FAKEDRIVER_DIR}/fakedriver fakedriver.build)
# fakelib
add_subdirectory(${FAKEDRIVER_DIR}/fakelib fakelib.build)
#]]
include_directories(
${FAKEDRIVER_DIR}/fakelib/include
${FAKEDRIVER_DIR}/include/khronos
${FAKEDRIVER_DIR}/include
${OGL3RD_DIR}/glad/include
${OGL3RD_DIR}/glfw-3.3.2.bin.WIN32/glfw-3.3.2.bin.WIN32/include
${OGL3RD_DIR}/glew-2.2.0-win32/glew-2.2.0/include
${OGL3RD_DIR}/glm-0.9.9.8/glm
${OGL3RD_DIR}/SOIL/include/SOIL
${OGL3RD_DIR}/assimp-5.0.1/assimp-5.0.1/include
${OGL3RD_DIR}/assimp-5.0.1/assimp-5.0.1/lib/vs2017_win32
${OGL3RD_DIR}/learnopengl
${OGL3RD_DIR}/stb
${OGL3RD_DIR}
)
link_directories(
${OGL3RD_DIR}/glew-2.2.0-win32/glew-2.2.0/lib/Release/Win32
${OGL3RD_DIR}/glfw-3.3.2.bin.WIN32/glfw-3.3.2.bin.WIN32/lib-vc2017
${OGL3RD_DIR}/SOIL/lib/vs2017/Release
${OGL3RD_DIR}/glad/lib/vs2017_win32/Release
)
FILE(GLOB_RECURSE OGL_HEADERS
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
"${OGL3RD_DIR}/learnopengl/*.*"
)
source_group(TREE "${OGL3RD_DIR}" FILES ${OGL_HEADERS})
set(
DEMO_SOURCES
irrtest.cpp
myshader.h
shaders/irrtest.vert
shaders/irrtest.frag
)
#[[
FILE(GLOB_RECURSE DEMO_SOURCES
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/5.2.framebuffers_exercise1/*.*"
)
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${DEMO_SOURCES})
#]]
# https://learnopengl-cn.readthedocs.io/zh/latest/01%20Getting%20started/08%20Coordinate%20Systems/
add_executable(
irrtest
${DEMO_SOURCES}
#${OGL_HEADERS}
)
target_link_libraries(
irrtest
glfw3 glew32s
SOIL
opengl32 # fakedriver
glad
)
# https://www.cnblogs.com/tangxin-blog/p/8283460.html
set_target_properties(irrtest PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/product")
set_target_properties(irrtest PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/product")