Hi, I started playing with the C code of GNU Make only a few days ago, so take what I say with a grain of salt.
It you want to simply print out filename and line number, you could use the `$(warning ...)` function. If you want to actually substitute filename/line number in some string to use later, I see two ways to go: 1. Define your own built-in function -- you can model it after the `warning` function, see `void error (const floc *flocp, size_t len, const char *fmt, ...)` in `output.c`. As I was anyway planning to test defining a new built-in function I just wrote this post (https://drdv.github.io/blog/202512-make-adding-builtin-function/) In summary, you define in `function.c`: ``` static char * func_location (char *o, char **argv, const char *funcname UNUSED) { const floc *flocp = reading_file; if (flocp && flocp->filenm) { const char *mode = argv[0]; if (!strcmp(mode, "file")) { return variable_buffer_output(o, flocp->filenm, strlen(flocp->filenm)); } else if (!strcmp(mode, "line")) { char buf[21]; snprintf(buf, sizeof(buf), "%lu", flocp->lineno + flocp->offset); return variable_buffer_output(o, buf, strlen(buf)); } } return o; } ``` register `FT_ENTRY ("location", 1, 1, 1, func_location),` in `function_table_init`, compile and you are done. Above I use `flocp->lineno` and it doesn't account for empty/commented lines after the first line in a recipe (an empirical observation). 2. The second way I see is to have a `Makefile:` target in which you update the `Makefile` by substituting filename/line numbers for some placeholders (maybe using `awk`). Dimitar On Tue, 2 Dec 2025 at 11:15, Basile Starynkevitch <[email protected]> wrote: > Hello all, > > I am French and using GNU make (with GNU guile) version GNU Make 4.4.1 on > a Debian Testing (or Ubuntu) computer with x86-64. > > Is there a simple way to get the current line number (in a similar way of > __LINE__ in C++ code) in a GNUmakefile? > > Maybe adding a $(__LINE__) and $(__FILE__) GNUmakefile functions? > > In other words, adding a GNU make feature so that the following > GNUmakefile example: > > .PHONY: tell-lines > > tell-lines: > printf "tell-lines at %s:%d\n" $(__LINE__) $(__FILE__) > > > when invoked as make tell-lines is outputing > > tell-lines at GNUmakefile:4 > > > I could even try to implement it if adding that feature is simple (less > than a day of coding on Linux) if given some concrete guidance..... > > Respectful regards > > > NB: this is useful in the RefPerSys free software project - it has an ugly > GNUmakefile (and I don't want to use generators like cmake) > > -- > > Basile STARYNKEVITCH basile AT starynkevitch DOT net > 8 rue de la Faïencerie > http://starynkevitch.net/Basile/ > 92340 Bourg-la-Reine https://github.com/bstarynk > France > https://github.com/RefPerSys/RefPerSys > https://orcid.org/0000-0003-0908-5250 > > >
