Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
初学者的Makefile(C+;+;) 我正在用下面的方式处理由不同文件夹组成的一个小型C++项目 Project | |-src (several .cpp files here - that depend on their .hpp) |-include (several .hpp files here) |-test(various things, not really important) |-bin |-build_C++_Makefile - Fatal编程技术网

初学者的Makefile(C+;+;) 我正在用下面的方式处理由不同文件夹组成的一个小型C++项目 Project | |-src (several .cpp files here - that depend on their .hpp) |-include (several .hpp files here) |-test(various things, not really important) |-bin |-build

初学者的Makefile(C+;+;) 我正在用下面的方式处理由不同文件夹组成的一个小型C++项目 Project | |-src (several .cpp files here - that depend on their .hpp) |-include (several .hpp files here) |-test(various things, not really important) |-bin |-build,c++,makefile,C++,Makefile,我想做的是能够将我的对象文件(.o)放入“build”文件夹,并确保我的Output.exe进入“bin”。这样,当我“清除”时,我可以简单地“从构建中删除*.o”和“从bin中删除Output.exe” 我读过一些解决方案,但看起来我无法使它以任何方式工作。有人能帮我吗?谢谢 现在我想出了这样的办法: CC = c++ INC_DIR = ./include SRC_DIR = ./src BUILD_DIR = ./build CPPFLAGS = -Wall -Wfatal-error

我想做的是能够将我的对象文件(.o)放入“build”文件夹,并确保我的Output.exe进入“bin”。这样,当我“清除”时,我可以简单地“从构建中删除*.o”和“从bin中删除Output.exe”

我读过一些解决方案,但看起来我无法使它以任何方式工作。有人能帮我吗?谢谢

现在我想出了这样的办法:

CC = c++

INC_DIR = ./include
SRC_DIR = ./src
BUILD_DIR = ./build

CPPFLAGS = -Wall -Wfatal-errors -std=c++11

SRCS = $(shell find $(SRC_DIR) -name *.cpp)
DEPS = $(shell find $(IND_DIR) -name *.hpp)
OBJS = $(SRCS:%=$(BUILD_DIR)/%.o)

INC_FLAGS = -I $(INC_DIR) -I $(SRC_DIR)
CPPFLAGS = -Wall -Wfatal-errors -std=c++11
FLAGS = $(INC_FLAGS) $(CPPFLAGS)

$(BUILD_DIR)/%.o : %.cpp $(DEPS)
    $(CC) -c -o $@ $< $(FLAGS)

.PHONY: clean

clean:
    rm -rf $(BUILD_DIR)/*.o
<代码> cc= C++ 公司目录=/包括 SRC_DIR=./SRC 构建目录=/构建 CPPFLAGS=-Wall-Wfatal errors-std=c++11 SRCS=$(shell find$(SRC_DIR)-name*.cpp) DEPS=$(shell find$(IND_DIR)-name*.hpp) OBJS=$(SRCS:%=$(BUILD\u DIR)/%.o) INC_FLAGS=-I$(INC_DIR)-I$(SRC_DIR) CPPFLAGS=-Wall-Wfatal errors-std=c++11 标志=$(INC_标志)$(CPPFLAGS) $(构建目录)/%.o:%.cpp$(DEPS) $(CC)-c-o$@$<$(标志) .假冒:干净 清洁: rm-rf$(构建目录)/*.o 当我输入“make”时,它会显示-rm-rf$(BUILD\u DIR)/*.o
这意味着他正在努力清理。问题是它甚至没有尝试编译

您缺少最终的生成目标,
bin/test.exe
。 就我个人而言,我不太喜欢
$(shell)
——请参阅下面我的解决方案

SRC       := $(wildcard src/*.cpp)
OBJ       := $(patsubst src/%.cpp,build/%.o,$(SRC))

vpath %.cpp src

CC        := g++
LD        := g++

build/%.o: %.cpp
    $(CC) -Iinclude -c $< -o $@

.PHONY: all clean

all: build bin bin/test.exe

bin/test.exe: $(OBJ)
    $(LD) $^ -o $@

clean:
    @rm -rf build/* bin/*
SRC:=$(通配符SRC/*.cpp)
对象:=$(patsubst src/%.cpp,build/%.o,$(src))
vpath%.cpp src
CC:=g++
LD:=g++
生成/%.o:%.cpp
$(CC)-i包括-c$<-o$@
.骗子:都是干净的
全部:生成bin/test.exe
bin/test.exe:$(OBJ)
$(LD)$^-o$@
清洁:
@rm-rf构建/*箱/*

您尝试过哪些解决方案,以及它们为什么不起作用?请查看cmake或介子元构建系统。我建议不要使用原始的makefile,除非你真的必须这样做。我看不出有什么问题。你被困在哪里了?非常感谢,这看起来很有魅力!现在我只需要详细了解它是如何工作的。无论如何,如果我想在不创建.exe文件的情况下“编译”呢?只是为了确保所有文件都不包含任何错误如果您没有100个文件,链接步骤不会花费很长时间,因此只需调用
make
就足够了。如果您感谢您的回答,请接受它好吗?谢谢