I am trying to compile a C project using a nmake makefile on Windows with Visual Studio 2019. When I run the compilation commands manually in the Developer Command Prompt, everything works fine. However, when I try to use nmake, I encounter the following error:
LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30133binHostX64x64cl.EXE"' : return code '0x2'
Stop.
Here is the relevant part of my makefile:
# Define variables
CC = cl
CFLAGS = /MT /I include /nologo /D_CRT_SECURE_NO_WARNINGS /W4 /Od /DDEBUG
LIB = lib
LIBFLAGS = /OUT:my_library.lib /nologo
SRC_DIR = src
INCLUDE_DIR = include
LIB_NAME = my_library
OBJ = my_library.obj
TARGET = my_program.exe
# Default target
all: $(TARGET)
# Rule to compile the library source file
$(OBJ): $(SRC_DIR)$(LIB_NAME).c
@echo Compiling $(SRC_DIR)$(LIB_NAME).c
$(CC) $(CFLAGS) /c $(SRC_DIR)$(LIB_NAME).c /Fo$(OBJ)
# Rule to create the static library
$(LIB_NAME).lib: $(OBJ)
@echo Creating library $(LIB_NAME).lib
$(LIB) $(LIBFLAGS) $(OBJ)
# Rule to compile the main program
$(TARGET): main.obj $(LIB_NAME).lib
@echo Compiling and linking main.c to create $(TARGET)
$(CC) /nologo /Fe$(TARGET) main.obj $(LIB_NAME).lib /link /LIBPATH:"C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30133libx64" /LIBPATH:"C:Program Files (x86)Windows Kits10lib10.0.19041.0ucrtx64" /LIBPATH:"C:Program Files (x86)Windows Kits10lib10.0.19041.0umx64"
# Rule to compile main.c
main.obj: main.c
@echo Compiling main.c
$(CC) $(CFLAGS) /c main.c /Fomain.obj
# Clean up intermediate files
clean:
@echo Cleaning up...
del $(OBJ) $(LIB_NAME).lib main.obj $(TARGET)
Environment:
Visual Studio 2019
Windows 10
Running commands in the “Developer Command Prompt for Visual Studio 2019”
Manual Commands:
When I run the following commands manually, they work without any issues:
cl /MT /I include /nologo /D_CRT_SECURE_NO_WARNINGS /W4 /Od /DDEBUG /c srcmy_library.c /Fomy_library.obj
lib /OUT:my_library.lib /nologo my_library.obj
cl /MT /I include /nologo /D_CRT_SECURE_NO_WARNINGS /W4 /Od /DDEBUG /c main.c /Fomain.obj
cl /nologo /Fe my_program.exe main.obj my_library.lib /link /LIBPATH:”C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30133libx64″ /LIBPATH:”C:Program Files (x86)Windows Kits10lib10.0.19041.0ucrtx64″ /LIBPATH:”C:Program Files (x86)Windows Kits10lib10.0.19041.0umx64″
Questions:
1. Why does nmake fail to find LIBCMT.lib when the manual commands work perfectly?
2. How can I ensure that nmake correctly finds and links LIBCMT.lib?
Any help or suggestions would be greatly appreciated. Thank you!
Kilter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.