c++ - 如何修改此 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)
解决方案
推荐阅读
- electron - Electron 如何找出哪个进程正在执行公共模块?
- javascript - 在 forEach 循环中使用 onclick 将变量传递给函数 - Google Apps 脚本
- c++ - C++ 基类作为代理
- c# - “程序不包含适用于入口点 WindowsFormsApp 的静态 'Main' 方法”错误消息
- php - 如何在 For 和 while 循环中像先进先出一样在 Top 位置添加数据?可能是反向循环?
- c# - 从资产文件夹统一加载精灵到游戏对象
- kubernetes - Helm 继承变量值
- java - 为什么这行得通,但不像我预期的那样?Java 条件运算符
- flutter - Flutter 中的应用内购买不获取不同设备中的过去购买
- openstreetmap - 使用全息视图放大时,如何让 plottetd 点保持相同大小?