在编译期间使用自定义消息转到源代码?

发布于 2024-10-10 21:19:06 字数 470 浏览 2 评论 0原文

在 Visual Studio 中,我可以在源代码中包含以下宏,并在构建过程中打印出消息。然后,我可以双击构建消息窗口中的消息并转到源代码行。

#define _QUOTE(x) # x
#define QUOTE(x) _QUOTE(x)
#define __FILE__LINE__ __FILE__ "(" QUOTE(__LINE__) ") : "
#pragma message ( __FILE__LINE__ "Notify user of something in code" )

是否可以在 Builder C++ 中执行相同的操作?

我认为不是,因为 Builder C++ 中的构建消息窗口中似乎有更多信息,允许“查看源代码”选项或双击命令起作用。

C++ Builder XE。

我在这个问题中包含了 Delphi 标签,因为许多 Delphi 用户也使用 Builder C++。

In Visual Studio I can include the following macros in the source and during build the message is printed out. I can then double click on the message in the build message window and go to the line of source.

#define _QUOTE(x) # x
#define QUOTE(x) _QUOTE(x)
#define __FILE__LINE__ __FILE__ "(" QUOTE(__LINE__) ") : "
#pragma message ( __FILE__LINE__ "Notify user of something in code" )

Is it possible to do the same in Builder C++?

I think not, as there seems to be more information in the build messages window in Builder C++ that allows the 'view source' option or double click command to work.

C++ Builder XE.

I have included the Delphi tag with this question as lots of Delphi users also use Builder C++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

安稳善良 2024-10-17 21:19:06

C++ Builder 中的等效项是#warning 指令。该行:

#warning Test warning message here

在消息窗格中显示以下内容:

消息窗格中的警告消息

这与任何其他编译器消息类似,双击它会将您带到代码行。

__FILE____LINE__ 宏不会在您使用 #warning 定义的消息内展开 - 它获取文本并完全按照编写的内容吐出。但是,您不需要使用它们,因为发出的消息无论如何都包含文件和行号。

如果您想写出错误消息(Delphi 允许您 - $MESSAGE 根据记忆具有提示、警告或错误级别),您可以使用 #error。它的工作方式与 #warning 相同,并在该行停止编译,就像任何其他错误编译一样,因此

#error This is an error message

给出

“消息窗格中出现错误消息”

我正在使用 C++ Builder 2010,但我我相当确定这些指令适用于许多版本。

(顺便说一句,将 C++ Builder 问题标记为“delphi”通常没问题,因为有关 IDE 或 VCL 的许多问题将由两个社区同等回答。我一直这样做。这可能不是这些问题之一,因为Delphi 人员不太可能了解特定的 C++ Builder 编译器指令,单独标记“c++-builder”就可以了。)

The equivalent in C++ Builder is the #warning directive. The line:

#warning Test warning message here

Shows the following in the Messages pane:

A warning message in the Messages pane

This acts like any other compiler message, and double-clicking it takes you to the line of code.

The __FILE__ and __LINE__ macros do not expand inside a message you define using #warning - it takes text and spits it out exactly as written. However, you don't need to use them since the message that's emitted includes the file and line number anyway.

If you want to write out an error message (as Delphi allows you to - $MESSAGE has a level, from memory, of hint, warning or error) you can use #error. It works the same as #warning and stops compilation at that line just like any other error compiling, so

#error This is an error message

gives

An error message in the Messages pane

I'm using C++ Builder 2010, but I'm moderately sure that these directives have worked for many versions.

(By the way, tagging a C++ Builder question 'delphi' is normally fine, since many questions about the IDE or VCL will be equally answerable by both communities. I do it all the time. This is probably not one of those questions, since Delphi people are unlikely to know about specific C++ Builder compiler directives. Tagging 'c++-builder' by itself is fine.)

塔塔猫 2024-10-17 21:19:06

在 Delphi 中,您可以包含消息指令。例如:

{$MESSAGE WARN 'To be or not to be'}

这会在构建消息中输出警告。该构建消息与任何其他编译器错误/警告/提示一样可单击,单击它会将您带到源代码中 {$MESSAGE ...} 指令的位置。

我不知道,因为我不使用 C++ 构建器,但我假设 C++ 构建器支持类似的技术......

In Delphi you can include a message directive. For example:

{$MESSAGE WARN 'To be or not to be'}

Which would output a warning in the build messages. That build message is just as clickable as any other compiler error/warning/hint and clicking it will take you to the location of the {$MESSAGE ...} directive in the source.

I do not know as I do not use C++ builder, but I would assume that C++ Builder supports a similar technique...

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