0

我正在尝试使用为 Windows 上的 mingw 编写的 makefile,并将 cl.exe 作为编译器进行交换。我删除了特定于 GCC 的 cflag,并将 .o 替换为 .obj。我根本没有改变规则,所以我不知道这是否应该是一个问题。

我收到以下错误...

cl /link build/WavFileTool.obj src/WavFileTool.cpp
process_begin: CreateProcess(NULL, cl /link build/WavFileTool.obj src/WavFileTool.cpp, ...) failed.
make (e=2): The system cannot find the file specified.

这是我的makefile

CC := cl
CFLAGS := /link
BIN_DIR := bin
BUILD_DIR := build
SRC_DIR := src
MAIN := WavFileTool
TARGET := wavfiletool.exe
SOURCES := $(wildcard src/*.cpp)
OBJECTS := $(SOURCES:$(SRC_DIR)/%.cpp=$(BUILD_DIR)/%.obj)

$(BIN_DIR)/$(TARGET): CREATE_DIRS $(BUILD_DIR)/$(MAIN).obj $(OBJECTS) 
    $(CC) $(OBJECTS) $@ 

$(BUILD_DIR)/$(MAIN).obj: $(SRC_DIR)/WavFileTool.cpp
    $(CC) $(CFLAGS) $@ $<

$(BUILD_DIR)/%.obj: $(SRC_DIR)/%.cpp $(SRC_DIR)/%.h
    $(CC) $(CFLAGS) $@ $<

CREATE_DIRS: 
    if not exist $(BIN_DIR) mkdir $(BIN_DIR)
    if not exist $(BUILD_DIR) mkdir $(BUILD_DIR)

CLEAN:
    if exist $(BUILD_DIR) rmdir /Q /S $(BUILD_DIR)
4

1 回答 1

0

Some issues with the makefile as posted:

  • CFLAGS should be '/c' not '/link';
  • the MS toolchain uses link.exe for linking, not $(CC);
  • both cl.exe and link.exe require a switch to specify the output file.

There is a less obvious problem as well. Since you seem to be using the mingw 'make' chances are that there might be a 'link.exe' in the same directory with 'make.exe'. Simply running 'link' from the makefile would execute the mingw 'link.exe' rather than the MS one. To direct 'make' to use the MS 'link' you need to provide a full path, which can generally be inferred from the environment e.g. '$(VCINSTALLDIR)\bin\link.exe'.

Below is the makefile with the necessary changes.

CC := cl
CFLAGS := /c
BIN_DIR := bin
BUILD_DIR := build
SRC_DIR := src
MAIN := WavFileTool
TARGET := wavfiletool.exe
SOURCES := $(wildcard src/*.cpp)
OBJECTS := $(SOURCES:$(SRC_DIR)/%.cpp=$(BUILD_DIR)/%.obj)

$(BIN_DIR)/$(TARGET): CREATE_DIRS $(BUILD_DIR)/$(MAIN).obj $(OBJECTS)
    $(VCINSTALLDIR)/bin/link /out:$@ $(OBJECTS)

$(BUILD_DIR)/$(MAIN).obj: $(SRC_DIR)/WavFileTool.cpp
    $(CC) $(CFLAGS) /Fo$@ $<

$(BUILD_DIR)/%.obj: $(SRC_DIR)/%.cpp $(SRC_DIR)/%.h
    $(CC) $(CFLAGS) /Fo$@ $<

CREATE_DIRS:
    if not exist $(BIN_DIR) mkdir $(BIN_DIR)
    if not exist $(BUILD_DIR) mkdir $(BUILD_DIR)

CLEAN:
    if exist $(BUILD_DIR) rmdir /Q /S $(BUILD_DIR)

See https://msdn.microsoft.com/en-us/library/19z1t1wy.aspx for the full CL syntax (the link goes to the VC++ 2015 version, but you can select other versions on the page), and https://msdn.microsoft.com/en-us/library/y0zzbyt4.aspx for LINK.

于 2015-11-14T04:41:54.320 回答