如何在CMake中包含标题 [英] How to include headers in cmake

查看:57
本文介绍了如何在CMake中包含标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图向自己介绍cmake。我在Windows上使用它来生成MinGW Makefile。



我习惯于将c ++类头文件和源文件放在同一目录中,并按模块组织目录。 / p>

我希望我的cmake配置具有任何cpp依赖于其相应的h文件。另外,我希望h和cpp始终存在于同一目录中。



根据我的阅读,在cmake的add_executable行中,我列出了所有cpp我要编译的文件。



如果在我的add_executable行中列出了src / module1 / myclass.cpp,如果myclass.cpp列出了,则cmake将在其中查找myclass.h #include?它会使myclass.cpp依赖于myclass.h吗?这是我想要的行为。

解决方案


我希望我的cmake配置具有任何cpp


CMake会自动计算cpp和h文件之间的依赖关系。它通过使用平台构建工具来实现。例如,它知道如何告诉gcc生成依赖文件,然后可以处理该文件。在Visual Studio等其他平台上,CMake知道如何告诉cl.exe输出标头依赖项,并且它也可以解析输出。



您不需要这样做使CMake正确处理标头依赖项的任何特殊操作。您要做的就是确保在编译过程中找到了标头。


如果我的目录中列出了src / module1 / myclass.cpp add_executable行,如果myclass.cpp列出#include,则将在哪里查找myclass.h?


编译器将查找其默认位置,您可以通过运行 include_directories(...)命令(为整个CMakeLists.txt文件设置它们)或运行<$ c $来配置其他搜索路径c> target_include_directories(your_executable_target ...)仅为一个可执行文件设置它们。



如果头文件都与cpp一起存储文件,然后从cpp文件位置提供 #include 相对路径(然后使用 #include ... 而不是 #include< ...> ),则无需配置标头搜索路径:我知道搜索的所有编译器都相对于cpp文件为默认文件。


I am trying to introduce myself to cmake. I am using it on windows to produce MinGW Makefiles.

I am used to having a c++ classes header and source file in the same directory, and directories organized by modules.

I would like my cmake configuration to have any cpp be dependent on its corresponding h file. In addition I would like the h and cpp to always exist in same directory.

From what I have read, in cmake, in my add_executable line, I list all the cpp files I want to compile.

If I have src/module1/myclass.cpp listed in my add_executable line, where will cmake look for myclass.h if myclass.cpp lists #include ? Will it make myclass.cpp dependent on myclass.h? This is the behavior I would like.

解决方案

I would like my cmake configuration to have any cpp be dependent on its corresponding h file.

CMake will automatically work out the dependencies among your cpp and h files. It does this by using your platform build tools. For example, it knows how to tell gcc to generate dependency files, and then it can process that file. On other platforms like with Visual Studio, CMake knows how to tell cl.exe to output header dependencies and it can parse that output too.

You don't need to do anything special to get CMake to correctly handle your header dependencies. All you have to do is make sure the headers are found during compilation.

If I have src/module1/myclass.cpp listed in my add_executable line, where will cmake look for myclass.h if myclass.cpp lists #include ?

The compiler will look in its default locations, and you can configure additional search paths by running the include_directories(...) command (to set them for the whole CMakeLists.txt file) or by running target_include_directories(your_executable_target ...) to set them for just the one executable.

If your header files are all stored alongside the cpp files, and you give #include relative paths from the cpp file location (and you use #include "..." instead of #include <...>) then you don't need to configure header search paths: all compilers that I'm aware of search relative to the cpp file by default.

这篇关于如何在CMake中包含标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