Modifying a makefile to compile .cc and .cpp files

独自空忆成欢 提交于 2020-01-06 03:18:41

问题


I am trying to modify my makefile to support .cpp and .cc, however, I keep getting an error such as

target `source/systemFuncs.cpp' doesn't match the target pattern

I am modifying an existing makefile that support .cc and I want to make it also compile .cpp, but I am unsure how. This was originally a make file for a nacl project.

How can I compile both .cpp and .cc?

Related content to the makefile:

x86_32_OBJS:=$(patsubst %.cc,%_32.o,$(CXX_SOURCES))
$(x86_32_OBJS) : %_32.o : %.cc $(THIS_MAKE)
    $(CXX) ${INCDIRS} -o $@ -c $< -m32 -O0 -g $(CXXFLAGS)

$(PROJECT)_x86_32.nexe : $(x86_32_OBJS)
    $(CXX) -o $@ $^ -m32 -O0 -g $(CXXFLAGS) $(LDFLAGS)

CXX_SOURCES has both .cc files AND .cpp files in them, so it needs to be able to compile both


回答1:


You haven't given us much to go on, but I'll make a guess. I think you added source/systemFuncs.cpp to CXX_SOURCES. Then Make hit this line:

x86_32_OBJS:=$(patsubst %.cc,%_32.o,$(CXX_SOURCES))

which replaced ".cc" with "_32.o", and left source/systemFuncs.cpp untouched. Make then tried to feed this name into a rule that expected "_32.o", and crashed.

Try this:

CPP_SOURCES += source/systemFuncs.cpp

x86_32_CPP_OBJS:=$(patsubst %.cpp,%_32.o,$(CPP_SOURCES))

$(x86_32_CPP_OBJS) : %_32.o : %.cpp $(THIS_MAKE)
    $(CXX) ${INCDIRS} -o $@ -c $< -m32 -O0 -g $(CXXFLAGS)

With luck, this will prove crude but effective. Further improvements will be possible later.

EDIT:
If both sets of filenames must be in one variable (CXX_SOURCES) you can separate them like this:

CC_SOURCES :=  $(filter %.cc, $(CXX_SOURCES))
CPP_SOURCES := $(filter %.cpp, $(CXX_SOURCES))

Does that suffice?




回答2:


You can create seperate but (for the execution) identical rules:

(I skipped THIS_MAKE, since it is empty in the code above, but you can add it)

# catch both
x86_32_OBJS:=$(addsuffix _32.o,$(basename $(CXX_SOURCES)))

# rule for .cc
$(patsubst %.cc,%_32.o,$(filter %.cc,$(CXX_SOURCES))):%_32.o: %.cc
     $(CXX) ${INCDIRS} -o $@ -c $< -m32 -O0 -g $(CXXFLAGS)

# rule for .cpp
$(patsubst %.cpp,%_32.o,$(filter %.cpp,$(CXX_SOURCES))):%_32.o: %.cpp
    $(CXX) ${INCDIRS} -o $@ -c $< -m32 -O0 -g $(CXXFLAGS)

$(PROJECT)_x86_32.nexe : $(x86_32_OBJS)
    $(CXX) -o $@ $^ -m32 -O0 -g $(CXXFLAGS) $(LDFLAGS)


来源:https://stackoverflow.com/questions/9781417/modifying-a-makefile-to-compile-cc-and-cpp-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!