开发者

Visual Studio 2010 C++ compiler - Get current line number when compiling

开发者 https://www.devze.com 2022-12-18 17:13 出处:网络
i have a question about how to get the current line number while compiling of the VS C++ compiler, IF its possible of course.

i have a question about how to get the current line number while compiling of the VS C++ compiler, IF its possible of course. I know its possible to use the LINE Macro from the preprocessor, but the results i get are not correct (well, at least not what i want).

Please tell me its possible :)

Thanks in advance

edit: I think i found my mistake with using the __LINE__ macro. I feel a kinda stupid now.. I think i have to go to bed (after some time you are not creating/adding anything new but destroying wh开发者_如何学Cat you have done so far). Problem solved, thanks all for your help!


Ok...to explain a bit better, as I think you have misunderstood the implications of the __LINE__ macro...

Consider three source files:

/* Source1.c */
...list of headers & functions ....
if (!(fp = fopen("foo.blah", "r"))){
   fprintf(stderr, "Error in %s @ line: %d: Could not open foo.blah\n", __FILE__, __LINE__);
}

/* Source2.c */
...list of headers & functions ....
if (!(p = (char *)malloc((10 * sizeof(char)) + 1)))){
   fprintf(stderr, "Error in %s @ line: %d: Could not malloc\n", __FILE__, __LINE__);
}

/* Source3.c */
...list of headers & functions ....
if (!(ptr = (char *)malloc((50 * sizeof(char)) + 1)))){
   fprintf(stderr, "Error in %s @ line: %d: Could not malloc\n", __FILE__, __LINE__);
}

Suppose those three files are compiled and linked into an executable called foo.exe and runtime errors appear, nitpicky aside, you would get:

Error in source2.c @ line 25: Could not malloc
Error in source1.c @ line 50: Could not open foo.blah
Error in source3.c @ line 33: Could not malloc

The total size of the project sources in terms of line count, does not mean that those lines are out of sync, regardless of what was pre-processed. I hope I have explained it somewhat easier for you to understand in aiding your reasoning behind the usage of the __LINE__ macro.

Hope this helps, Best regards, Tom.

0

精彩评论

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