C ++ Makefile-如何处理混合源文件后缀的规则(例如.cpp和.cxx) [英] c++ makefile - how do you deal with rules for mixed source file suffixes (e.g. .cpp and .cxx)

查看:607
本文介绍了C ++ Makefile-如何处理混合源文件后缀的规则(例如.cpp和.cxx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目混合了源文件(由于第三方代码),因此我们有一些.cpp和一些.cxx文件.

My project has mixed source files (due to 3rd party code), so we have some .cpp and some .cxx file.

接着我的上一个问题

Following on from my previous question here I have the following makefile:

# Folders
SRCDIR = src
OBJDIR = obj
SUBFOLDERS = test test/test2
OBJSUBFOLDERS = $(OBJDIR) $(addprefix $(OBJDIR)/, $(SUBFOLDERS))
SRCSUBFOLDERS = $(SRCDIR) $(addprefix $(SRCDIR)/, $(SUBFOLDERS))

# Just for me so I can read my own makefile :o
_TARGET = $@
_DEPEND = $<

# Get sources from /src and /src/test/ and /src/test/test2/
SOURCES = $(foreach dir, $(SRCSUBFOLDERS), $(wildcard $(dir)/*.cpp $(dir)/*.cxx))
# Source file without the /src at the start, then take the basename, then add .o, then add obj/ to the front
OBJECTS = $(addprefix $(OBJDIR)/, $(addsuffix .o, $(basename $(patsubst src/%, %, $(SOURCES)))))

# Main target 'make debug'
debug: debugging $(OBJECTS)
    @echo building: gcc $(OBJECTS) -o out.exe

# Compiling each file
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp         <---- ISSUE HERE
    g++ -c $(_DEPEND) -o $(_TARGET)

debugging:
    @echo $(SOURCES)
    @echo $(OBJECTS)

因此,我已经生成了源文件和目标文件的列表.对象混合为.cpp和.cxx.因此,我的规则$(OBJDIR)/%.o: $(SRCDIR)/%.cpp仅适用于.cpp文件.我尝试将其设置为$(OBJDIR)/%.o: $(SRCDIR)/%.c*,但这没有用.我怎样才能使该规则通用来处理.还是有更好的方法?

So I have generated a list of source files and object files. The objects are mixed .cpp and .cxx. So my rule $(OBJDIR)/%.o: $(SRCDIR)/%.cpp only works for .cpp files. I tried to make it $(OBJDIR)/%.o: $(SRCDIR)/%.c* but that did not work. How can I make this rule generic to handle either. Or is there a better way?

如果我从SOURCES =中删除了$(dir)/*.cxx,那么这一切都有效,但仅适用于.cpp文件.

If I remove the $(dir)/*.cxx from SOURCES = then this all works but only for .cpp files.

更新

这是我得到的输出:

SOURCES: src/main.cpp src/test/test.cpp src/test/test2.cxx
OBJECTS: obj/main.o obj/test/test.o obj/test/test2.o

Error: No rule to make target 'obj/test/test2.o', needed by 'debug'

推荐答案

您可以有多个规则来从不同的扩展名构建目标器.

You can have multiple rules for building a targer from different extensions.

例如,默认的Makefile规则已经包含了从.c.cpp构建.o的规则. make使用后缀.SUFFIXES目标列出.

For example, the default Makefile rules already include rules for building .o from either .c or .cpp. make simply searches for the first mathcing rule, using the suffixes listed for the .SUFFIXES target.

在您的情况下,您仅需要两个规则:

In your case you simply need two rules:

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
    g++ -c $(_DEPEND) -o $(_TARGET)

$(OBJDIR)/%.o: $(SRCDIR)/%.cxx
    g++ -c $(_DEPEND) -o $(_TARGET)

这篇关于C ++ Makefile-如何处理混合源文件后缀的规则(例如.cpp和.cxx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