#include <stdio.h>
#define STRINGIZE_HELPER(x) #x
#define STRINGIZE(x) STRINGIZE_HELPER(x)
#define LINE_STR STRINGIZE(__LINE__)
int main(int, char**)
{
printf("%s\n", LINE_STR);
}
The preprocessor output is (skipping stdio.h stuff):
<stdio stuff skipped>....
}
#line 443 "d:\\microsoft visual studio .net\\vc7
\\include\\stdio.h"
#pragma pack(pop)
#line 447 "d:\\microsoft visual studio .net\\vc7
\\include\\stdio.h"
#line 449 "d:\\microsoft visual studio .net\\vc7
\\include\\stdio.h"
#line
26 "c:\\ccm_wa\\onscup\\phoenix\\pxpl\\code\\test_preproce
ssor\\test_strcat.cpp"
int main(int, char**)
{
printf("%s\n", "34");
}
The output of that program when executed is:
(__LINE__Var+1)
What's wrong and how to get the line number instead of
__LINE__Var+1 ?
Thanks.
<snip>
>
>
>What's wrong and how to get the line number instead of
>__LINE__Var+1 ?
>
>
>Thanks.
The problem here is that you have enabled Edit-and-Continue (/ZI) and for
this feature to work correctly the compiler needs to be able to adjust the
values of any uses of __LINE__ inside of a function if a user adds more
code while editing during a debug session. For example:
void f(S* pS;)
{
for (int i = 0; i < 10; i++) {
pS->m_data1 += i;
}
printf("Line = %d\n", __LINE__);
}
Imagine that when you run this code the program emits:
Line = 126
Then while debugging the user realizes that the bug they are trying to
track down is due to a field not
being updated: so they change the code to:
void f(S* pS;)
{
for (int i = 0; i < 10; i++) {
pS->m_data1 += i;
pS->m_data2 += i;
}
printf("Line = %d\n", __LINE__);
}
and continue debugging -- when the debugger adds in the changes the user
has made it needs to ensure that the usage of __LINE__ has the correct
value (127 instead of 126) so under Edit and Continue __LINE__ is
implemented as an offset from the first line of function. This is why if
you preprocess a file that uses __LINE__ and you have enabled Edit and
Continue you will see __LINE__Var+1 instead of just a number.
The workaround is to turn off Edit and Continue (/ZI) while preprocessing.
--
Jonathan Caves, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.