开发者

Is there a portable way to print a message from the C preprocessor?

I would like to be able to do something like

#print "C Preprocessor got here!"
开发者_如何学运维

for debugging purposes. What's the best / most portable way to do this?


The warning directive is probably the closest you'll get, but it's not entirely platform-independent:

#warning "C Preprocessor got here!"

AFAIK this works on most compilers except MSVC, on which you'll have to use a pragma directive:

#pragma message ( "C Preprocessor got here!" )


The following are supported by MSVC, and GCC.

#pragma message("stuff")
#pragma message "stuff"

Clang has begun adding support recently, see here for more.


You might want to try: #pragma message("Hello World!")


Most C compilers will recognize a #warning directive, so

 #warning "Got here"

There's also the standard '#error' directive,

 #error "Got here"

While all compilers support that, it'll also stop the compilation/preprocessing.


#pragma message("foo")

works great. Also wouldn't stop compilation even if you use -Werror


Another solution is to use comments plus a shell script to process them. This takes some discipline (or a shell script which catches typos).

For example, I add comments formatted //TODO and then a shell script which collects all of them into a report.

For more complex use cases, you can try to write your own simple preprocessor. For example, you could edit your sources as *.c2 files. The simple preprocessor would read the source, look for //TODO, and write printf("TODO ...") into the output *.c file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