Reputation: 743
I am unable to write a makefile that works. I have read the following tutorial (*) and I see that we can write simple files such as:
program : program.cpp
g++ -o program program.cpp -lm
(*) http://www.pma.caltech.edu/~physlab/make.html
I tried to adpt the example to suit my own needs, but it is not working:
interpreter: gvr_v51.c gvr_v51_interpreter.h
gcc gvr_v51_interpreter.h gvr_v51.c -pedantic -Wall -std=c99 -o gvr_v51 -lSDL
parser: gvr_v51_parser.h gvr_v51.c
gcc gvr_v51_parser.h gvr_v51.c -pedantic -Wall -std=c99 -o gvr_v51 -lSDL
What I need to achieve is to be able to allow the user to compile by typing either "make interpreter" or "make parser". The difference between the two is that the first will include the header gvr_v51_interpreter.h, while the other will include the header gvr_v51_parser.h
Both header files are identical, except for one #define line that holds different value in the two files. The remaining contents of the header files include the declaration of structs, enums, as well as functions prototypes.
Is what I want to achieve even possible? If so, could you please tell me what I am doing wrong?
Thanks.
Upvotes: 0
Views: 69
Reputation: 99084
You can include header files conditionally without altering the source file (gvr_v51.c
):
interpreter: gvr_v51.c gvr_v51_interpreter.h
gcc -include gvr_v51_interpreter.h gvr_v51.c -pedantic ...
parser: gvr_v51_parser.h gvr_v51.c
gcc -include gvr_v51_parser.h gvr_v51.c -pedantic ...
Once that's working, there are several ways to improve it. In particular, I urge you not to have two rules that build gvr_v51
, neither of them called gvr_v51
.
Upvotes: 1
Reputation: 319
You can't include header files by appending them to the list of c files to compile. However you can include a header depending on the definition of a macro and predefine it with a gcc option.
In gvr_v51.c:
#ifdef INTERPRETER
#include "gvr_v51_interpreter.h"
#else
#include "gvr_v51_parser.h"
#endif
In the Makefile:
interpreter:
gcc -lSDL -DINTERPRETER -o gvr_v51 gvr_v51.c
parser:
gcc -lSDL -o gvr_v51 gvr_v51.c
Upvotes: 1