CMake未检测到GCC包含的标头(使用-include)更改 [英] GCC included header (using `-include`) changes not detected by CMake

查看:166
本文介绍了CMake未检测到GCC包含的标头(使用-include)更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观察到奇怪的CMake行为。如果我的项目使用 -include inc.h 通过 ADD_COMPILE_OPTIONS(-include inc.h)命令然后更改为从未检测到的标头。这意味着我可以更改标题,但是CMake绝不会尝试重新编译 main.cpp 。难道我做错了什么?这是CMake错误吗?有解决方法吗?

I'm observing strange CMake behavior. If my project has a header file included to all sources using -include inc.h by means of ADD_COMPILE_OPTIONS(-include inc.h) command then changes to the header never detected. Meaning I can change the header, but CMake will never try to recompile the main.cpp. Am I doing something wrong? Is it a CMake bug? Any workaround?

CMakeLists.txt

CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 3.12)
PROJECT(include_test)

SET(CMAKE_CXX_STANDARD 17)

INCLUDE_DIRECTORIES(.)
ADD_COMPILE_OPTIONS(
        -include inc.h
)
ADD_EXECUTABLE(include_test main.cpp)

main.cpp

#include <iostream>

int main()
{

    foo a;
    std::cout << a.bar << std::endl;
    return 0;
}

inc.h

struct foo
{
    int bar = 1;
    double baz = 3;
};

EDIT001:
如@Oliv所建议的,当尝试使用<$ c $之类的东西时c> SET_SOURCE_FILES_PROPERTIES(main.cpp PROPERTY OBJECT_DEPENDS inc.h)
当然,它是行不通的,因为依赖关系应该是目标而不是cpp所依赖的文件,所以我添加了以下内容:

EDIT001: As @Oliv suggests, when trying to use something like SET_SOURCE_FILES_PROPERTIES(main.cpp PROPERTY OBJECT_DEPENDS inc.h) Of course it wouldnt work because dependency should be a target and not a file on which the cpp depends so I added following:

ADD_CUSTOM_TARGET(HeaderChanged
                  DEPENDS
                  inc.h
                  COMMENT "Checking if include file has changed")

SET_SOURCE_FILES_PROPERTIES(main.cpp PROPERTY OBJECT_DEPENDS HeaderChanged)

仍然会导致 make [2]:***'CMakeFiles / include_test.dir / main.cpp.o'不需要建立目标'HeaderChanged'的规则。停止。尽管存在 HeaderChanged 目标

which still results in make[2]: *** No rule to make target 'HeaderChanged', needed by 'CMakeFiles/include_test.dir/main.cpp.o'. Stop. despite the HeaderChanged target exists

推荐答案

在EDIT0001上:您可能需要指定 inc.h 的完整路径,以使Makefile知道在哪里可以找到它(因为您很可能在外面源构建版本),即:

On your EDIT0001: You probably need to specify the entire path to inc.h such that the Makefile knows where to find it (since you're most likely doing an out of source build), i.e:

SET_SOURCE_FILES_PROPERTIES(main.cpp属性对象$ {CMAKE_CURRENT_LIST_DIR} /inc.h)

还要注意, OBJECT_DEPENDS 解决方案不适用于Visual Studio(标志<$ c也不适用) $ c> -include )。但是在Visual Studio中,添加 ADD_COMPILE_OPTIONS(/FIinc.h)就足够了,它将自动检测对 inc.h

Also note that the OBJECT_DEPENDS solution won't work for Visual Studio (neither does the flag -include). But in Visual Studio it was sufficient to add the ADD_COMPILE_OPTIONS(/FIinc.h) and it would automatically detect changes to inc.h.

这篇关于CMake未检测到GCC包含的标头(使用-include)更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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