Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My note #87

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion 01-basic/A-hello-cmake/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ cmake_minimum_required(VERSION 3.5)

A CMake build can include a project name to make referencing certain
variables easier when using multiple projects.
(**CMake构建可以包含一个项目名称,以确保使用多个项目时,引用变量更容易**)

[source,cmake]
----
Expand All @@ -56,6 +57,7 @@ The +add_executable()+ command specifies that an executable should be
build from the specified source files, in this example main.cpp. The
first argument to the +add_executable()+ function is the name of the
executable to be built, and the second argument is the list of source files to compile.
(**这将创建一个可执行程序,但不一定需要与项目同名**)

[source,cmake]
----
Expand Down Expand Up @@ -88,6 +90,8 @@ The root or top level folder that you run the cmake command from is known as you
CMAKE_BINARY_DIR and is the root folder for all your binary files.
CMake supports building and generating your binary files both in-place and also
out-of-source.
(**CMAKE_BINARY_DIR,为运行cmake命令的工作目录,是二进制目标文件的根文件目录,即二进制中间目标文件存放位置的顶级目录**)
(**cmake支持在本地或源文件之外,构建您的二进制文件**)


#### In-Place Build
Expand Down Expand Up @@ -153,7 +157,7 @@ A-hello-cmake$ tree
----


#### Out-of-Source Build
#### Out-of-Source Build(**推荐**)

Out-of-source builds allow you to create a single build folder that can be anywhere on
your file system. All temporary build and object files are located in this directory keeping
Expand Down
16 changes: 9 additions & 7 deletions 01-basic/B-hello-headers/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ Some of these include:
[cols=",",options="header",]
|=======================================================================
|Variable |Info
|CMAKE_SOURCE_DIR |The root source directory
|CMAKE_SOURCE_DIR |The root source directory(最外层CMakeLists.txt所在目录)

|CMAKE_CURRENT_SOURCE_DIR |The current source directory if using
sub-projects and directories.
sub-projects and directories.(当前处理的CMakeLists.txt所在的目录)

|PROJECT_SOURCE_DIR |The source directory of the current cmake project.
|PROJECT_SOURCE_DIR |The source directory of the current cmake project.(包含PROJECT()命令的最近一个CMakeLists.txt所在文件夹路径)

|CMAKE_BINARY_DIR |The root binary / build directory. This is the
directory where you ran the cmake command.
directory where you ran the cmake command.(二进制文件所在的根目录)

|CMAKE_CURRENT_BINARY_DIR |The build directory you are currently in.
|CMAKE_CURRENT_BINARY_DIR |The build directory you are currently in.(当前正在处理的二进制目录路径)

|PROJECT_BINARY_DIR |The build directory for the current project.
|PROJECT_BINARY_DIR |The build directory for the current project.(当前工程所创建的build目录)
|=======================================================================

## Source Files Variable
## Source Files Variable(给需要多个一起编译的源文件设置一个统一变量名)

Creating a variable which includes the source files allows you to be
clearer about these files and easily add them to multiple commands, for example,
Expand Down Expand Up @@ -98,6 +98,8 @@ correct results if you add a new source file.
When you have different include folders, you can make your compiler aware of them using the
+target_include_directories()+ link:https://cmake.org/cmake/help/v3.0/command/target_include_directories.html[function]. When compiling this target this will add these directories to the compiler with the -I flag e.g. `-I/directory/path`

(*编译此目标时,将把该目录添加到编译器中,故这里的target应该指的是可执行程序,而目录指的是头文件所在目录*)

