Qt 分段错误,除非我重建 [英] Qt segmentation fault unless I rebuild

查看:24
本文介绍了Qt 分段错误,除非我重建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 Qt 项目,并且经常在我进行大量更改和构建时,当我运行程序时,我会在 Qt 框架的某个地方遇到随机分段错误.

I'm working on a Qt project and often when I make a bunch of changes and build, when I run the program I get a random segmentation fault somewhere in the Qt framework.

要修复它,我必须重建它,然后它就会消失.

To fix it I have to rebuild and then it goes away.

我找到了另一个遇到这个问题的人,他们收到了这个答案:

I found another person who had this issue and they received this answer:

来自:Qt 应用程序框架中的分段错误

这听起来好像你的构建系统无法识别依赖和改变类定义没有触发重建应该是的东西重新编译时定义变化.

This makes it sound as though your build system isn't recognizing a dependency and that a change to that class definition isn't triggering a rebuild of something that should be recompiled when the definition changes.

确保类 LevelIndicator 是恰好在一个地方定义(通常那将是一个头文件被任何模块包含需要使用 LevelIndicator 对象).还要确保任何全局/静态LevelIndicator 对象的实例遵循一个定义规则.

Make sure class LevelIndicator is defined in exactly one place (generally that would be a header file that gets included by whatever modules need to use a LevelIndicator object). Also make sure that any global/static instances of LevelIndicator objects are following the one definition rule.

所以这对我来说是有意义的,但是每个类都只定义在一个地方,并使用#ifndef 保护来防止双重包含.那我该如何避免呢?

So that makes sense to me, however each class is defined in only one place, with #ifndef guards to prevent double inclusions. So how can I avoid this?

推荐答案

如果你更改了头文件但某些源文件没有重新构建,通常会发生这样的错误,例如通过向结构/类添加成员.QMake 的依赖处理在这方面是特殊的.如果您使用 INCLUDEPATH 变量包含来自其他目录的标头,则还必须将目录添加到 DEPENDPATH 变量以监视"它们以进行更改.否则 INCLUDEPATH 目录中的更改不会触发当前目录中的重建.它应该是这样的:

Usually such errors happen if you change header files but some source file isn't rebuilt, e.g. by adding members to structs/classes. QMake's dependency handling is peculiar in that regard. If you include headers from other directories using the INCLUDEPATH variable, you must also add the directory to the DEPENDPATH variable to have them "monitored" for changes. Otherwise changes in the INCLUDEPATH directories won't trigger rebuilds in the current directory. It should look like this:

 INCLUDEPATH += ../somelib/include
 DEPENDPATH += ../somelib/include

经验法则:如果您看到一个 INCLUDEPATH 指令指向您的项目中的路径而没有相应的 DEPENDPATH 指令,那几乎总是错误的.

Rule of thumb: If you see a INCLUDEPATH directive pointing to a path inside your project without corresponding DEPENDPATH directive, almost always that's wrong.

另请参阅 我对类似问题的回答.

这篇关于Qt 分段错误,除非我重建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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