在 makefile 中使用多个源文件扩展名
我有一个 C++ 项目,其中包含各种源文件扩展名(.cpp、.c、.cc)和各种头文件扩展名(.hpp、.h、.hh)。源文件位于名为 SRC 的目录中,头文件可以预见地位于名为 INC 的目录中。
我想使用类似的规则来编译源代码,
vpath %.c $(SRC)
%.o: %.c
$(COMPILER) $(FLAGS) $< $(INCFLAG)$(INC)
如果我知道源文件的格式为 %,那么这当然可以工作.c,但在存在多个可能的文件扩展名的情况下,我还需要为 %.cpp 和 %.cc 构建类似的规则。当然,编写三个规则并不是什么大问题,但如果能够使用这个 makefile 作为任何项目的拖放功能,即使是使用不同的语言,而无需重新编写规则,那就太好了。
那么我怎样才能编写一个规则(或实现相同目标的其他结构),其工作原理如下:
SRC_EXT = cpp c cc
vpath %.$(SRC_EXT) $(SRC)
%.o: %.$(SRC_EXT)
$(COMPILER) $(FLAGS) $< $(INCFLAG)$(INC)
感谢您的帮助。
I have a c++ project with various extensions for the source files (.cpp, .c, .cc) and various extensions for the header files (.hpp, .h, .hh). The source files are located in a directory called SRC, and the header files are predictably in a directory called INC.
I would like to compile the source with a rule like
vpath %.c $(SRC)
%.o: %.c
$(COMPILER) $(FLAGS) lt; $(INCFLAG)$(INC)
This of course works if I know the source file will be of the form %.c, but in the case of multiple possible file extensions, I would need to build a similar rule for %.cpp and %.cc as well. Of course three rules isn't a big deal to write, but it would be nice to be able to use this makefile as a drag and drop for any project, even in a different language, without having to re-write the rules.
So how can I write a rule (or some other construct that accomplishes the same goal) that works like:
SRC_EXT = cpp c cc
vpath %.$(SRC_EXT) $(SRC)
%.o: %.$(SRC_EXT)
$(COMPILER) $(FLAGS) lt; $(INCFLAG)$(INC)
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能在标准 POSIX make 中。不过,既然你提到了 vpath,我就假设你正在使用 GNU make。如果您有足够新的版本(3.81 或更新版本),您可以使用 call 和 eval 轻松完成此操作:
如果您没有足够新的 GNU make,或者更喜欢替代解决方案,您可以使用 generated 执行相同的操作生成文件。
You can't in standard POSIX make. However since you mention vpath I'll assume you're using GNU make. If you have a sufficiently new version (3.81 or newer), you can do this easily enough with call and eval:
If you don't have sufficiently new GNU make, or would prefer an alternate solution, you can do the same thing with generated makefiles.