[source,cmake]
----
target_include_directories(target
Expand Down
22 changes: 18 additions & 4 deletions 01-basic/C-static-library/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ $ tree

# Concepts

## Adding a Static Library
## Adding a Static Library(创建一个静态库)

The +add_library()+ function is used to create a library from some source files.
This is called as follows:

(*使用关键字STATIC-静态,这里的target为hello_library,不必与工程同名,只是声明一个静态库target名,这将创建一个名为libhello_library.a的静态库(在二进制文件目录下)*)

(*使用的源文件目录,默认以CMakeLists.txt所在目录为起点*)

[source,cmake]
----
add_library(hello_library STATIC
Expand All @@ -57,6 +61,12 @@ As mentioned in the previous example, we pass the source files directly to the

In this example, we include directories in the library using the +target_include_directories()+ function with the scope set to +PUBLIC+.

(*这里的target目标,为hello_library,其实是库target名*)

(*为target包含的目录为~/include,所以库的源文件只需#include "static/Hello.h"(这就是target's include directory),而不需使用绝对路径*)

(*这里的范围属性scope,设置成PUBIC,表示当编译这个target或连接link了这个target的其他target时,将该目录添加到target或连接了该target的其他target的包含目录中,所以这也是为什么连接了库的源文件只需#include "static/Hello.h"*)

[source,cmake]
----
target_include_directories(hello_library
Expand All @@ -72,9 +82,9 @@ This will cause the included directory used in the following places:

The meaning of scopes are:

* +PRIVATE+ - the directory is added to this target's include directories
* +INTERFACE+ - the directory is added to the include directories for any targets that link this library.
* +PUBLIC+ - As above, it is included in this library and also any targets that link this library.
* +PRIVATE+ - the directory is added to this target's include directories(将目录添加到该target的包含目录中)
* +INTERFACE+ - the directory is added to the include directories for any targets that link this library.(该目录将添加到连接此target的任何其他target的包含目录中,不包括本target)
* +PUBLIC+ - As above, it is included in this library and also any targets that link this library.(如上所述,它包括在此target中,也包括连接此target的任何其他target)


[TIP]
Expand All @@ -100,6 +110,10 @@ you use multiple libraries in your project.
When creating an executable that will use your library you must tell the compiler
about the library. This can be done using the +target_link_libraries()+ function.

(*创建可执行文件,之传递了一个main.cpp,我们还需建可执行文件与库连接起来,我们使用target_link_libraries函数来告诉编译器*)

(*这里只需将范围属性设置成PRIVATE*)

[source,cmake]
----
add_executable(hello_binary
Expand Down
10 changes: 9 additions & 1 deletion 01-basic/D-shared-library/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ $ tree

# Concepts

## Adding a Shared Library
## Adding a Shared Library(创建一个动态库)

As with the previous example on static libraries, the +add_library()+ function
is also used to create a shared library from some source files.
This is called as follows:

(*使用关键自SHARED-动态,这将创建一个名为libhello_library.so的动态库文件*)

[source,cmake]
----
add_library(hello_library SHARED
Expand All @@ -59,6 +61,12 @@ add_library(hello::library ALIAS hello_library)

As shown below, this allows you to reference the target using the alias name when linking it against other targets.

## 添加包含目录

----
target_include_directories(hello_library PUBLIC {PROJECT_SOURCE_DIR}/include)
----

## Linking a Shared Library

Linking a shared library is the same as linking a static library. When creating your
Expand Down
17 changes: 15 additions & 2 deletions 01-basic/F-build-type/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@ CMake has a number of built in build configurations which can be used to compile
your project. These specify the optimization levels and if debug information is
to be included in the binary.

(*CMake中有许多内置的构建配置,用来编译你的工程project。它们指定了优化级别以及是否将调试信息包含在二进制文件中*)

The levels provided are:

* Release - Adds the `-O3 -DNDEBUG` flags to the compiler

(*发行版,顾名思义,程序完成开发后的发布版本,对代码做了优化,速度快,但无法跟踪代码,不能打断点调试*)

* Debug - Adds the `-g` flag

(*调试版,即对代码不做任何优化,但可以调试项目中的任意文件。速度相对较慢、体积也更大*)

* MinSizeRel - Adds `-Os -DNDEBUG`

(*Release的近似版,代码也同样记过优化,但同时也支持添加断点进行调试*)

* RelWithDebInfo - Adds `-O2 -g -DNDEBUG` flags

(最小体积版本,与Release版近似,但更偏重于优化文件大小,而非运行速度)

The files in this tutorial are below:

Expand All @@ -31,11 +44,11 @@ $ tree

# Concepts

## Set Build Type
## Set Build Type (构建类型)

The build type can be set using the following methods.

- Using a gui tool such as ccmake / cmake-gui
- Using a gui tool such as ccmake / cmake-gui (CMake图形界面)

image::cmake-gui-build-type.png[cmake-gui build type]

Expand Down
2 changes: 2 additions & 0 deletions 01-basic/G-compile-flags/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ toc::[]

# Introduction

(*compile flags——编译标志。可执行文件的生成离不开编译,那么如何编译,如使用C++或C的那个版本?这就要用到编译标志了*)

CMake supports setting compile flags in a number of different ways:

* using +target_compile_definitions()+ function
Expand Down
37 changes: 32 additions & 5 deletions 01-basic/H-third-party-library/README.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= Including Third Party Library
= Including Third Party Library(包含第三方库)
:toc:
:toc-placement!:

Expand All @@ -13,6 +13,14 @@ the `find_package()` function. This will search for CMake modules in the format
default search path will include `/usr/share/cmake/Modules`. On my system this
includes support for approximately 142 common third party libraries.

(*几乎所有非平凡的项目都要求包含第三方库、头文件或程序。CMake支持使用find_package()函数(间接)查找这些工具的路径*)

(*这将从CMAKE_MODULE_PATH中的文件夹列表搜索格式为FindXXX.cmake的CMake模块,如果没找到将会查找XXXConfig.cmake或XXX-config.cmake,在这些格式的文件里定义了许多变量,用于指定要包含的库和头文件的路径*)

(*CMAKE_MODULE_PATH指定的默认路径为/usr/share/cmake-<version>/Modules;在我的系统中,包含对大约142个通用第三方库的支持*)

(*find_package()支持两种模式,一是Module模式,二是Config模式;如果Module模式搜索失败,则会转入Config模式,搜索路径是/usr/local/lib/cmake/<libraryname>(find_package的搜索路径是一系列的集合)*)


The files in this tutorial are below:

Expand Down Expand Up @@ -40,6 +48,8 @@ As mentioned above the `find_package()` function will search for CMake modules i
format of the arguments to `find_package` will depend on the module you are looking
for. This is typically documented at the top of the `FindXXX.cmake` file.

(*find_package()参数的确切格式取决于要查找的模块,这通常记录在FindXXX.cmake文件的顶部*)

A basic example of finding boost is below:

[source,cmake]
Expand All @@ -49,10 +59,21 @@ find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)

The arguments are:

* Boost - Name of the library. This is part of used to find the module file FindBoost.cmake
* Boost - Name of the library. This is part of used to find the module file FindBoost.cmake(是用于查找模块文件FindBoost.cmake的一部分)

*Boost库是为C++语言标准库提供扩展的一些程序库的总称,由Boost社区组织开发、维护。Boost库可以与C++标准库完美共同工作,并为其提供扩展功能*

* 1.46.1 - The minimum version of boost to find

*需要的boost库的最低版本*

* REQUIRED - Tells the module that this is required and to fail if it cannot be found

*告诉模块这是必须的,如果找不到会报错*

* COMPONENTS - The list of components to find in the library.

*在Boost中要找的部分*

Boost includes can take more arguments and also make use of other variables.
More complex setups are provided in later examples.
Expand All @@ -63,6 +84,8 @@ More complex setups are provided in later examples.
Most included packages will set a variable `XXX_FOUND`, which can be used to check
if the package is available on the system.

(*大多数被包含(被找到)的包将设置变量XXX_FOUND,用于检查软件包在系统上是否可用*)

In this example the variable is `Boost_FOUND`:

[source,cmake]
Expand All @@ -75,25 +98,29 @@ else()
endif()
----

## Exported Variables
## Exported Variables(导出变量)

After a package is found it will often export variables which can inform the user
where to find the library, header, or executable files. Similar to the `XXX_FOUND`
variable, these are package specific and are typically documented at the top of the
`FindXXX.cmake` file.

(*找到包后,将自动导出变量,这些变量通常记录在FindXXX.cmake文件的顶部。这些变量可以通知用户在哪里可以找到库、头文件或可执行文件*)

The variables exported in this example include:

* `Boost_INCLUDE_DIRS` - The path to the boost header files.

In some cases you can also check these variables by examining the cache using
ccmake or cmake-gui.

## Alias / Imported targets
## Alias / Imported targets(别名/导入目标)

Most modern CMake libraries link:https://cmake.org/cmake/help/v3.6/prop_tgt/IMPORTED.html#prop_tgt:IMPORTED[export] +ALIAS+ targets in their module files.
The benefit of imported targets are that they can also populate include directories and linked libraries.

(*大多数现代CMake库在其模块文件(FindXXX.cmake)导出别名目标。导入目标的好处是它们也可以填充包含目录和链接的库。*)

For example, starting from v3.5+ of CMake, the
Boost module supports this. Similar to using your own ALIAS target for libraires, an +ALIAS+ in a module can make referencing found targets easier.

Expand All @@ -104,7 +131,7 @@ of the subsystem. For example you can use:
* `Boost::system` for the boost system library.
* `Boost::filesystem` for filesystem library.

As with your own targets, these targets include their dependencies, so linking against
As with your own targets, these targets include their dependencies(依赖关系), so linking against
`Boost::filesystem` will automatically add `Boost::boost` and `Boost::system` dependencies.

To link against an imported target you can use the following:
Expand Down
2 changes: 2 additions & 0 deletions 01-basic/I-compiling-with-clang/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ When building with CMake it is possible to set the C and C++ compiler. This exam
is the same as the link:../A-hello-cmake[hello-cmake] example except that it shows the most basic
method of changing the compiler from the default gcc to http://clang.llvm.org/[clang].

(*以下介绍了将编译器从默认的gcc更改为clang的最基本的方式*)

The files in this tutorial are below:

```
Expand Down
16 changes: 10 additions & 6 deletions 02-sub-projects/A-basic/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ $ tree
In this example I have moved the header files to a subfolder under each projects +include+
directory, while leaving the target include as the root +include+ folder. This is a good idea to prevent
filename clashes because you have to include a file like below:

*不是直接将头文件放在include下,而是放在include/header/header.h,并且将target的包含目录设置为~/include,这样在一个项目含有多个头文件时可以有效防止文件名冲突*

[source,cpp]
----
#include "sublib1/sublib1.h"
Expand All @@ -65,7 +68,7 @@ This also means that if you install your library for other users the default ins
## Adding a Sub-Directory

A CMakeLists.txt file can include and call sub-directories which include a CMakeLists.txt
files.
files.(利用 `add_subdirectory()` 函数一个CMakeLists.txt文件可以包含和调用一个含有CMakeLists.txt文件的子目录)

[source,cmake]
----
Expand All @@ -80,6 +83,7 @@ When a project is created using the `project()` command, CMake will automaticall
create a number of variables which can be used to reference details about the project.
These variables can then be used by other sub-projects or the main project. For example,
to reference the source directory for a different project you can use.
(当使用 `project()` 创建一个项目时,CMake会自动创建一系列可以用来引用该项目相关信息的变量。这些变量可以被子项目或主项目使用)

[source,cmake]
----
Expand All @@ -97,7 +101,7 @@ The variables created by CMake include:
|CMAKE_PROJECT_NAME |the name of the first project set by the `project()`
command, i.e. the top level project.

|PROJECT_SOURCE_DIR |The source director of the current project.
|PROJECT_SOURCE_DIR |The source directory of the current project.

|PROJECT_BINARY_DIR |The build directory for the current project.

Expand All @@ -111,7 +115,7 @@ In this example the binary directories created would be `sublibrary1_BINARY_DIR`

|=======================================================================

## Header only Libraries
## Header only Libraries(*只含头文件的库,利用INTERFACE*)

If you have a library that is created as a header only library, cmake supports the +INTERFACE+
target to allow creating a target without any build output. More details can be found from
Expand All @@ -134,12 +138,12 @@ target_include_directories(${PROJECT_NAME}
)
----

## Referencing Libraries from Sub-Projects
## Referencing Libraries from Sub-Projects(*引用子目录中的库*)

If a sub-project creates a library, it can be referenced by other projects by
calling the name of the target in the `target_link_libraries()` command. This
means that you don't have to reference the full path of the new library and it
is added as a dependency.
is added as a dependency.(*在某项目中创建的target名,可在子目录或主目录中调用,可以说其作用域为整个项目*)

[source,cmake]
----
Expand All @@ -150,7 +154,7 @@ target_link_libraries(subbinary
----

Alternatively, you can create an alias target which allows you to reference the
target in read only contexts.
target in read only contexts.(*当然target的别名同样是全局作用域,只不过是只读的*)

To create an alias target run:

Expand Down
2 changes: 1 addition & 1 deletion 03-code-generation/README.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= Code Generation

Code generation can be useful to create source code in different languages from a common description file. This can reduce the amount of manual code to write and increase interoperability.
Code generation can be useful to create source code in different languages from a common description file. This can reduce the amount of manual code to write and increase interoperability.(代码生成对于从公共描述文件创建不同语言的源代码非常有用。这可以减少要编写的手动代码量,并提高互操作性。)

Examples showing code generation using variables from CMake and also using some common tools.

Expand Down
Loading