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

xto*_*ofl 32 c++ pragma visual-studio-2010 line-numbers predefined-macro

为了将"todo"项添加到我的代码中,我想在编译器输出中添加一条消息.
我希望它看起来像这样:

c:/temp/main.cpp(104): TODO - add code to implement this
Run Code Online (Sandbox Code Playgroud)

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

__LINE__宏似乎扩展到了一个int不允许写作的

#pragma message( __FILE__ "("__LINE__"): ..." )
Run Code Online (Sandbox Code Playgroud)

会有另一种方式吗?

Red*_*edX 41

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

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

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: "
Run Code Online (Sandbox Code Playgroud)

一旦定义,使用如下:

#pragma message(Reminder "Fix this problem!") 
Run Code Online (Sandbox Code Playgroud)

这将创建如下输出:

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

  • 告诉你不能在你的消息中输入“错误”或“警告”的说法至少可以说是可疑的...... (2认同)

Nec*_*lis 9

现在只是鞭打了它,它确实击败了我使用的旧解决方案#error:D

#define _STR(x) #x
#define STR(x) _STR(x)
#define TODO(x) __pragma(message("TODO: "_STR(x) " :: " __FILE__ "@" STR(__LINE__)))
Run Code Online (Sandbox Code Playgroud)

您可以根据自己的需要修改这种方式.其用法示例:

//in code somewhere
TODO(Fix this);
Run Code Online (Sandbox Code Playgroud)

控制台窗格中的输出:

1>TODO: Fix this :: c:\users\administrator\documents\visual studio 2008\projects\metatest\metatest\metatest.cpp@33
Run Code Online (Sandbox Code Playgroud)

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


Meh*_*dad 9

在 Visual C++ 上你可以这样做

#pragma message(__FILE__ "(" _CRT_STRINGIZE(__LINE__) ")" ": warning: [blah]")
Run Code Online (Sandbox Code Playgroud)

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

#define _CRT_STRINGIZE_(x) #x
#define _CRT_STRINGIZE(x) _CRT_STRINGIZE_(x)
Run Code Online (Sandbox Code Playgroud)