C语言 如何调试“INT_MAX 未声明”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6801390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I debug "INT_MAX undeclared"
提问by Blub
printf(INT_MAX);
printf(INT_MAX);
limits.h is included, for some reason it's not working in this particular project. In my testbed, it just works. I have no idea how to approach this problem other than removing every single file in the entire project until it starts working. This would be an inhuman amount of work. How can I find this bug faster? What are common causes of this?
包括limits.h,由于某种原因它在这个特定项目中不起作用。在我的测试台上,它只是有效。除了删除整个项目中的每个文件直到它开始工作之外,我不知道如何解决这个问题。这将是一项不人道的工作。我怎样才能更快地找到这个错误?造成这种情况的常见原因是什么?
回答by
I assume you've searched your own code and build system for instances where INT_MAXmight get undefined.
我假设您已经搜索了自己的代码并构建系统以查找INT_MAX可能未定义的实例。
If possible, find a way to compile just the one source file (.c), so that your iteration speed goes up. It is enough if your Makefile(or whatever you use) already compiles only the changed file, instead of everything.
如果可能,找到一种方法只编译一个源文件 ( .c),这样您的迭代速度就会提高。如果您的Makefile(或您使用的任何东西)已经只编译了更改过的文件,而不是所有文件,这就足够了。
Then, in the problematic file, add this before the first #include, and after every #include:
然后,在有问题的文件中,在 first 之前#include和 every 之后添加#include:
#ifndef INT_MAX
#error INT_MAX not defined at this point
#endif
Then rebuild that module and see where the error happens.
然后重建该模块并查看错误发生的位置。
If that fails, add the code snippet above here and there in the code to see where it gets undefined.
如果失败,请在代码中各处添加上面的代码片段以查看未定义的位置。
If it turns out that your <limits.h>isn't defining it, there are at least two possibilities:
如果事实证明您<limits.h>没有定义它,那么至少有两种可能性:
- your platform does not conform to the C standard, at least in the way you are using it; perhaps you need to use a specific compiler switch to get the C standard?
- your code is including a different
limits.hthan the standard one
- 您的平台不符合 C 标准,至少在您使用它的方式上;也许您需要使用特定的编译器开关来获得 C 标准?
- 您的代码包含
limits.h与标准代码不同的代码
Happy hacking.
快乐黑客。
PS. If you need to go through the process of removing files to find the problem, note that you can use binary search to speed things up: first remove one half of the files, and if the problem persists, you know it's in the remaining files, and if not, in the files you removed, or, rarely, in the interaction between the two sets of files. Then iterate using the appropriate set of files, until you narrow it down to something sufficiently small.
附注。如果您需要通过删除文件的过程来查找问题,请注意您可以使用二进制搜索来加快速度:首先删除一半的文件,如果问题仍然存在,则您知道它在剩余的文件中,如果不是,则在您删除的文件中,或者很少出现在两组文件之间的交互中。然后使用适当的文件集进行迭代,直到将其缩小到足够小的范围。
回答by Keith Thompson
Make sure the include directive is
确保包含指令是
#include <limits.h>
not
不是
#include "limits.h"
Show us a complete program that exhibits the problem, along with the exact error message you're getting. Copy-and-paste both of them. And if you're calling printf(), don't forget the
向我们展示一个显示问题的完整程序,以及您收到的确切错误消息。复制并粘贴它们。如果您正在调用 printf(),请不要忘记
#include <stdio.h>
And try compiling and running this program:
并尝试编译并运行这个程序:
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf("INT_MAX = %d\n", INT_MAX);
return 0;
}
It should work correctly, with no warnings or error messages. If it doesn't, there may be something wrong with your compilation system, or with the way it's installed or configured, or with the way you're using it.
它应该可以正常工作,没有警告或错误消息。如果没有,则可能是您的编译系统、安装或配置方式或您使用它的方式有问题。
回答by R.. GitHub STOP HELPING ICE
This code will never work because printftakes a pointer to a format string as its first argument. INT_MAXis not a pointer to a format string but rather an integer.
这段代码永远不会工作,因为printf将指向格式字符串的指针作为它的第一个参数。INT_MAX不是指向格式字符串的指针,而是一个整数。
What you want is printf("%d", INT_MAX).
你想要的是printf("%d", INT_MAX).

