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

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

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:

  1. BUILD_DIR:= ./build
  2. SRC_DIRS := ./src
  3. INCL_DIR := ./includes
  4. CC := /usr/bin/g++ #Compiler used
  5. LVERSION := -std=c++17 #Language Version
  6. CXXFLAGS := -g #Cpp flags
  7. CFLAGS := -Wall #Compiler Flags
  8. LFLAGS := -lstdc++fs #Linker Flags
  9. SRCS := Audio.cpp Song.cpp Visual.cpp VisualSong.cpp
  10. LIBS :=
  11. INCLUDES := $(SRCS:%.cpp=$(INCL_DIR)/%.h)
  12. OBJS := $(SRCS:%.cpp=$(BUILD_DIR)/%.o)
  13. PROG := progName.exe
  14. progName: $(OBJS)
  15. $(CC) $(CFLAGS) $(CXXFLAGS) -o $(INCLUDES) $(PROG) $(OBJS)
  16. $(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
  17. $(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^
  18. .PHONY: progName
  19. clean:
  20. /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:

  1. BUILD_DIR:= ./build
  2. SRC_DIRS := ./src
  3. INCL_DIR := ./includes
  4. CC := /usr/bin/g++ #Compiler used
  5. LVERSION := -std=c++17 #Language Version
  6. CXXFLAGS := -g #Cpp flags
  7. CFLAGS := -Wall #Compiler Flags
  8. LFLAGS := -lstdc++fs #Linker Flags
  9. SRCS := Audio.cpp Song.cpp Visual.cpp VisualSong.cpp
  10. LIBS :=
  11. INCLUDES := $(SRCS:%.cpp=$(INCL_DIR)/%.h)
  12. OBJS := $(SRCS:%.cpp=$(BUILD_DIR)/%.o)
  13. PROG := progName.exe
  14. progName: $(OBJS)
  15. $(CC) $(CFLAGS) $(CXXFLAGS) -o $(INCLUDES) $(PROG) $(OBJS)
  16. $(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
  17. $(CC) ${CFLAGS} $(CXXFLAGS) $(LVERSION) ${LFLAGS} -c $^
  18. .PHONY: progName
  19. clean:
  20. /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

看这个规则:

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

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

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

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

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

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

英文:

Look at this rule:

  1. $(BUILD_DIR)/%.o: $(INCL_DIR)/%.h $(SRC_DIRS)/%.cpp
  2. $(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:

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

Once that works I suggest you make one further change:

  1. $(BUILD_DIR)/%.o: $(SRC_DIRS)/%.cpp $(INCL_DIR)/%.h
  2. $(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:

确定