C++ 如何使用 #pragma message() 使消息指向文件 (lineno)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5966594/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:14:57  来源:igfitidea点击:

How can I use #pragma message() so that the message points to the file(lineno)?

c++visual-studio-2010pragmaline-numberspredefined-macro

提问by xtofl

In order to add 'todo' items into my code, I want to put a message in the compiler output.
I would like it to look like this:

为了将“待办事项”项添加到我的代码中,我想在编译器输出中放置一条消息。
我希望它看起来像这样:

c:/temp/main.cpp(104): TODO - add code to implement this

in order to make use of the Visual Studio build output functionality to navigate to the respective line by double-clicking it.

为了利用 Visual Studio 构建输出功能通过双击导航到相应的行。

But the __LINE__macro seems to expand to an int, which disallows writing

但是__LINE__宏似乎扩展为 an int,这不允许写入

#pragma message( __FILE__ "("__LINE__"): ..." )

Would there be another way?

会不会有别的办法?

回答by RedX

Here is one that allows you to click on the output pane:

这是一个允许您单击输出窗格的方法:

(There are also some other nice tips there)

(那里还有一些其他不错的提示)

http://www.highprogrammer.com/alan/windev/visualstudio.html

http://www.highprogrammer.com/alan/windev/visualstudio.html

 // Statements like:
 // #pragma message(Reminder "Fix this problem!")
 // Which will cause messages like:
 // C:\Source\Project\main.cpp(47): Reminder: Fix this problem!
 // to show up during compiles. Note that you can NOT use the
 // words "error" or "warning" in your reminders, since it will
 // make the IDE think it should abort execution. You can double
 // click on these messages and jump to the line in question.

 #define Stringize( L )     #L 
 #define MakeString( M, L ) M(L)
 #define $Line MakeString( Stringize, __LINE__ )
 #define Reminder __FILE__ "(" $Line ") : Reminder: "

Once defined, use like so:

定义后,像这样使用:

#pragma message(Reminder "Fix this problem!") 

This will create output like:

这将创建如下输出:

C:\Source\Project\main.cpp(47): Reminder: Fix this problem!

C:\Source\Project\main.cpp(47): 提醒:修复这个问题!

回答by Necrolis

just whipped this up now, and it sure beats my old solution of using #error:D

现在刚刚提出这个问题,它肯定胜过我使用的旧解决方案#error:D

#define _STR(x) #x
#define STR(x) _STR(x)
#define TODO(x) __pragma(message("TODO: "_STR(x) " :: " __FILE__ "@" STR(__LINE__)))

you can modify this how ever you like/to whatever suits your needs. An example of its usage:

您可以根据自己的喜好/任何适合您需要的方式修改它。其用法示例:

//in code somewhere
TODO(Fix this);

output in the console pane:

控制台窗格中的输出:

1>TODO: Fix this :: c:\users\administrator\documents\visual studio 2008\projects\metatest\metatest\metatest.cpp@33

only downside is you can't jump to the line of this (by double clicking the message in the console pane) using __pragma(but testing with #pragmait doesn't seem to be the case anyways...)

