How to get only file name in preprocessor?

If you are using GNU Make then you can simply pass -D BASE_FILE_NAME=\"$*.c\" in on the preprocessing stage of compilation (if you're doing them separately, or at compilation if in a single stage, which is the norm).

This depends upon the way you have your file names determined. Mine come from a list of plain file names and are prefixed with directories using functions in the makefile at a later stage.

IE, this works well for me, but your mileage may vary! :-)

A simplified version of my make "code" :

CLASSES = main.c init.c

PREPROCESSED = $(patsubst %.c,$(PPCDIR)/%.pp.c,$(CLASSES))

$(PREPROCESSED): $(PPCDIR)/%.pp.c: %.c $(ALLH) 
    $(GCC) $(GCCOPTS) -D BASE_FILE_NAME=\"$*\" -E $< > $@

The simply use BASE_FILE_NAME in your code as you like :-)


There is no known preprocessor macro that provides the functionality. Passing __FILE__ through a function seams like the only sensible option.


In reply to FredCooke above, you can exchange this line:

-D BASE_FILE_NAME=\"$*.c\"

With:

-D BASE_FILE_NAME=\"$(<F)\"

This will give you proper file name expansion, for .cpp as well.