使用 makefile 分离对象和源
我一直无法让我的 makefile 按我想要的方式工作。 首先,我想说这是 POSIX make,如 http:// /www.opengroup.org/onlinepubs/009695399/utilities/make.html 我需要我的构建系统能够与 BSD 和 GNU(Linux)一起工作。
我想要的是一个零维护的 makefile。 我希望它只编译 src/ 中的所有 .c 和 .asm 文件,并将目标文件放在 objs/ 中,然后将 objs/ 中的所有内容链接到二进制文件。
我可以做很多事情,但我无法将源文件和 obj 文件分开。
如果这需要一些内置的 shell 脚本(使用 POSIX 定义的 /bin/sh),我没问题,但我无法让依赖项正常工作。 我希望它仅在源文件较新时构建目标文件。
我最接近的是:
${C_OBJS}: ${HDRS} ${*:objs/%=src/%}.c ${CC} ${CFLAGS} -c ${*:objs/%=src/%}.c -o $*.o
这有一个问题,我仍然必须指定 C_OBJS=objs/foo.o 等,而且它几乎不是 POSIX,因此,使用 BSD make 而不是 GNU make 进行编译。
I have been having troubles getting my makefiles to work the way I want. First off, I would like to say this is POSIX make, as in http://www.opengroup.org/onlinepubs/009695399/utilities/make.html I am needing my build system to work with both BSDs and GNUs(Linux).
What I am wanting is a zero maintenance makefile. I want it to just compile all .c and .asm files in src/ and place the object files in objs/ and then to link everything in objs/ to a binary file.
I can do a lot, but I can't get it to separate the source and obj files.
I am ok if this requires a little built-in shell scripting (using POSIX defined /bin/sh), but I can just not get the dependencies to work right. I want it to only build the object file if the source file is newer.
My closest is this:
${C_OBJS}: ${HDRS} ${*:objs/%=src/%}.c ${CC} ${CFLAGS} -c ${*:objs/%=src/%}.c -o $*.o
This has the problem that I must still specify C_OBJS=objs/foo.o and such and also it is just barely not POSIX and therefore, compiles with BSD make but not GNU make.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
POSIX 版本的 make 不明确支持包含斜杠的文件名,也不提供将不同目录中的源文件与目标文件分开的规定。 而且,正如 @caskey 所指出的,它不支持使用“
%
”字符的任何表示法,尽管它指出存在此类规则并建议保留它们用作元字符。因此,您可能无法使用标准 POSIX
make
执行您想要的操作。在实践中,您通常可以使用
make
的特定实现来完成您想要的任务,但生成的makefile
的可移植性有限。考虑使用某种 makefile 生成系统 -
cmake
或自动工具(autoconf
、libtool
、automake
, ETC)。 或者是对make
基本概念的众多改造之一:The POSIX version of make does not explicitly support file names with slashes in them, nor does it make provision for separating source files in a different directory from the object files. And, as noted by @caskey, it does not support any notation using '
%
' characters, though it notes that such rules exist and recommends that they be reserved for use as metacharacters.Consequently, you probably cannot do what you want with standard POSIX
make
.In practice, you can often do what you seek with specific implementations of
make
, but the resultingmakefile
has limited portability.Consider using a makefile generation systems of some sort -
cmake
or the auto-tools (autoconf
,libtool
,automake
, etc). Or one of the many reworkings of the basic concepts ofmake
:POSIX make 不支持类似的构造?
忘记最后的问号,希望这能让我的评论更清楚。
POSIX make doesn't support constructs like?
Forgot the question mark at the end, hope that makes my comment more clear.