CMake小结 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 cmake -B build -DCMAKE_BUILD_TYPE=Release cmake --build build -j4 cmake --build build --target install 如上设置了-D 当不清除Cache时,下次不加-D,也是Release -G 指定构建系统生成器 在cmake --help里面可以看到 动态库链接静态库,需要开启PIC set (CMAKE_POSITION_INDEPENDENT_CODE ON ) 或者 set_property (TRGET lib PROPERTY POSITION_INDEPENDENT_CODE ON ) 设定find_package 路径,把路径加入CMAKE_MODULE_PATH、查看包搜索路径,如Qt会找Qt5_DIR,把路径放这里也好使、环境变量的Qt5_DIR也行 生成器表达式语法:$<$<类型:值>:为真时的表达式> 比如 $<$<PLATFORM_ID:Windows,ABC>:MY_NAME="Bill Gates" > 在 Windows或者ABC 平台上会变为 MY_NAME="Bill Gates" set (abc HELLO)set (HELLO ccc)if (abc MATCH HELLO) doneif (${abc} MATCH HELLO) no 其会展开为HELLO变量 再变成cccif ("${abc}" MATCH HELLO) done__declspec(dllexport) 导出 __declspec(dllimport) 导入 aux_source_directory (<dir> <variable>) list () include (<file |module> [OPTIONAL] [RESULT_VARIABLE <var>] [NO_POLICY_SCOPE]) set (<variable> <value>... [PARENT_SCOPE]) set (<variable> <value>... CACHE <type> <docstring> [FORCE]) set (ENV{<variable>} [<value>]) file (read <filename> <variable> [OFFSET <offset>] [LIMIT <max-in>] [HEX]) file (STRINGS <filename> <variable> [<options> ...]) file (<HASH> <filename> <variable>) file (TIMESTAMP <filename> <variable> [<format>] [UTC]) file (WRITE <filename> <content> ... ) file (APPEND <filename> <content> ... ) file (TOUCH [<files> ...]) file (TOUCH_NOCREATE [<files> ...]) file (GENERATE OUTPUT output-file <INPUT input-file |CONTENT content> [CONDITION expression]) file (GLOB <variable> [LIST_DIRECTORIES true |false ] [RELATIVE <path>] [<globbing-expressions>...]) file (GLOB_RECURSE <variable> [FOLLOW_SYMLINKS] [LIST_DIRECTORIES true |false ] [RELATIVE <path>] [<globbing-expressions>...]) file (RENAME <oldname> <newname> [RESULT <result>] [NO_REPLACE]) file (REMOVE [<files>...]) file (REMOVE_RECURSE [<files>...]) file (MAKE_DIRECTORY [<directories> ...]) file (COPY_FILE <oldname> <newname> [RESULT <result>] [ONLY_IF_DIFFERENT]) file (<COPY|INSTALL > <files>... DESTINATION <dir> [FILE_PERMISSIONS <permissions>...] [DIRECTORY_PERMISSIONS <permissions>...] [NO_SOURCE_PERMISSIONS] [USE_SOURCE_PERMISSIONS] [FILES_MATCHING] [[PATTERN <pattern> | REGEX <regex>] [EXCLUDE] [PERMISSIONS <permissions>...]] [...]) file (SIZE <filename> <variable>)file (READ_SYMLINK <linkname> <variable>) file (CREATE_LINK <original> <linkname> [RESULT <result>] [COPY_ON_ERROR] [SYMBOLIC]) file (CHMOD|CHMOD_RECURSE <files>... <directories>... [PERMISSIONS <permissions>...] [FILE_PERMISSIONS <permissions>...] [DIRECTORY_PERMISSIONS <permissions>...]) file (REAL_PATH <path> <out-var> [BASE_DIRECTORY <dir>] [EXPAND_TILDE]) file (RELATIVE_PATH <variable> <directory> <file >) file (TO_CMAKE_PATH "<path>" <variable>) file (TO_NATIVE_PATH "<path>" <variable>) file (DOWNLOAD <url> [<file >] [<options>...]) file (UPLOAD <file > <url> [<options>...]) file (LOCK <path> [DIRECTORY] [RELEASE] [GUARD <FUNCTION |FILE |PROCESS>] [RESULT_VARIABLE <variable>] [TIMEOUT <seconds>]) file (ARCHIVE_CREATE OUTPUT <archive> PATHS <paths>... [FORMAT <format>] [COMPRESSION <compression> [COMPRESSION_LEVEL <compression-level>]] [MTIME <mtime>] [VERBOSE]) file (ARCHIVE_EXTRACT INPUT <archive> [DESTINATION <dir>] [PATTERNS <patterns>...] [LIST_ONLY] [VERBOSE] [TOUCH])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 add_library (<name> [STATIC | SHARED | MODULE] [EXCLUED_FROM_ALL] [<source>...]) add_library (<name> OBJECT [<source>...]) add_library (<name> INTERFACE) add_library (<name> <type> IMPORTED [GLOBAL]) add_library (<name> ALIAS <target >) add_executable (<name> [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [source1] [source2 ...]) add_executable (<name> IMPORTED [GLOBAL]) add_executable (<name> ALIAS <target >)target_sources (<target > <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...]) target_sources (<target > [<INTERFACE|PUBLIC|PRIVATE> [FILE_SET <set > [TYPE <type>] [BASE_DIRS <dirs>...] [FILES <files>...]]... ]...) target_compile_definitions (<target > <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...]) target_include_directories (<target > [SYSTEM] [AFTER|BEFORE] <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...]) include_directories ([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...]) link_directories ([AFTER|BEFORE] directory1 [directory2 ...]) link_libraries ([item1 [item2 [...]]] [[debug|optimized|general] <item>] ...) target_link_libraries (<target > ... <item>... ...) get_filename_component (<var> <FileName> <mode> [CACHE]) option (<variable> "<help_text>" [value]) configure_file (<input> <output> [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS | FILE_PERMISSIONS <permissions>...] [COPYONLY] [ESCAPE_QUOTES] [@ONLY] [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ]) 将文件复制到其它位置并修改内容,将input文件中@var@或者${var} 替换为cmake指定的值, 需要先include (CheckCXXCompilerFlag) check_cxx_compiler_flag(<flag> <var>) 检测编译器是否支持CXX,结果在var里 mark_as_advanced ([CLEAR|FORCE] <var1> ...) source_group (<name> [FILES <src>...] [REGULAR_EXPRESSION <regex>])source_group (TREE <root> [PREFIX <prefix>] [FILES <src>...]) set_property (<GLOBAL | DIRECTORY [<dir>] | TARGET [<target1> ...] | SOURCE [<src1> ...] [DIRECTORY <dirs> ...] [TARGET_DIRECTORY <targets> ...] | INSTALL [<file1> ...] | TEST [<test1> ...] | CACHE [<entry1> ...] > [APPEND] [APPEND_STRING] PROPERTY <name> [<value1> ...]) get_property (<variable> <GLOBAL | DIRECTORY [<dir>] | TARGET <target > | SOURCE <source> [DIRECTORY <dir> | TARGET_DIRECTORY <target >] | INSTALL <file > | TEST <test > | CACHE <entry> | VARIABLE > PROPERTY <name> [SET | DEFINED | BRIEF_DOCS | FULL_DOCS]) set_target_properties (target1 target2 ... PROPERTIES prop1 value1 prop2 value2 ...) get_target_property (<VAR> target property) add_subdirectory (source_dir [binary_dir] [EXCLUDE_FROM_ALL]) add_compile_options (<option > ...) target_compile_options (<target > [BEFORE] <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...]) add_definitions (-DFOO -DBAR ...) add_dependencies (<target > [<target -dependency>]...) message ([<mode>] "message text" ...) message (<checkState> "message text" ...) project (<PROJECT -NAME> [<language-name>...]) project (<PROJECT -NAME> [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]] [DESCRIPTION <project -description-string >] [HOMEPAGE_URL <url-string >] [LANGUAGES <language-name>...]) 当指定了project ,会自动定义如下变量,则PROJECT_NAME工程名;PROJECT_SOURCE_DIR源码路径;<PROJECT -NAME>_SOURCE_DIR源码路径,与前者区别就是后者是指定工程的,前者是当前工程的;PROJECT_BINARY_DIR二进制路径;<PROJECT -NAME>_BINARY_DIR同意思;CMAKE_PROJECT_NAME顶层工程名。 指定了VERSION,会定义出一堆PROJECT_VERSION*和<PROJECT -NAME>_VERSION*。其它两个也一样。目前支持C、CXX、CUDA、OBJC、OBJCXX、Fortran、ASM,不指定则是C和CXX,指定为NONE或者LANGUAGES则表示不支持任何语言。检测CMAKE_<LANGUAGES>_COMPILER指定编译器是否存在。多次调用,则以最近一次为准。 add_custom_target (Name [ALL] [command1 [args1...]] [COMMAND command2 [args2...] ...] [DEPENDS depend depend depend ... ] [BYPRODUCTS [files...]] [WORKING_DIRECTORY dir] [COMMENT comment] [JOB_POOL job_pool] [VERBATIM] [USES_TERMINAL] [COMMAND_EXPAND_LISTS] [SOURCES src1 [src2...]]) add_custom_command (OUTPUT output1 [output2 ...] COMMAND command1 [ARGS] [args1...] [COMMAND command2 [ARGS] [args2...] ...] [MAIN_DEPENDENCY depend] [DEPENDS [depends...]] [BYPRODUCTS [files...]] [IMPLICIT_DEPENDS <lang1> depend1 [<lang2> depend2] ...] [WORKING_DIRECTORY dir] [COMMENT comment] [DEPFILE depfile] [JOB_POOL job_pool] [VERBATIM] [APPEND] [USES_TERMINAL] [COMMAND_EXPAND_LISTS]) add_custom_command (TARGET <target > PRE_BUILD | PRE_LINK | POST_BUILD COMMAND command1 [ARGS] [args1...] [COMMAND command2 [ARGS] [args2...] ...] [BYPRODUCTS [files...]] [WORKING_DIRECTORY dir] [COMMENT comment] [VERBATIM] [USES_TERMINAL] [COMMAND_EXPAND_LISTS]) > 当add_custom_target 所要生成的target 依赖add_custom_command 所生成的文件时,这个文件就是一个纽带;add_custom_command 命令输出的OUTPUT文件和命令里的command 之间的关系是:每当这个文件需要被重新生成时,都会执行这段command ;开始构建target ,依赖add_custom_command 的输出文件,再查看其是否构建,其构建就会执行COMMAND 。 execute_process (COMMAND <cmd1> [<arguments>] [COMMAND <cmd2> [<arguments>]]... [WORKING_DIRECTORY <directory>] [TIMEOUT <seconds>] [RESULT_VARIABLE <variable>] [RESULTS_VARIABLE <variable>] [OUTPUT_VARIABLE <variable>] [ERROR_VARIABLE <variable>] [INPUT_FILE <file >] [OUTPUT_FILE <file >] [ERROR_FILE <file >] [OUTPUT_QUIET] [ERROR_QUIET] [COMMAND_ECHO <where>] [OUTPUT_STRIP_TRAILING_WHITESPACE] [ERROR_STRIP_TRAILING_WHITESPACE] [ENCODING <name>] [ECHO_OUTPUT_VARIABLE] [ECHO_ERROR_VARIABLE] [COMMAND_ERROR_IS_FATAL <ANY|LAST>]) find_package 目的是帮助我们调用对应的cmake文件,导入某个库,这个cmake文件是官方、安装、自行编写的,其格式为Find<lib_name>.cmake、。一般会产生<lib_name>_FOUND、<lib_name>_INCLUDE_DIR、<lib_name>_INCLUDES、<lib_name>_LIBRARY、<lib_name>_LIBRARIES,需要根据cmake文件中实现决定,并不统一。其引入模式分为两种,Module和Config,其中Module是从CMAKE_MODULE_PATH和cmake安装路径/Modules中,找的是Find<lib_name>.cmake;Config找的是<lib_name>Config.cmake或<lib_name>-config.cmake,其一般是通过安装添加到/usr/local/lib/cmake/...目录的。 find_package (<PackageName> [version] [EXACT] [QUIET] [MODULE] [REQUIRED] [[COMPONENTS] [components...]] [OPTIONAL_COMPONENTS components...] [REGISTRY_VIEW (64 |32 |64 _32|32 _64|HOST|TARGET |BOTH)] [GLOBAL] [NO_POLICY_SCOPE] [BYPASS_PROVIDER]) CONFIG|NO_MODULE,指定搜索模式;NAMES使用该名为lib_name名;PATHS/HINTS,Config模式下,搜索的路径;NO_XXX_PATH,Config模式下,忽略的路径。 默认优先用MODULE模式,CMAKE_FIND_PACKAGE_PREFER_CONFIG可以更换。
本文标题: CMake小结
文章作者: whppmy
发布时间: 2023-03-04
最后更新: 2022-09-11
原始链接: http://bugnull.com/无处安放/cmake小结/
版权声明: 个人记录,没有获取同意时,禁止转载!!