How can I pass a macro definition from “make” command line arguments (-D) to C source code?

Call the make command this way: make CFLAGS=-Dvar=42 And be sure to use $(CFLAGS) in your compile command in the Makefile. As @jørgensen mentioned, putting the variable assignment after the make command will override the CFLAGS value already defined in the Makefile. Alternatively, you could set -Dvar=42 in another variable than CFLAGS and then reuse … Read more

Function “patsubst” in Makefile

In fact all is explained in the doc: Finds whitespace-separated words in TEXT … means that one or more spaces have to separate the words. … that match PATTERN … means that it select only words that match a pattern (which can include some spaces). … and replaces them with REPLACEMENT. means that the selected … Read more

Compile multiple C files with make

The links posted are all good. For you particular case you can try this. Essentially all Makefiles follow this pattern. Everything else is shortcuts and macros. program: main.o dbAdapter.o gcc -o program main.o dbAdapter.o main.o: main.c dbAdapter.h gcc -c main.c dbAdapter.o dbAdapter.c dbAdapter.h gcc -c dbAdapter.c The key thing here is that the Makefile looks … Read more

undefined reference to `log’

I don’t know what the reason is, but if you move -lm to the end, it will compile. $ cc -o randomselection rfc3797.c MD5.c -lm rfc3797.c: In function ‘getinteger’: rfc3797.c:183:3: warning: format not a string literal and no format arguments [-Wformat-security]

How do you “echo” the last configure/make build –options within a source directory?

Incredibly, somehow everyone else missed the canonical way to do this, which has been around since 2 years before this thread began. 🙂 I wondered the same thing as the OP and was disappointed by the lack of proper (non-ugly) ways to do this when I read this thread. A few days later, while idly … Read more

tech