Search code examples
cvisual-studionmake

Using nmake for program slightly more complicated than hello world


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.


Solution

  • 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.