Using nmake for program slightly more complicated than hello world

114 Views Asked by At

I have Visual Studio 2022 Community and have downloaded some c astronomy software, NOVAS, from the US Naval Observatory. I have programmed in c years ago but am not up on compiling with the command prompt in modern Visual Studio. All the tutorials I find online jump from "hello world" to a complicated example creating all kinds of macros and locating files all over creation.

I would like a simple makefile for nmake that follows this step in the directions from the US Naval Observatory (page C-27):

b. Compile and link files checkout-stars.c, novas.c, novascon.c, nutation.c, solsys3.c, and readeph0.c. Name the resulting application checkout-stars

Each Some of these C files, with the exception of checkout-stars.c is are accompanied by a .h file. All of the files have seemingly appropriate #include directives. The purpose of the program is to exercise many of the functions in the package to show that the package has been properly compiled.

My reason for trying nmake rather than compiling directly on the command line is I expect the command line invocation would be very long. Also, I want it to be easy to modify as I perform more sophisticated testing and then develop my own programs using this package.

2

There are 2 best solutions below

3
Joseph Quinsey On BEST ANSWER

For the simplest makefile, just use the standard built-in rules. For example, to create checkout-stars.exe, the following nmake makefile will suffice:

TARGET  = checkout-stars.exe
SOURCES = checkout-stars.c novas.c novascon.c nutation.c solsys3.c readeph0.c
OBJECTS = $(SOURCES:.c=.obj)
CFLAGS  = -W3 -O2 -nologo

$(TARGET): $(OBJECTS)
    link -nologo $** 
$(OBJECTS): *.h
clean:
    del /q $(OBJECTS) $(TARGET) 

If you modify any local header file (*.h), then all the given source files will be recompiled; this is perhaps overbroad, but easy to state.

To use, at a VS developer command prompt type nmake to compile and link to create the exe. To remove the temporary files created, type nmake clean.

0
Gerard Ashton On

Inspired by an example at https://w3.cs.jmu.edu/bernstdh/web/common/help/msc_nmake.php I created the following makefile

checkout-stars.exe: checkout-stars.obj novas.obj novascon.obj nutation.obj solsys3.obj readeph0.obj
     link checkout-stars.obj novas.obj novascon.obj nutation.obj solsys3.obj readeph0.obj

checkout-stars.obj: checkout-stars.c *.h
     cl /c checkout-stars.c

novas.obj: novas.c *.h
     cl /c novas.c

novascon.obj: novascon.c *.h
     cl /c novascon.c

nutation.obj: nutation.c *.h
     cl /c nutation.c

solsys3.obj: solsys3.c *.h
     cl /c solsys3.c

readeph0.obj: readeph0.c *.h
     cl /c readeph0.c

In response to the comment by Joseph Quinley I made all the .obj files dependent on all the .h files in case they included any of them.

With all the files in the same directory, and using the VS developer command prompt, I gave the command

nmake /b /d /f checkout-stars /p

The files compiled without errors, and when I ran the program, the output was identical to the sample provided by the US Naval Observatory.