简体   繁体   English

隐藏<project> CMake 中的 _automoc 目标</project>

[英]Hide <project>_automoc targets in CMake

I am trying to use the CMAKE_AUTOMOC property to automatically find and compile mocable files.我正在尝试使用 CMAKE_AUTOMOC 属性来自动查找和编译可移动文件。

However, the command set( CMAKE_AUTOMOC ON ) also includes the generated _automoc.cpp file in the Visual Studio "Source Files" filter.但是,命令set( CMAKE_AUTOMOC ON )还包括 Visual Studio“源文件”过滤器中生成的 _automoc.cpp 文件。 This is a problem for two reasons:这是一个问题,原因有两个:

  • It creates the filter even if it was not used before, and therefore pollutes VS explorer.即使以前没有使用过,它也会创建过滤器,因此会污染 VS 资源管理器。
  • It adds an additionnal file that should not be manually modified to the solution, in the middle of other source files.它在其他源文件中间添加了一个不应手动修改到解决方案的附加文件。

I would like to know if it possible to:我想知道是否可以:

1) Prevent CMake from including this file to the Visual Studio filters. 1) 防止 CMake 将此文件包含到 Visual Studio 过滤器中。 I searched and found https://cmake.org/Bug/print_bug_page.php?bug_id=13788 .我搜索并找到了 https://cmake.org/Bug/print_bug_page.php?bug_id=13788 However using但是使用

SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(GLOBAL PROPERTY AUTOMOC_FOLDER automoc)

did not change anything to my problem.没有改变我的问题。

2) Remove a given entry from the.vcxproj.filters file using CMake, using a command similar to 2) 使用 CMake 从 .vcxproj.filters 文件中删除给定条目,使用类似于

source_group( "Source Files" FILES "filepath" )

which is used to add the entry "filepath" to the "Source Files" filter.用于将条目“文件路径”添加到“源文件”过滤器。

I am currently using CMake 3.5, VS 2015 and Qt 5.6.我目前正在使用 CMake 3.5、VS 2015 和 Qt 5.6。 Here is a shortened version of the CMake that reproduces the problem:这是重现该问题的 CMake 的缩短版本:

project( myproj )

# Some stuff to include Qt libraries
# ...

set( CMAKE_AUTOMOC ON )

# These 2 lines don't change anything
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(GLOBAL PROPERTY AUTOMOC_FOLDER automoc)

# Create project
add_executable( ${PROJECT_NAME} "main.cpp" )

In the VS filter named "Source Files", I can see main.cpp and myproj_automoc.cpp, which does not even exist before the first compilation (trying to open it with VS sends an error "Cannot open the file").在名为“Source Files”的 VS 过滤器中,我可以看到 main.cpp 和 myproj_automoc.cpp,它们在第一次编译之前甚至不存在(尝试使用 VS 打开它会发送错误“无法打开文件”)。 In myproj.vcxproj.filters there is an entry:在 myproj.vcxproj.filters 中有一个条目:

Include="C:\pathto\build\myproj_automoc.cpp">
<Filter>Source Files</Filter>

which shouldn't be here since I did not ask for it.它不应该在这里,因为我没有要求它。

Am I missing something?我错过了什么吗?

Thank you for your help!谢谢您的帮助!

I've had trouble getting this to work as documented as well. 我也很难使它按文档记载工作。 It looks like they renamed the variable in one of the releases. 看起来他们在其中一个版本中重命名了变量。 As of Cmake 3.0.2, you can do the following: 从Cmake 3.0.2开始,您可以执行以下操作:

cmake_minimum_required(VERSION 3.0.2)
project(MyProj CXX)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_property(GLOBAL PROPERTY AUTOGEN_TARGETS_FOLDER MyAutoMocFolder)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
add_executable(${PROJECT_NAME}
    ${MyProj_HEADERS}
    ${MyProj_SRCS}
    ${MyProj_QRC}
    ${MyProj_UI})

Note that you have to use set_property and the property name is now AUTOGEN_TARGETS_FOLDER. 请注意,您必须使用set_property,并且属性名称现在为AUTOGEN_TARGETS_FOLDER。

In Xcode, this puts the generated _automoc folders in the "MyAutoMocFolder" instead of littering the parent folders with them. 在Xcode中,这会将生成的_automoc文件夹放置在“ MyAutoMocFolder”中,而不是将父文件夹杂乱无章。 In Visual Studio the automoc folders in the folder as well. 在Visual Studio中,该文件夹中的automoc文件夹也是如此。

It doesn't however hide the project_automoc.cpp files that are generated. 但是,它不会隐藏生成的project_automoc.cpp文件。 To move those you have to define a source group, as Armand pointed out: 如Armand所指出,要移动那些对象,您必须定义一个源组。

source_group( MyAutoMocFolder FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_automoc.cpp ) 

As of CMake 3.9, you can use AUTOGEN_SOURCE_GROUP to filter MOC files.从 CMake 3.9 开始,您可以使用AUTOGEN_SOURCE_GROUP过滤 MOC 文件。

set(CMAKE_AUTOMOC ON)
set_property(GLOBAL PROPERTY AUTOGEN_SOURCE_GROUP "Generated Files")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM