用于 linux 的最小 C++ 生成文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/287259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:21:43  来源:igfitidea点击:

minimum c++ make file for linux

c++makefilecompilation

提问by RichieHH

I've looking to find a simple recommended "minimal" c++ makefile for linux which will use g++ to compile and link a single file and h file. Ideally the make file will not even have the physical file names in it and only have a .cpp to .o transform. What is the best way to generate such a makefile without diving into the horrors of autoconf?

我正在寻找一个简单的推荐的“最小”c++ makefile for linux,它将使用 g++ 来编译和链接单个文件和 h 文件。理想情况下,make 文件中甚至不会包含物理文件名,并且只有 .cpp 到 .o 的转换。在不深入了解 autoconf 的恐怖的情况下,生成这样的 makefile 的最佳方法是什么?

The current dir contains, for example

当前目录包含,例如

t.cpp t.h

t.cpp th

and I want a makefile for that to be created. I tried autoconf but its assuming .h is gcc instead of g++. Yes, while not a beginner, I am relearning from years ago best approaches to project manipulation and hence am looking for automated ways to create and maintain makefiles for small projects.

我想为此创建一个 makefile。我试过 autoconf 但它假设 .h 是 gcc 而不是 g++。是的,虽然不是初学者,但我正在从几年前重新学习项目操作的最佳方法,因此我正在寻找自动化的方法来为小型项目创建和维护 makefile。

回答by hazzen

If it is a single file, you can type

如果是单个文件,可以输入

make t

And it will invoke

它会调用

g++ t.cpp -o t

This doesn't even require a Makefile in the directory, although it will get confused if you have a t.cpp and a t.c and a t.java, etc etc.

这甚至不需要目录中的 Makefile,尽管如果您有 t.cpp、tc 和 t.java 等,它会感到困惑。

Also a real Makefile:

也是一个真正的 Makefile:

SOURCES := t.cpp
# Objs are all the sources, with .cpp replaced by .o
OBJS := $(SOURCES:.cpp=.o)

all: t

# Compile the binary 't' by calling the compiler with cflags, lflags, and any libs (if defined) and the list of objects.
t: $(OBJS)
    $(CC) $(CFLAGS) -o t $(OBJS) $(LFLAGS) $(LIBS)

# Get a .o from a .cpp by calling compiler with cflags and includes (if defined)
.cpp.o:
    $(CC) $(CFLAGS) $(INCLUDES) -c $<

回答by florin

Here is a generic makefile from my code snippets directory:

这是我的代码片段目录中的通用 makefile:

