首页 > 解决方案 > 如何修改此 C++ Makefile 以制作具有相同源文件但来自不同 main.cpp 的多个二进制文件

问题描述

目前,我有一个非常酷的通用 C++ makefile,它只能从多个源文件和头文件中生成一个二进制文件。这一切都足够了。我想知道如何修改它以生成 2 个二进制文件。只有 mains.cpp 会有所不同。标题也是一样的。源文件是一样的。我认为这是可能的,我什至想知道是否存在循环槽 exec 和二进制文件(EXEC 文件)来生成超过 2 个二进制文件(具有相同的源文件)。我没有掌握足够多的 GNU makefile 语言来了解这一点。多谢。我只是确定我必须在我的 2 个二进制文件中添加一个 .all 规则,但之后什么都没有。看这个 :

# sources (*.cpp)
SRC  := sources
# headers (*.cpp)
INC  := include
MAIN := kwic_personnel.cpp
EXEC := kwic
# second binary here
MAIN2 := other.cpp
EXEC2 := other

# main file is in current directory or in sources file but here the main files are in out of
# sources file for sure
ifneq ("$(wildcard $(MAIN))","")
    sources := $(MAIN) $(wildcard $(SRC)/*.cpp)
else
    sources := $(wildcard $(SRC)/*.cpp)
endif
# obj files
objects := $(sources:.cpp=.o)
# dep files
deps    := $(objects:.o=.d)

CXX := g++
CXXFLAGS := -I $(INC) -MMD -MP -g -std=c++17 -Wall -pedantic -Weffc++ -Werror

# OS name
UNAME := $(shell uname -s)

# NOT SURE ABOUT THAT FOR MAC OS
# if linux
ifeq ($(UNAME), Linux)

    LDFLAGS  := -L/usr/lib/x86_64-linux-gnu
    LDLIBS   := -lcurl

# else but mac OS
else
    CXXFLAGS += -D OSX
    LDFLAGS  := -stdlib=libstdc++
endif

# linking
$(EXEC) : $(objects)
    @echo "Generation du fichier executable..."
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
    $(RM) $(objects) $(deps)
    @echo "Compilation réussie !"

# compilation of cpp files
$(SRC)/%.o: $(SRC)/%.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $^ -o $@

# remove exec
.PHONY: clean
clean:
    $(RM) $(EXEC)

# dependencies
-include $(deps)

标签: c++makefilegnu-make

解决方案


推荐阅读