最低 c++ 为Linux制作文件

发布于 2024-07-09 01:05:17 字数 339 浏览 5 评论 0原文

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

当前目录包含,例如

t.cpp th

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

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?

The current dir contains, for example

t.cpp
t.h

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

水中月 2024-07-16 01:05:18

如果它是单个文件,您可以键入

make t

它会调用

g++ t.cpp -o t

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

还有一个真正的 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 
lt;

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.

Also a real 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 
lt;
最舍不得你 2024-07-16 01:05:18

下面是我的代码片段目录中的一个通用 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)

只要您有一个生成一个二进制文件的 .cpp 源代码,您就不需要再做任何事情了。 我只将它与 GNU make 一起使用,并且依赖项生成使用 gcc 语法(也受 icc 支持)。 如果您使用的是SUN编译器,则需要将“-MMD”更改为“-xMMD”。 另外,请确保在粘贴此代码时 clean: 后面的行开头的制表符不会更改为空格,否则 make 将会出现缺少分隔符的错误。

Here is a generic makefile from my code snippets directory:

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 make will give you a missing separator error.

夏了南城 2024-07-16 01:05:18

您看过SCons 吗?

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

Program("t.cpp")

然后键入:

scons

完成!

Have you looked at SCons?

Simply create a SConstruct file with the following:

Program("t.cpp")

Then type:

scons

Done!

一杆小烟枪 2024-07-16 01:05:18

假设没有预配置系统范围的 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 
lt;

test.cpp: test.h

Assuming no preconfigured system-wide make settings:

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 
lt;

test.cpp: test.h
逆光飞翔i 2024-07-16 01:05:18

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

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

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

CC=c++
CXXFLAGS=-g -Wall -Wextra -MMD
LDLIBS=-lm
program: program.o sub.o
clean:
    $(RM) *.o *.d program
-include $(wildcard *.d)
原谅我要高飞 2024-07-16 01:05:18

您看过 OMake 吗?

OMakeroot

open build/C
DefineCommandVars()
.SUBDIRS: .

OMakefile

.DEFAULT: $(CXXProgram test, test)

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

omake

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

  • 使用 -j 选项进行并行构建(与 make 相同)。
  • MD5 校验和而不是时间戳(构建可以适应时间同步失败)。
  • 自动且准确的 C/C++ 标头依赖项。
  • 准确的目录间依赖关系(递归 make 不提供的东西)。
  • 可移植性(1 个构建链来统治它们,不受路径样式问题的影响)。
  • 一种真正的编程语言(比 GNU make 更好)。

Have you looked at OMake ?

OMakeroot

open build/C
DefineCommandVars()
.SUBDIRS: .

OMakefile

.DEFAULT: $(CXXProgram test, test)

Then on Linux or Windows, simply type:

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).
骄傲 2024-07-16 01:05:18

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

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

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

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

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

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

Some good references on creating a basic Makefile

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

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

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

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.

从此见与不见 2024-07-16 01:05:18

SConstruct 带有调试选项:

env = Environment()

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

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

SConstruct with debug option:

env = Environment()

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

env.Program( source = "template.cpp" )
多情出卖 2024-07-16 01:05:18

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

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

[编辑] 此时,我对所有项目都使用 cmake,因为它为许多构建系统生成有用的项目文件。

杰夫·考夫蒂诺夫

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

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.

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

jeff koftinoff

听风念你 2024-07-16 01:05:18

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

some_stuff:
    @echo "Hello World"

我知道我参加这个聚会迟到了之外,但我想我也会把我的帽子扔进擂台。 以下是我使用多年的单目录项目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 $@ 
lt;

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

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

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

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.

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 $@ 
lt;

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

clean:
        rm -vf $(NAME) $(OBJ)
冰魂雪魄 2024-07-16 01:05:18

如果您的问题是因为 autoconf 认为 .h 文件是 ac 文件,请尝试将其重命名为 .hpp 或 .h++

If your issues are because autoconf thinks the .h file is a c file, try renaming it to .hpp or .h++

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文