如何让编译器告诉我#define 值是什么文件?
我的代码正在链接到我公司开发的其他几个库,其中一个库正在重新定义 errno.h 中的几个值,我希望能够解决此问题,但是我无法找到确切的文件重新定义这些值,我想知道是否有办法让编译器告诉我文件何时定义了特定值。
My code is linking against several other libraries that are also developed at my company, one of these libraries is redefining several values from errno.h, I would like to be able to fix this, however I am having trouble finding the exact file that is redefining these values, I am want to know if there is a way to make the compiler tell me when a file has defined a particular value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以通过将
-include errno.h
添加到构建相关库的命令行来完成此操作。这是一个简单的例子。我有一个名为“file.c”的 C 程序:就是这样 - 然后我用以下内容进行编译:
然后,编译器警告:
这会告诉您错误的定义在哪里。
You can probably do it by adding
-include errno.h
to the command line that builds the library in question. Here's a quick example. I have a C program called "file.c":That's it - then I compile with:
And presto, a compiler warning:
That will tell you where your bad definitions are.
您是否尝试过使用
grep
进行搜索?Have you tried searching with
grep
?如果您不想在所有标头中搜索特定的 #define,您可以
在源模块中的每个 #include 之后使用,然后开始从下到上删除它们,看看您的定义来自哪里。
另外,您的编译器可能会告诉您#define 已被重新定义。打开所有警告。
If you don't want to search through all your headers for the particular #define, you could use
after each #include in your source module and then start removing them from the bottom up and see where your definitions come from.
Also, your compiler may tell you that a #define has been redefined. Turn all your warnings on.
对于 GCC,我做了类似的事情:
-dD 告诉 cpp 打印定义它们的所有定义。在 cpp 输出中还有包含文件名和行号的标记。
With GCC I did something similar with:
-dD tells cpp to print all defines where they were defined. And in the cpp output there are also markers for the include file names and the line numbers.
某些环境(我认为是 IDE)可能会将配置选项绑定到“项目设置”中,而不是使用配置标头。如果您与许多其他开发人员在一个不反对这种行为的地方合作,那么您也可以检查您的工具设置。
大多数编译器会告诉您问题出在哪里,您必须查看并思考诊断通知告诉您的内容。
除此之外,*nix/Windows 上的 grep/findstr 是你的朋友。
如果没有任何结果,则检查构建系统中的工具设置。
It is possible that some environments, I'm thinking IDE's here, have configuration options tied into the "project settings" rather than using a configuration header. If you work with a lot of other developers in a place where this behavior is NOT frowned on then you might also check your tool settings.
Most compilers will tell you where the problem is, you have to look and think about what the diagnostic notification is telling you.
Short of that, grep/findstr on *nix/Windows is your friend.
If that yields nothing then check for tool settings in your build system.
如果您右键单击用法并选择“转到定义”,某些 IDE 将跳转到正确的位置。
如果您确实遇到困难,另一个选择是编译器上的命令行选项。大多数编译器都可以选择输出编译 C++ 代码时生成的汇编程序。
您可以查看这个汇编程序(其中的注释让您知道 C++ 源文件中的相对行号)。您不必了解汇编器,但您可以看到编译器运行时使用了什么值以及包含哪些文件和定义。检查编译器的文档以了解要使用的确切选项
Some IDE's will jump to the correct location if you right click on the usage and select 'go to definition'.
Another option if you're really stuck is a command line option on the compiler. Most compilers have an option to output the assembler they generate when compiling C++ code.
You can view this assembler (which has comments letting you know the relative line number in the C++ source file). You don't have to understand the assembler but you can see what value was used and what files and definitions were included when the compiler ran. Check your compiler's documentation for the exact option to use