唯一的缺点是你不能跳转到这一行(通过双击控制台窗格中的消息)使用__pragma(但#pragma无论如何测试似乎不是这种情况......)

回答by dirkgently

This is an addendum to the answer for those who find it tedious to punch in #pragmadirectives every-time they need to put a bookmark in the code: You can save a few keystrokes by whipping up a macro to do this for you! While in general, you cannot have a #pragmadirective within macros, MS C/C++ compilers 2008 and above do support a special vendor-specific extension called the __pragmawhich can be used with macros. See Pragma Directives and the __Pragma Keyword.

对于那些发现#pragma每次需要在代码中添加书签时输入指令很乏味的人来说,这是一个答案的附录:您可以通过创建一个宏来为您执行此操作,从而节省一些击键次数!虽然一般来说,#pragma宏中不能有指令,但 MS C/C++ 编译器 2008 及更高版本确实支持一个特殊的供应商特定扩展,称为__pragma可以与宏一起使用。请参阅Pragma 指令和 __Pragma 关键字

I use something akin to the following on a daily basis:

我每天都使用类似于以下内容的东西:

#define STR2(x) #x
#define STR1(x) STR2(x)
#define LOC __FILE__ "("STR1(__LINE__)") : Warning Msg: "
#define WARNING_BUILDER(x) __FILE__ "("STR1(__LINE__)") : Warning Msg: " __FUNCTION__ " requires " #x
#define WREVIEW WARNING_BUILDER(review)
#define WUT WARNING_BUILDER(unit-testing)

#ifdef SPECIAL_WARNINGS
    #ifdef SPECIAL_WARNINGS_REVIEW
        #define MARK_FOR_REVIEW() do { \
                    __pragma(message( WREVIEW )) \
                } while (0)
    #else
        #define MARK_FOR_REVIEW 
    #endif

    #ifdef SPECIAL_WARNINGS_UNIT_TEST
        #define MARK_FOR_UNIT_TEST() do { \
                    __pragma(message( WUT )) \
                } while (0)
    #else
        #define MARK_FOR_UNIT_TEST 
    #endif
#endif


// uncomment/set in build-environment to enable special warnings
//#define SPECIAL_WARNINGS
#ifdef SPECIAL_WARNINGS
// uncomment/set in build-environment if you want only code review warnings
//#define SPECIAL_WARNINGS_REVIEW
// uncomment/set in build-environment if you want only unit-test warnings
//#define SPECIAL_WARNINGS_UNIT_TEST
#endif

int main()
{
MARK_FOR_REVIEW();
MARK_FOR_UNIT_TEST();
}

You can easily extend it to suit your needs and add more warnings. The good part of having such a system is that you can selectively turn-on say, only code-review items and not have to worry about anything else by setting the appropriate macro in the build settings.

您可以轻松扩展它以满足您的需求并添加更多警告。拥有这样一个系统的好处在于,您可以通过在构建设置中设置适当的宏来有选择地打开,例如仅代码项,而不必担心其他任何事情。

回答by Richard V Day

This one allows it to be used without #pragma (Microsoft specific I think) and when you click it takes you to the line since it shows the file and line number just like a regular err/warning message does since none of the other ones seem to do this. This used to work without the __pragma but newer versions of msvc require it. Ive been using it since sometime in the 90's. I use Visual Studio 2013

这个允许它在没有#pragma(我认为是微软特定的)的情况下使用,当你点击它时,它会带你到行,因为它显示文件和行号,就像常规的错误/警告消息一样,因为其他人似乎都没有去做这个。这曾经在没有 __pragma 的情况下工作,但较新版本的 msvc 需要它。我从 90 年代的某个时候开始使用它。我使用 Visual Studio 2013

#define MacroStr(x)   #x
#define MacroStr2(x)  MacroStr(x)
#define Message(desc) __pragma(message(__FILE__ "(" MacroStr2(__LINE__) ") :" #desc))

example :

例子 :

Message("Need to add unit testing here")

output: 1> c:\source\include\mithrilsoftware.h(180) :"Need to add unit testing here"

输出:1> c:\source\include\mithrilsoftware.h(180) :“需要在这里添加单元测试”

回答by user541686

On Visual C++ you can just do

在 Visual C++ 上你可以做

#pragma message(__FILE__ "(" _CRT_STRINGIZE(__LINE__) ")" ": warning: [blah]")

_CRT_STRINGIZEis often already defined in some header, but if it's not, you can define it:

_CRT_STRINGIZE通常已经在某些标题中定义,但如果没有,您可以定义它:

#define _CRT_STRINGIZE_(x) #x
#define _CRT_STRINGIZE(x) _CRT_STRINGIZE_(x)

回答by Jacob

Use the #token. I've posted an example from MSDN below:

使用#令牌。我在下面的 MSDN 上发布了一个示例:

// collisions.h
#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define __LOC__ __FILE__ "("__STR1__(__LINE__)") : Warning Msg: "

// collisions.cpp
#pragma message(__LOC__"Need to do 3D collision testing")