15 ERROR_MACRO("Error is in %s on line %d\n",
16 __FILE__, __LINE__);
I am getting following output:
Error is in tmp.c on line 16
I am getting same output, even if I use the above line in this form :
15 ERROR_MACRO("Error is in %s on line %d\n", \
16 __FILE__, __LINE__);
Shouldn't I get "line 15" instead of "line 16" ?
What should I do to get "line 15" ?
__LINE__ always expands to the exact line number that it appears on. It's up to the compiler how it reports errors for code that spans multiple lines, but most compilers go by the line that the statement started on (since most errors cannot be localized to a single character).
There is no macro which can determine what line the current statement appears on, as preprocessing typically occurs before the compiler even starts thinking about statements.
move the ERROR_MACRO to line 15 in your code? __LINE__ is the line number within the current file. There is no (legal) way to change it unless you move your code...
If you insist upon using __LINE__ on the line following the call, then just do:
ERROR_MACRO("Err in %s on line %d\n",
__FILE__, __LINE__ - 1);
Better yet, why don't you just define a macro for your macro:
#define MY_ERR ERROR_MACRO("Err in %s on line %d", __FILE__, __LINE__)
Now you can just call a short MY_ERR; instead of worrying about line length restrictions (which is why you're doing this, I assume).
Related
I have in several C-sources trace statements, like
TRACE(23, "abc");
TRACE(24, "def");
The numbers 23 and 24 are identifiers counted out of an automatically generated list containing in each line one string
...
"abc"
"def"
...
"abc" is in line 23 and so I write 23 in the appropriate trace statement.
The preprocessor generates me this wanted output
trace(23);
trace(24);
I think it should be possible to automate it in that way that I only write
TRACE("abc");
TRACE("def");
During C preprocessing i want to exchange the strings with the appropriate line number of my generated file automatically, so that I get in the preprocessor output
trace(23);
trace(24);
I can write a function which returns me the line number 23 for the string "abc" but I need to activate it during the preprocessing process. Are there any preprocessor hooks or other ideas?
The preprocessor supplies the automagic macros __FILE__ and __LINE__ (and a few others) which you can use:
#include <stdio.h>
#define TRACE(m) fprintf(stderr, "%s,%d: %s\n", __FILE__ , __LINE__, m)
int main(void)
{
int a;
if(a) TRACE("a");
else TRACE("no");
TRACE("returning");
return 0;
}
I have this idea now: Let the preprocessor generate this output:
trace("abc");
trace("def");
Than write a tool (i.e. a bash script with awk) exchanging the strings with their line number from the generated list file:
...
"abc"
"def"
...
And finally let the compiler do his work. I am not really happy with it, because it needs to be adapted for each compiler. Any better idea?
This question already has answers here:
C/C++ line number
(10 answers)
Closed 6 years ago.
Is there any way to get the source code line number in C code in run time?
The thought behind it is, suppose a software is made in plain C language. the .exe file is distributed to a user who knows nothing about C. Now if there is any error, the user can see the line number and report the error to the manufacturer so that the debugging can be done at manufacturer site. I just to debug the run time errors and get the corresponding line numbers in the source code while running it. I am a beginner in these stuffs.
If you are using the GCC compiler, you can use the Standard Predefined Macro - __LINE__ as a line number placeholder.
The compiler will fill in the line number while compiling.
Eg :-
printf("%d",__LINE__);
Use gdb instead. But I guess it will work:
if(someThingsWrong())
printf("wrong at line number %d in file %s\n", __LINE__, __FILE__);
You can use the built-in macros __LINE__ and __FILE__, which always expand to the current file name and line number.
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argc<2)
{
printf("Error: invalid arguments (%s:%d)\n", __FILE__, __LINE__);
return 0;
}
printf("You said: %s\n", argv[1]);
return 0;
}
I am working on a complex code that use multi-process techniques.
I have 1 simulation process and 3 child processes.
The code compile with no errors and the software usually run OK and give good result. In some case, usually in long simulation runs, one of the child process send sigchld and the program is terminated.
Is there a way to force the child process to print the name of the last function being called before the error?
Is there a way to force the child process to print the line number of the last line being used before the error?
I am working with eclipse 3.8.1 under Ubuntu and the code is regular C.
I don't know if you can do what you are asking, but I would suggest adding debug statements using a macro such as:
#define DBG_ENABLED 1
#if DBG_ENABLED
#define DBG(...) \
do { \
printf("%s(%d) %s(): ", __FILE__, __LINE__, __func__); \
printf(__VA_ARGS__); \
printf("\n"); \
} while (0)
#else
#define DBG(...)
#endif
Then for example:
void YourFunction(int x, int y) {
DBG("x=%d y=%d", x, y);
. . .
DBG("ok");
}
This allows you to see what your program is doing, and it should help pinpoint the location of the problem. Then you can set DBG_ENABLED to 0 to remove the debug from your executable.
(Probably better to use fprintf() to print to a file rather than printf() if you are running long simulations.)
I am writing a program for an embedded ARM processor in C. I would like to see the source filename and line number in the logging statements.
As the compiled code has no knowledge of line numbers and source files, I am looking for ways to have this inserted automatically before / during the compile process.
Are there any standard tools or compiler features that I can use for this?
I am using GCC.
For example:
This is what I would write in the source file:
log("<#filename#> <#linenumber#> : Hello World");
This is what would actually get compiled:
log("Foobar.c 225 : Hello World");
Typically you'd do something like this:
// logging function
void log(const char * file, const int line, const char *msg)
{
fprintf(stderr, "%s:%d: %s\n", file, line, msg);
}
// logging macro - passes __FILE__ and __LINE__ to logging function
#define LOG(msg) do { log(__FILE__, __LINE__, msg) } while (0)
Then when you want to log something:
LOG("We made it to this point!");
which will then generate a log message such as:
foo.c:42: We made it to this point!
There is a standard set of predefined macros as part of the preprocessor: https://gcc.gnu.org/onlinedocs/gcc-4.9.0/cpp/Standard-Predefined-Macros.html
The macros you want to use are __FILE__ and __LINE__ which are the file name and line numbers.
I have a compiled version of the game 'Rogue', as well as its source code. Sometimes, at seemingly random times, the game will put up a non-descriptive error message.
Is there a way of compiling the source in an IDE, and getting the specific line number where the program fails when it is run in the IDE?
If you can edit the error messages to:
printf (stderr, "Blah error at %s (%d)\n", __FILE__, __LINE__);
you can get the exact location.
Since editing all the messages would be a big pain, I'd suggest you define a macro that does it:
#define MYERR(...) do { \
printf ("# %s (%d): ", __FILE__, __LINE__); \
printf (__VA_ARGS__); \
} while (0)
Replace all error messages calls to invoke this macro (thats much easier than appending to each message).