SOURCES=$(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
DEPS=$(SOURCES:.cpp=.d)
BINS=$(SOURCES:.cpp=)

CFLAGS+=-MMD
CXXFLAGS+=-MMD

all: $(BINS)

.PHONY: clean

clean:
    $(RM) $(OBJECTS) $(DEPS) $(BINS)

-include $(DEPS)

As long as you have one .cpp source producing one binary, you don't need anything more. I have only used it with GNU make, and the dependency generation uses gcc syntax (also supported by icc). If you are using the SUN compilers, you need to change "-MMD" to "-xMMD". Also, ensure that the tab on the start of the line after clean:does not get changed to spaces when you paste this code or makewill give you a missing separator error.

只要您有一个 .cpp 源生成一个二进制文件,您就不需要其他任何东西了。我只用过 GNU make,依赖生成使用 gcc 语法(icc 也支持)。如果您使用的是 SUN 编译器,则需要将“-MMD”更改为“-xMMD”。此外,请确保clean:在粘贴此代码时,行首的选项卡不会更改为空格,否则make会出现缺少分隔符的错误。

回答by grieve

Have you looked at SCons?

你看过SCons吗?

Simply create a SConstruct file with the following:

只需使用以下内容创建一个 SConstruct 文件:

Program("t.cpp")

Then type:

然后输入:

scons

Done!

完毕!

回答by Alnitak

Assuming no preconfigured system-wide makesettings:

假设没有预配置的系统范围的make设置:

CXX = g++
CPPFLAGS =        # put pre-processor settings (-I, -D, etc) here
CXXFLAGS = -Wall  # put compiler settings here
LDFLAGS =         # put linker settings here

test: test.o
    $(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) test.o

.cpp.o:
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $<

test.cpp: test.h

回答by Sam Watkins

a fairly small GNU Makefile, using predefined rules and auto-deps:

一个相当小的 GNU Makefile,使用预定义的规则和自动依赖:

CC=c++
CXXFLAGS=-g -Wall -Wextra -MMD
LDLIBS=-lm
program: program.o sub.o
clean:
    $(RM) *.o *.d program
-include $(wildcard *.d)

回答by bltxd

Have you looked at OMake?

你看过OMake吗?

OMakeroot

OMakeroot

open build/C
DefineCommandVars()
.SUBDIRS: .

OMakefile

生成文件

.DEFAULT: $(CXXProgram test, test)

Then on Linux or Windows, simply type:

然后在 Linux 或 Windows 上,只需键入:

omake

As a bonus, you automatically get:

作为奖励,您将自动获得:

  • parallel builds with the -j option (same as make).
  • MD5 checksums instead of timestamps (build becomes resilient to time synchronization failures).
  • Automatic and accurate C/C++ header dependencies.
  • Accurate inter-directory dependencies (something that recursive make does not offer).
  • Portability (1 build chain to rule them all, immune to path style issues).
  • A real programming language (better than GNU make).
  • 使用 -j 选项并行构建(与 make 相同)。
  • MD5 校验和而不是时间戳(构建对时间同步失败具有弹性)。
  • 自动且准确的 C/C++ 头文件依赖项。
  • 准确的目录间依赖关系(递归 make 不提供的东西)。
  • 可移植性(1 个构建链来统治它们,不受路径样式问题的影响)。
  • 一种真正的编程语言(比 GNU make 更好)。

回答by Bob Smith

I was hunting around for what a minimal Makefile might look like other than

我一直在寻找最小的 Makefile 可能是什么样子,除了

some_stuff:
    @echo "Hello World"

I know I am late for this party, but I thought I would toss my hat into the ring as well. The following is my one directory project Makefile I have used for years. With a little modification it scales to use multiple directories (e.g. src, obj, bin, header, test, etc). Assumes all headers and source files are in the current directory. And, have to give the project a name which is used for the output binary name.

我知道我参加这个派对迟到了,但我想我也会把我的帽子扔进戒指里。以下是我使用多年的一个目录项目Makefile。稍加修改,它就可以扩展到使用多个目录(例如 src、obj、bin、header、test 等)。假设所有头文件和源文件都在当前目录中。并且,必须为项目提供一个用于输出二进制名称的名称。

NAME = my_project

FILES = $(shell basename -a $$(ls *.cpp) | sed 's/\.cpp//g')
SRC = $(patsubst %, %.cpp, $(FILES))
OBJ = $(patsubst %, %.o, $(FILES))
HDR = $(patsubst %, -include %.h, $(FILES))
CXX = g++ -Wall

%.o : %.cpp
        $(CXX) $(HDR) -c -o $@ $<

build: $(OBJ)
        $(CXX) -o $(NAME) $(OBJ)

clean:
        rm -vf $(NAME) $(OBJ)

回答by RichieHH

SConstruct with debug option:

带有调试选项的 SConstruct:

env = Environment()

if ARGUMENTS.get('debug', 0):
    env.Append(CCFLAGS = ' -g')

env.Program( source = "template.cpp" )

回答by jdkoftinoff

florin has a good starting point. I didn't like gnu autoconf so I started there and took the concept further and called it the MagicMakefile. I have 3 versions of it from simple to more complex. The latest is now on github: https://github.com/jdkoftinoff/magicmake

弗洛林有一个很好的起点。我不喜欢 gnu autoconf,所以我从那里开始并进一步扩展了这个概念并将其称为 MagicMakefile。我有 3 个版本,从简单到复杂。最新的现在在 github 上:https: //github.com/jdkoftinoff/magicmake

Basically, it assumes you have a standard layout for the source files of your project and uses the wildcard function to create the makefile rules on the fly which are then eval'd, handling header file dependancies, cross compiling, unit tests, install, and packaging.

基本上,它假设您有项目源文件的标准布局,并使用通配符函数动态创建 makefile 规则,然后对其进行评估,处理头文件依赖项、交叉编译、单元测试、安装和包装。

[edit] At this point I use cmake for all my projects since it generates useful project files for many build systems.

[编辑] 在这一点上,我将 cmake 用于我的所有项目,因为它为许多构建系统生成有用的项目文件。

jeff koftinoff

杰夫·科夫蒂诺夫

回答by Jay

Some good references on creating a basic Makefile

关于创建基本 Makefile 的一些很好的参考

http://en.wikipedia.org/wiki/Make_(software)

http://en.wikipedia.org/wiki/Make_(软件)

http://mrbook.org/tutorials/make/

http://mrbook.org/tutorials/make/

http://www.opussoftware.com/tutorial/TutMakefile.htm

http://www.opussoftware.com/tutorial/TutMakefile.htm

http://www.hsrl.rutgers.edu/ug/make_help.html

http://www.hsrl.rutgers.edu/ug/make_help.html

The first couple in particular have minimal example Makefiles like you were describing. Hope that helps.

特别是第一对夫妇像您描述的那样具有最少的示例 Makefile。希望有帮助。