[mingw g++] makefile target without exe extension

Feb 12, 2021 at 11:39pm
Let's say I have a makefile such as the following:

1
2
my_program.exe : source.cpp other_dependency.cpp
	g++ source.cpp other_dependency.cpp -o my_program

(command simplified for brevity)

If I type "make my_program", it will NOT use the my_program.exe target because the target name includes the .exe, which I did not type.

Okay then, the solution is to rename the target:
1
2
my_program : source.cpp other_dependency.cpp
	g++ source.cpp other_dependency.cpp -o my_program


Now I can type "make my_program" and everything works. Except, the new issue is that because the target is not the name of a file, it will never say "make: 'my_program' is up to date." even though I didn't modify a dependency.

That brings me to my question: How can I get make to still only rebuild when necessary, but use target names that aren't necessarily the name of a file. Do I have to use dummy files that get deleted and re-written to? This seems awfully inconvenient, so I'm sure there has to be better way.

PS: This would also apply to targets that I want to be "groups", e.g. make all_debug which will only run if the debug exe's are out of date, for instance.
Last edited on Feb 12, 2021 at 11:41pm
Feb 12, 2021 at 11:47pm
How about:

1
2
3
4
my_program : my_program.exe

my_program.exe : source.cpp other_dependency.cpp
	g++ $^ -o $@

Feb 12, 2021 at 11:54pm
Thank you, I was actually just going down that path myself.

I had tried what you just wrote, but you also need to make sure your line 2 is indented (the line after the first target)... otherwise you get some cryptic error.

Anyway, guess that solves that. Thanks again.
Last edited on Feb 12, 2021 at 11:55pm
Feb 13, 2021 at 12:13am
Interesting. My test seems to work with or without an explicit empty recipe.
But if you do need an empty recipe, then perhaps a better way to write one is like this:

 
my_program : my_program.exe ;

The first line of a recipe can be put on the target/prereq line after a semicolon.
(I've been going through the make documentation over the last week!)
Feb 13, 2021 at 4:02am
Beware that since a file named my_program is never created, the target will always be out-of-date.

Tell make that my_program doesn't name a file by marking it a prerequisite of the .PHONY target
.PHONY: my_program

You'll probably get away without this, but it's a good idea.
Feb 13, 2021 at 4:42am
Good point. It should definitely be .PHONY. I didn't notice that.
Feb 13, 2021 at 12:58pm
Beware that since a file named my_program is never created, the target will always be out-of-date.
Seemed to correctly be up-to-date and out-of-date for me, but I will follow your advice and do the .PHONY method, thanks.
Feb 13, 2021 at 11:58pm
I think he's saying that the "my_program" target will always be "out of date" since a file with that name is never created. That's not a problem, though, since it's only purpose is to cause the creation of "my_program.exe", which will not happen if it's up-to-date. Marking "my_program" as .PHONY just ensures that any actual program called "my_program" (unlikely as it is to exist) is ignored.
Topic archived. No new replies allowed.