Maximilian
Maximilian

Reputation: 1375

How to add the compiler command lines in the makefile to the source code

When I execute several different compilation commands via a makefile on one specific sourcecode (c-code). Is there a way to add these compilation commands as a comment to the source code for documentation reason?

Upvotes: 2

Views: 122

Answers (2)

Beta
Beta

Reputation: 99084

Maybe I misunderstand the question, but this doesn't seem too difficult:

foo.o: foo.cc
    @command="$(CXX) $(CPPFLAGS) -c $< -o $@"; echo $$command ;\
  sed -i .bak "1{x;s|^|//$$command|;G;}" $< ; \
  $$command

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409136

You can do it by adding a preprocessor macro defined as a string containing the compiler flags, and then use this macro in an assignment to a constant string pointer.

Something like this in the Makefile

$(CC) $(CFLAGS) -DCFLAGS="$(CFLAGS)" ...

And in one source file do e.g.

const char cflags[] = CFLAGS;

There is no generic way to get it as a part of a comment though.

You could have a special marker in a comment block in a source file, and replace that using e.g. sed in a POSIX enviromnent (like Linux or OSX).

Something like this:

sed -i.bak -e 's@// CFLAGS: .*$@// CFLAGS: $(CFLAGS)@' some_source_file.c

Upvotes: 3

Related Questions