I am trying to compile a c program but am having difficulty getting nmake to place object files where I want them.
My directory structure is as follows:
C:.
├───src
│ └───obj
├───library
└───librarysrc
The contents of my makefile, which is in src, is as follows:
# Define directories
SRC_DIR = .
LIBSRC_DIR = ..librarysrc
OBJ_DIR = .obj
LIB_DIR = ..library
# Define variables
CC = cl
CFLAGS = /MT /I $(LIBSRC_DIR) /I $(SRC_DIR) /nologo /D_CRT_SECURE_NO_WARNINGS /W4 /Od /DDEBUG
LIBB = lib
LIBFLAGS = /OUT:$(LIB_DIR)integrate.lib /nologo
# Define target names
LIB_OBJ = $(LIBSRC_DIR)*.obj
SRC_OBJ = $(OBJ_DIR)*.obj
LIBRARY = $(LIB_DIR)integrate.lib
TARGET = $(SRC_DIR)otsuka.exe
# Default target
all: $(TARGET)
# Pattern rule to compile all .c files in libsrc to .obj files in obj
{$(LIBSRC_DIR)}.c{$(LIBSRC_DIR)}.obj:
@echo Compiling $<
$(CC) $(CFLAGS) /c $<
# Rule to create the static library
$(LIBRARY): $(LIB_OBJ)
@echo Creating library $(LIBRARY)
$(LIBB) $(LIBFLAGS) $(LIB_OBJ)
# Pattern rule to compile all .c files in src to .obj files in obj
{$(SRC_DIR)}.c{$(OBJ_DIR)}.obj::
@echo Compiling $<
$(CC) $(CFLAGS) /c $<
# Rule to compile the main program
$(TARGET): $(SRC_OBJ) $(LIBRARY)
@echo Compiling and linking to create $(TARGET)
$(CC) /nologo /Fe$(TARGET) $(SRC_OBJ) $(LIBRARY)
# Clean up intermediate files
clean:
@echo Cleaning up...
del $(OBJ_DIR)*.obj $(LIBSRC_DIR)*.obj $(LIBRARY) $(TARGET)
On running nmake, I was expecting the .obj files associated with the library to end up in ..librarysrc and those for the files in src to end up in srcobj
In fact they all end up in src and the following commands then fail as they can’t find the required object files.
Any suggestions on how to fix this would be very much appreciated!
Kilter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.