目前,我MYPROJECT_CURRENT_HEADERS在CMake中使用一个变量来列出所有标题.当我使用Qt时,我的CMakeLists.txt包含:
QT4_WRAP_CPP(MYPROJECT_CURRENT_MOC ${MYPROJECT_CURRENT_HEADERS})
Run Code Online (Sandbox Code Playgroud)
问题是所有标头都由moc处理,即使那些没有Q_OBJECT:所以它生成许多空文件.
是否有"grep"/检测文件是否包含字符串的解决方案Q_OBJECT,如果是这种情况,请将其添加到MYPROJECT_CURRENT_MOC?
谢谢
我不知道从列表中选择包含字符串的标题的简单命令,但您始终可以创建一个循环来查找所有此类标题:
set(HEADERS_HAVING_Q_OBJECT)
foreach(header ${MYPROJECT_CURRENT_HEADERS})
file(STRINGS "${header}" lines REGEX "Q_OBJECT")
if(lines)
list(APPEND HEADERS_HAVING_Q_OBJECT "${header}")
endif()
endforeach()
Run Code Online (Sandbox Code Playgroud)
但是这个解决方案有其自身的缺点:如果您将一个Q_OBJECT已过滤的文件添加到其中一个,则需要手动重新运行cmake.否则,在构建过程中将不会自动生成新文件的moc代码.
即将发布的CMake 2.8.6中有一个名为"AUTOMOC"的新目标属性可能会帮助你.
可以在此处找到此功能的测试(您可以将其用作指南或示例):
非常简单的CMakeLists.txt文件在这里:
如果您使用此功能,cmake将扫描Q_OBJECT的标题并自动为您运行moc.
如果你想在CMake 2.8.6的最终版本之前试用它,你可以在这里下载一个候选版本:
http://cmake.org/files/v2.8/?C=M;O=D
"-rc2"文件包含AUTOMOC属性.
以下是运行"cmake --help-property AUTOMOC"的帮助文本:
cmake version 2.8.6-rc2
AUTOMOC
Should the target be processed with automoc (for Qt projects).
AUTOMOC is a boolean specifying whether CMake will handle the Qt moc
preprocessor automatically, i.e. without having to use the
QT4_WRAP_CPP() macro. Currently Qt4 is supported. When this property
is set to TRUE, CMake will scan the source files at build time and
invoke moc accordingly. If an #include statement like #include
"moc_foo.cpp" is found, the Q_OBJECT class declaration is expected in
the header, and moc is run on the header file. If an #include
statement like #include "foo.moc" is found, then a Q_OBJECT is
expected in the current source file and moc is run on the file itself.
Additionally, all header files are parsed for Q_OBJECT macros, and if
found, moc is also executed on those files. The resulting moc files,
which are not included as shown above in any of the source files are
included in a generated _automoc.cpp file, which is
compiled as part of the target.This property is initialized by the
value of the variable CMAKE_AUTOMOC if it is set when a target is
created.
| 归档时间: |
|
| 查看次数: |
2485 次 |
| 最近记录: |