如何让编译器告诉我#define 值是什么文件?

发布于 2024-09-07 02:21:19 字数 112 浏览 3 评论 0原文

我的代码正在链接到我公司开发的其他几个库,其中一个库正在重新定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

揽清风入怀 2024-09-14 02:21:19

您可以通过将 -include errno.h 添加到构建相关库的命令行来完成此操作。这是一个简单的例子。我有一个名为“file.c”的 C 程序:

#define ESRCH 8

就是这样 - 然后我用以下内容进行编译:

cc -c -include errno.h file.c

然后,编译器警告:

file.c:1:1: warning: "ESRCH" redefined
In file included from /usr/include/errno.h:23,
                 from <command-line>:0:
/usr/include/sys/errno.h:84:1: warning: this is the location of the previous definition

这会告诉您错误的定义在哪里。

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":

#define ESRCH 8

That's it - then I compile with:

cc -c -include errno.h file.c

And presto, a compiler warning:

file.c:1:1: warning: "ESRCH" redefined
In file included from /usr/include/errno.h:23,
                 from <command-line>:0:
/usr/include/sys/errno.h:84:1: warning: this is the location of the previous definition

That will tell you where your bad definitions are.

黯然 2024-09-14 02:21:19

您是否尝试过使用 grep 进行搜索?

Have you tried searching with grep?

猫烠⑼条掵仅有一顆心 2024-09-14 02:21:19

如果您不想在所有标头中搜索特定的 #define,您可以

#undef YOUR_MANIFEST_CONSTANT

在源模块中的每个 #include 之后使用,然后开始从下到上删除它们,看看您的定义来自哪里。

另外,您的编译器可能会告诉您#define 已被重新定义。打开所有警告。

If you don't want to search through all your headers for the particular #define, you could use

#undef YOUR_MANIFEST_CONSTANT

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.

溇涏 2024-09-14 02:21:19

对于 GCC,我做了类似的事情:

g++ input.cc -dD -E > cpp.out

-dD 告诉 cpp 打印定义它们的所有定义。在 cpp 输出中还有包含文件名和行号的标记。

With GCC I did something similar with:

g++ input.cc -dD -E > cpp.out

-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.

⒈起吃苦の倖褔 2024-09-14 02:21:19

某些环境(我认为是 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.

呆橘 2024-09-14 02:21:19

如果您右键单击用法并选择“转到定义”,某些 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文