最低 c++ 为Linux制作文件
我正在寻找一个简单的推荐的“最小”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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果它是单个文件,您可以键入
它会调用
这甚至不需要目录中的 Makefile,尽管如果您有 t.cpp 和 tc 和 t.java 等,它会感到困惑。
还有一个真正的 Makefile:
If it is a single file, you can type
And it will invoke
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:
下面是我的代码片段目录中的一个通用 makefile:
只要您有一个生成一个二进制文件的 .cpp 源代码,您就不需要再做任何事情了。 我只将它与 GNU make 一起使用,并且依赖项生成使用 gcc 语法(也受 icc 支持)。 如果您使用的是SUN编译器,则需要将“-MMD”更改为“-xMMD”。 另外,请确保在粘贴此代码时
clean:
后面的行开头的制表符不会更改为空格,否则make
将会出现缺少分隔符的错误。Here is a generic makefile from my code snippets directory:
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 ormake
will give you a missing separator error.您看过SCons 吗?
只需使用以下内容创建一个 SConstruct 文件:
然后键入:
完成!
Have you looked at SCons?
Simply create a SConstruct file with the following:
Then type:
Done!
假设没有预配置系统范围的
make
设置:Assuming no preconfigured system-wide
make
settings:一个相当小的 GNU Makefile,使用预定义的规则和自动依赖:
a fairly small GNU Makefile, using predefined rules and auto-deps:
您看过 OMake 吗?
OMakeroot
OMakefile
然后在 Linux 或 Windows 上,只需键入:
作为奖励,您将自动获得:
Have you looked at OMake ?
OMakeroot
OMakefile
Then on Linux or Windows, simply type:
As a bonus, you automatically get:
关于创建基本 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.
SConstruct 带有调试选项:
SConstruct with debug option:
弗罗林有一个很好的起点。 我不喜欢 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
我一直在寻找一个最小的 Makefile 可能是什么样子,除了
我知道我参加这个聚会迟到了之外,但我想我也会把我的帽子扔进擂台。 以下是我使用多年的单目录项目Makefile。 经过一点修改,它可以扩展到使用多个目录(例如 src、obj、bin、header、test 等)。 假设所有头文件和源文件都位于当前目录中。 并且,必须为项目指定一个用于输出二进制名称的名称。
I was hunting around for what a minimal Makefile might look like other than
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.
如果您的问题是因为 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++