so, suppose I have a file file.c, and a file anothr.c. I would like to set the compilation command for each of those files separately, say: gcc -Wall -O3 -o f file.c
, and gcc -Wall -std=c99 -o a another.c
. How can I set the gcc
command for that buffer so that every time I open it, it knows how to compile it? Is there something with the // -*-
directive or something like that? Thanks.
Asked
Active
Viewed 789 times
5

Dervin Thunk
- 19,515
- 28
- 127
- 217
2 Answers
5
Yes, you can use the directive in the file, and also set other value. Try this in line one or two:
// -*- compile-command: "gcc -Wall -O3 -o f file.c" -*-
and then re-load the file with C-x v
so that the setting takes effect.
I sometimes set things like c-indent-level: 4; c-basic-offset: 4;
in there too.

Dirk Eddelbuettel
- 360,940
- 56
- 644
- 725
-
`C-x v` apparently means `revert-buffer`. Not a default binding for me. – Felipe Cortez Nov 29 '20 at 13:00
-
1It might be mode dependent. I think I (decades ago) set programming mode to derive from text mode. – Dirk Eddelbuettel Nov 29 '20 at 14:56
3
What you're looking for are called file local variables, or sometimes just "local variables", per the in-comment declarative format described here.
In addition to the syntax given in Dirk's answer, you can write a "local variables" block at the end of your file:
/* Local Variables: */
/* compile-command:"gcc -Wall -O3 -o f file.c" */
/* End: */
You can use the interactive function add-file-local-variable
to help maintain this list if you don't want to type each entry manually.