Handmade Network»Forums
4 posts
How to change project directory in a makefile ?
I'm reaching here for help because I have wasted couple of hours in trying to accomplish this very simple thing.
Here is my makefile:
(ctags -R * - This line is because I use Vim editor)
1
2
3
4
5
6
7
8
9
cflags=-Od -MTd -nologo -fp:fast -fp:except- -Gm- -GR- -EHa- -EHsc -Zo -Oi -WX -W4 -wd4100 -wd4189 -wd4201 -wd4505 -wd4127 -FC -Zi
cvars=-D_CRT_SECURE_NO_WARNINGS -DFROMNOTHINGS_DEBUG=1 -DFROMNOTHINGS_COMPATIBILITY=0
link=-incremental:no -opt:ref
conlibs=user32.lib gdi32.lib opengl32.lib zlibstat.lib
all:
	ctags -R *
        pushd ..\build
	cl $(cflags) $(cvars) ..\code\win32_fromnothings.cpp /link $(link) $(conlibs)
        popd


It is very much the same as Casey's but there is one problem... How do I tell nmake to change project's directory?
In other words what I mean is how to 'pushd' and 'popd' as in Casey's bat file...
(And by the way I am new to nmake syntax.)

Appreciate your help!
Alex Baines
44 posts / 2 projects
How to change project directory in a makefile ?
Edited by Alex Baines on
Hi, I'm more used to GNU make, but I think something like this should work for nmake:

1
2
3
4
5
6
7
8
cflags=-Od -MTd -nologo -fp:fast -fp:except- -Gm- -GR- -EHa- -EHsc -Zo -Oi -WX -W4 -wd4100 -wd4189 -wd4201 -wd4505 -wd4127 -FC -Zi
cvars=-D_CRT_SECURE_NO_WARNINGS -DFROMNOTHINGS_DEBUG=1 -DFROMNOTHINGS_COMPATIBILITY=0
link=-incremental:no -opt:ref
conlibs=user32.lib gdi32.lib opengl32.lib zlibstat.lib

..\build\win32_fromnothings.exe: ..\code\*.cpp ..\code\*.h
	ctags -R *
	cl.exe $(cflags) $(cvars) ..\code\win32_fromnothings.cpp /link $(link) $(conlibs) /F $@


Basically, the target name "..\build\win32_fromnothings.exe" is the path to the file you want to be created. The file(s) after the colon are the files that the target depends on, in this case the all the .cpp and .h files in "..\code".

The rule uses $@ as a shorthand for the .exe name, along with cl.exe's /F flag to specify the output filename. More details on the shortcuts: https://msdn.microsoft.com/en-us/library/ms933742.aspx

Using the output file name as the target like this also means nmake.exe won't rebuild unless the timestamps of dependencies are newer than the target, so unless you modify one of the .cpp or .h files, nmake will probably (rightly) say nothing needs to be done.
If you want it to always build, then you can keep using "all" as the target name, and replace $@ with ..\build\win32_fromnothings.exe.

At least with GNU make, each line in the recipe part of the makefile runs in a separate shell, so if nmake is similar that might be why pushd and popd don't seem to work. You can usually write the rules so that changing directory isn't necessary, but if it is, then you might be able to run everything in the same shell with some bracket syntax if nmake also supports this:

1
2
something: some-dependency.cpp
	(cd /somewhere && cl.exe some-dependency.cpp)
4 posts
How to change project directory in a makefile ?
Well Thanks! It worked, but I had to specify a path for every file that cl.exe outputs...
-Fe..\build\win32_fromnothings.exe -Fo..\build\win32_fromnothings.obj -Fd..\build\win32_fromnothings.pdb in order to move them in the build directory.
Simply -F$@ expands properly but it says Invalid Numeric Argument with error code: D8021
If there was a more general option for the output I couldn't find it :)

Thanks!