我怎样将目标文件生成到与 Makefile 不同的目录中?

huangapple go评论64阅读模式
英文:

How do I create object files into a different directory than Makefile's one?

问题

I'm new on using Makefiles because I've been programming with VS2019 on Windows, solving all my compilation and linking problems.

This is the result:

BUILD_DIR:= ./build
SRC_DIRS := ./src
INCL_DIR := ./includes
CC := /usr/bin/g++		#Compiler used
LVERSION := -std=c++17 	#Language Version
CXXFLAGS := -g		    #Cpp flags
CFLAGS := -Wall			#Compiler Flags
LFLAGS := -lstdc++fs	#Linker Flags
SRCS := Audio.cpp Song.cpp Visual.cpp VisualSong.cpp
LIBS := 
INCLUDES := $(SRCS:%.cpp=$(INCL_DIR)/%.h)
OBJS := $(SRCS:%.cpp=$(BUILD_DIR)/%.o)
PROG := progName.exe

progName: $(OBJS)
	$(CC) $(CFLAGS) $(CXXFLAGS) -o $(INCLUDES) $(PROG) $(OBJS)

$(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
	$(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^ 

.PHONY: progName
clean:
	/bin/rm -rf build/*.o $(PROG) includes/*.gch

This makefile works until is trying to look on objects file, supposedly created on build directory but, in the end, they're created in Makefile's directory, which is an inconvenient since all what i want is to have separated files for organization purposes.

I know that somehow using implicit rules that are using the dst's directory should do the trick, but i think that I'm missing something on the way...

I'm on a Windows 10 machine with WSL for Ubuntu, but this shouldn't be a problem at all for this problem.

Could anyone explain to me what am I missing?

英文:

I'm new on using Makefiles because I've been programming with VS2019 on Windows, solving all my compilation and linking problems.

This is the result:

BUILD_DIR:= ./build
SRC_DIRS := ./src
INCL_DIR := ./includes
CC := /usr/bin/g++		#Compiler used
LVERSION := -std=c++17 	#Language Version
CXXFLAGS := -g		    #Cpp flags
CFLAGS := -Wall			#Compiler Flags
LFLAGS := -lstdc++fs	#Linker Flags
SRCS := Audio.cpp Song.cpp Visual.cpp VisualSong.cpp
LIBS := 
INCLUDES := $(SRCS:%.cpp=$(INCL_DIR)/%.h)
OBJS := $(SRCS:%.cpp=$(BUILD_DIR)/%.o)
PROG := progName.exe

progName: $(OBJS)
	$(CC) $(CFLAGS) $(CXXFLAGS) -o $(INCLUDES) $(PROG) $(OBJS)

$(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
	$(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^ 

.PHONY: progName
clean:
	/bin/rm -rf build/*.o $(PROG) includes/*.gch

This makefile works until is trying to look on objects file, supposedly created on build directory but, in the end, they're created in Makefile's directory, which is an inconvenient since all what i want is to have separated files for organization purposes.

I know that somehow using implicit rules that are using the dst's directory should do the trick, but i think that I'm missing something on the way...

I'm on a Windows 10 machine with WSL for Ubuntu, but this shouldn't be a problem at all for this problem.

Could anyone explain to me what am I missing?

答案1

得分: 2

看这个规则:

$(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
    $(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^

表面上它是用来构建build/foo.o的规则,但是配方($(CC)…)实际上构建了foo.o。有一个简单的修复方法:

$(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
    $(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^ -o $@

一旦这样工作了,我建议您再做一个更改:

$(BUILD_DIR)/%.o: $(SRC_DIRS)/%.cpp $(INCL_DIR)/%.h
    $(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $< -o $@

如果您需要进一步的帮助,请告诉我。

英文:

Look at this rule:

$(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
    $(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^ 

Ostensibly it is the rule to build build/foo.o, but the recipe ($(CC)...) actually builds foo.o. There is an easy fix:

$(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
    $(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^ -o $@

Once that works I suggest you make one further change:

$(BUILD_DIR)/%.o: $(SRC_DIRS)/%.cpp $(INCL_DIR)/%.h
    $(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $&lt; -o $@

huangapple
  • 本文由 发表于 2023年2月19日 23:48:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定