C++ 如何在 CMake 中明确指定树外源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35260552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I explicitly specify an out-of-tree source in CMake?
提问by user3063750
I looked at this post: Using CMake to statically link to a library outside of the project. But I'm still having trouble interpreting what this means:
我看了这篇文章:Using CMake to statically link to a library of the project。但我仍然无法解释这意味着什么:
add_subdirectory(/path/to/the/library/source/directory subproject/grzeslib)
I'm assuming "/path/to/the/library/source/directory" means the path from the hard drive, but I don't understand what "subproject/grzeslib" means. Now I tried:
我假设“/path/to/the/library/source/directory”是指来自硬盘的路径,但我不明白“subproject/grzeslib”是什么意思。现在我试过:
include_directories(../path/to/dir)
add_subdirectory (../path/to/dir .)
But I'm getting an elaborate warning. Is there a better way to do this?
但是我收到了一个详细的警告。有一个更好的方法吗?
回答by FrankM
The second parameter is output directory for the results of the targets from that subdirectory.
第二个参数是该子目录中目标结果的输出目录。
From the documentation here: https://cmake.org/cmake/help/v3.3/command/add_subdirectory.html
从这里的文档:https: //cmake.org/cmake/help/v3.3/command/add_subdirectory.html
add_subdirectory
Add a subdirectory to the build.
add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
Add a subdirectory to the build.
- The source_dir specifies the directory in which the source CMakeLists.txt and code files are located. If it is a relative path it will be evaluated with respect to the current directory (the typical usage), but it may also be an absolute path.
- The binary_dir specifies the directory in which to place the output files. If it is a relative path it will be evaluated with respect to the current output directory, but it may also be an absolute path. If binary_dir is not specified, the value of source_dir, before expanding any relative path, will be used (the typical usage).
- The CMakeLists.txt file in the specified source directory will be processed immediately by CMake before processing in the current input file continues beyond this command.
- If the EXCLUDE_FROM_ALL argument is provided then targets in the subdirectory will not be included in the ALL target of the parent directory by default, and will be excluded from IDE project files. Users must explicitly build targets in the subdirectory. This is meant for use when the subdirectory contains a separate part of the project that is useful but not necessary, such as a set of examples. Typically the subdirectory should contain its own project() command invocation so that a full build system will be generated in the subdirectory (such as a VS IDE solution file). Note that inter-target dependencies supercede this exclusion. If a target built by the parent project depends on a target in the subdirectory, the dependee target will be included in the parent project build system to satisfy the dependency.
添加子目录
将子目录添加到构建中。
add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
将子目录添加到构建中。
- source_dir 指定源 CMakeLists.txt 和代码文件所在的目录。如果它是一个相对路径,它将相对于当前目录(典型用法)进行评估,但它也可能是一个绝对路径。
- binary_dir 指定放置输出文件的目录。如果它是相对路径,它将相对于当前输出目录进行评估,但它也可能是绝对路径。如果未指定 binary_dir,则在展开任何相对路径之前,将使用 source_dir 的值(典型用法)。
- CMake 将立即处理指定源目录中的 CMakeLists.txt 文件,然后在当前输入文件中的处理继续超出此命令。
- 如果提供了 EXCLUDE_FROM_ALL 参数,则默认情况下子目录中的目标将不会包含在父目录的 ALL 目标中,并且将从 IDE 项目文件中排除。用户必须在子目录中明确构建目标。这意味着在子目录包含有用但不必要的项目的单独部分时使用,例如一组示例。通常子目录应该包含自己的 project() 命令调用,以便在子目录中生成完整的构建系统(例如 VS IDE 解决方案文件)。请注意,目标间依赖项取代了此排除项。如果父项目构建的目标依赖于子目录中的目标,则依赖目标将包含在父项目构建系统中以满足依赖关系。
回答by Joel
From the documentation
从文档
Add a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.txt and code files are located. If it is a relative path it will be evaluated with respect to the current directory (the typical usage), but it may also be an absolute path. The binary_dir specifies the directory in which to place the output files. If it is a relative path it will be evaluated with respect to the current output directory, but it may also be an absolute path. If binary_dir is not specified, the value of source_dir, before expanding any relative path, will be used (the typical usage). The CMakeLists.txt file in the specified source directory will be processed immediately by CMake before processing in the current input file continues beyond this command
将子目录添加到构建中。source_dir 指定源 CMakeLists.txt 和代码文件所在的目录。如果它是一个相对路径,它将相对于当前目录(典型用法)进行评估,但它也可能是一个绝对路径。binary_dir 指定放置输出文件的目录。如果它是相对路径,它将相对于当前输出目录进行评估,但它也可能是绝对路径。如果未指定 binary_dir,则在展开任何相对路径之前,将使用 source_dir 的值(典型用法)。指定源目录中的 CMakeLists.txt 文件将被 CMake 立即处理,然后在当前输入文件中的处理继续超出此命令
From your example, all the binaries created in "/path/to/the/library/source/directory" will be placed in "subproject/grzeslib", it's a good thing to keep "clean" the source dirs.
从您的示例中,在“/path/to/the/library/source/directory”中创建的所有二进制文件都将放置在“subproject/grzeslib”中,保持“干净”源目录是一件好事。