Search notes:
Predefined preprocessor macros __FUNCTION__, __FILE__, __LINE__
Note, __FUNCTION__
is not really a macro: the preprocessor has now concept of functions and thus doesn't not know in which function it is.
print_function.h
#include <stdio.h>
#define PRINT_FUNCTION(TXT) printf("%-10s %-10s: %4d: %s\n", __FUNCTION__, __FILE__, __LINE__, TXT);
funcs.c
#include "print_function.h"
#include "funcs.h"
void func_a(const char* txt) {
PRINT_FUNCTION(txt);
}
void func_b(const char* txt) {
char buf[100]; // beware the buffer overflows…
sprintf(buf, "txt = %s", txt);
PRINT_FUNCTION(buf);
}
main.c
#include "print_function.h"
#include "funcs.h"
int main() {
PRINT_FUNCTION("starting up");
func_a("abc");
func_b("def");
}
funcs.h
void func_a(const char* txt);
void func_b(const char* txt);
Makefile
all: print-functions
main.o: main.c print_function.h funcs.h
gcc -c main.c
funcs.o: funcs.c print_function.h funcs.h
gcc -c funcs.c
print-functions: funcs.o main.o
gcc funcs.o main.o -o print-functions