简体   繁体   中英

How can I use different rules in Makefile for two groups of files?

I have one Makefile to build an executable and a library. Executable consists of a lot of source files and library consists of one .cpp file. The difference between compilation of executable and library is -fPIC option.

There is a compilation rule:

%.o : %.cpp
        $(CXX) -c $(CXXFLAGS) $< -o $@

all: $(TARGET) $(TARGET_LIB)

$(TARGET): $(OBJS)
        $(CXX) $(LDFLAGS) -o $@ $^

$(TARGET_LIB): $(LIBOBJS)
        $(CXX) $(LDFLAGS) -fPIC -shared -o $@ $^

I tried to add compilation rule for library and got this:

lib.o : lib.cpp
        $(CXX) -fPIC -c $(CXXFLAGS) $< -o $@

%.o : %.cpp
        $(CXX) -c $(CXXFLAGS) $< -o $@

all: $(TARGET) $(TARGET_LIB)

$(TARGET): $(OBJS)
        $(CXX) $(LDFLAGS) -o $@ $^

$(TARGET_LIB): $(LIBOBJS)
        $(CXX) $(LDFLAGS) -fPIC -shared -o $@ $^

Unfortunately, only lib is compiled in this case. Second rule is omitted.

How can I use a rule for one file and different rule for group of other files?

If you just run make without a target, the first target gets built. So just put all back at the top and it should work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM