简体   繁体   中英

How to use g++ -MM in a makefile

I can use the -MM option in g++ to generate the dependencies in a makefile rule format.

g++ -MM module2.cpp -I../src -I../../raven-set -I. -I../src/ext

outputs

module2.o: module2.cpp pch.h ../src/theGlobalDefines.h \
 ../../raven-set/raven_sqlite.h ../src/ext/sqlite3.h Module2.h \
 cPelexMixerComponent.h cErrorHandler.h cTimedEvent.h cPelexConfig.h \
 ../src/sgp.h ../src/cCircularVector.h ../../raven-set/cTimerBusyLoop.h \
 ../src/channelIdentification.h ../src/cPacketData.h cRxTx.h \
 cOutputTransmitter.h cDelayStats.h cMCUSB202.h cPeakerServer.h cInput.h \
 ../src/cSequenceNumber.h cRxPelexWireless.h ../../raven-set/cRunCount.h \
 cPeakFilter.h cSPO2StateMachine.h wrs_cProcessed.h ../src/log.h \
 wrs/cRaw.h wrs/cPacket.h wrs/cCalibrate.h wrs/cStream.h \
 ../src/cPelexMixerConfig.h ../src/ext/json.h wrs/cSignalProcessor_wrs.h \
 cD1ZeroCross.h ../src/cVitals.h cUI.h cTimeProfiler.h \
 ../licenser/cLicence.h cSignalProcessor.h ../src/cPeakFilterSet.h \
 ../src/cPeakFinder.h cDataRange.h cDerivativeTemplate.h \
 cDerivativePeak.h cInputRecord.h cSGPOutput.h cSignalProcessorConfig.h \
 cHeartRate.h ../src/StatusDB.h cLogger.h cKeyBoardMonitor.h \
 wrs/cStartSequence.h ../src/Configure.h ../src/HistoryDB.h \
 ../../raven-set/cRunWatch.h version.h

Now what do I do with this?

Is there a way for make to run the g++ -MM command and then use the generated rule?

Here is the makefile

#source file search paths
VPATH = wrs . ../src ../src/ext ../licenser

# compiler include search paths
INCS=-I../src -I../src/ext \
    -I. -I"C:\Users\Public\Documents\Measurement Computing\DAQ\C" \
    -I../../boost/boost1_72 

# libraries required by linker
LIBS=-lstdc++fs  -lws2_32 -lwsock32 \
    -L"C:\Users\Public\Documents\Measurement Computing\DAQ\C" \
    -lcbw64 -lIphlpapi \
    -L../../boost/boost1_72/lib \
                -lboost_thread-mgw82-mt-x64-1_72 \
            -lboost_system-mgw82-mt-x64-1_72 \
            -lboost_program_options-mgw82-mt-x64-1_72 \
            -lboost_filesystem-mgw82-mt-x64-1_72


# folder for .o files
ODIR=./obj

#  sources
_OBJ =      \
        cLicence.o  \
        sha1.o  \
        ChannelIdentification.o  \
        ChannelLabels.o  \
        Configure.o  \
        CubicSpline.o  \
        HistoryDB.o  \
        StatusDB.o  \
        cPacketData.o  \
        cPeakFilterSet.o  \
        cPeakFinder.o  \
        cPelexMixerConfig.o  \
        cVitals.o  \
        cRunWatch.o  \
        cSpline.o  \
        cTimerBusyLoop.o  \
        json.o  \
        raven_sqlite.o  \
        sqlite3.o \
        log.o  \
        cD1ZeroCross.o  \
        cDataRange.o  \
        cDelayStats.o  \
        cDerivativeTemplate.o  \
        cErrorHandler.o  \
        cHeartRate.o  \
        cInput.o  \
        cInputRecord.o  \
        cMCUSB202.o  \
        cOutputTransmitter.o  \
        cPacketAlpha.o  \
        cPacketWRS.o  \
        cPeakFilter.o  \
        cPeakFinderSustainedD1.o  \
        cPeakFinderTallPoppy.o  \
        cPeakerServer.o  \
        cPelexConfig.o  \
        cPelexMixerComponent.o  \
        cRxPelexWireless.o  \
        cRxTx.o  \
        cSGPOutput.o  \
        cSPO2StateMachine.o  \
        cSignalProcessor.o  \
        cTimeProfile.o  \
        cTimedEvent.o  \
        cUI.o  \
        module2.o  \
        sgp.o  \
        cCalibrate.o  \
        cRaw.o  \
        cSignalProcessor_wrs.o  \
        cStartSequence.o  \
        cStream.o  \
        wrs_cProcessed.o

OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/sqlite3.o: sqlite3.c
    gcc  -c -o $@ $<
$(ODIR)/%.o: %.cpp
    g++ -std=c++17 -m64 -fexceptions -D_mingw_ -DMODULE2 -O2 \
         -c -o $@ $< $(INCS)

mixer: $(OBJ) 
    g++ -m64 -O2 -s -o ../bin/PelexMixer.exe $^ $(LIBS)

I need to do the following steps to generate, store and include the dependency files

This follows the hints in the link provided by @GM and helpful comments added to this answer

Step 1: define some flags requesting the generation and storage of dependencies. Notice that I am storing both the .o and the .d files in the same folder - makes things a bit simpler

# flags requesting dependency generation
DEPFLAGS = -MT $@ -MMD -MP -MF $(ODIR)/$*.d

Step 2 add flags to compilation rule so that they will be generated as we go along

$(ODIR)/%.o: %.cpp
    g++ -std=c++17 -m64 -fexceptions -D_mingw_ -DMODULE2 -O2 \
        $(DEPFLAGS) \
         -c -o $@ $< $(INCS)

Step 3 Include the dependency files

    # convert list of object files to list of dependency files
    DEPFILES := $(_OBJ:%.o=$(ODIR)/%.d)

    # empty rule, so make won't complain
    # about not having a rule to make the dependency file if missing
    $(DEPFILES):

    # include the dependency files 

    include $(wildcard $(DEPFILES))

Here is my complete makefile with the changes described above

#source file search paths
VPATH = wrs . ../src ../src/ext ../licenser

# compiler include search paths
INCS=-I../src -I../src/ext \
    -I. -I"C:\Users\Public\Documents\Measurement Computing\DAQ\C" \
    -I../../boost/boost1_72 

# libraries required by linker
LIBS=-lstdc++fs  -lws2_32 -lwsock32 \
    -L"C:\Users\Public\Documents\Measurement Computing\DAQ\C" \
    -lcbw64 -lIphlpapi \
    -L../../boost/boost1_72/lib \
                -lboost_thread-mgw82-mt-x64-1_72 \
            -lboost_system-mgw82-mt-x64-1_72 \
            -lboost_program_options-mgw82-mt-x64-1_72 \
            -lboost_filesystem-mgw82-mt-x64-1_72


# folder for .o files and depedency files
ODIR = ../pelexmixer/obj

# flags requesting dependency generation
DEPFLAGS = -MT $@ -MMD -MP -MF $(ODIR)/$*.d

#  sources
_OBJ =      \
        cLicence.o  \
        sha1.o  \
        ChannelIdentification.o  \
        ChannelLabels.o  \
        Configure.o  \
        CubicSpline.o  \
        HistoryDB.o  \
        StatusDB.o  \
        cPacketData.o  \
        cPeakFilterSet.o  \
        cPeakFinder.o  \
        cPelexMixerConfig.o  \
        cVitals.o  \
        cRunWatch.o  \
        cSpline.o  \
        cTimerBusyLoop.o  \
        json.o  \
        raven_sqlite.o  \
        sqlite3.o \
        log.o  \
        cD1ZeroCross.o  \
        cDataRange.o  \
        cDelayStats.o  \
        cDerivativeTemplate.o  \
        cErrorHandler.o  \
        cHeartRate.o  \
        cInput.o  \
        cInputRecord.o  \
        cMCUSB202.o  \
        cOutputTransmitter.o  \
        cPacketAlpha.o  \
        cPacketWRS.o  \
        cPeakFilter.o  \
        cPeakFinderSustainedD1.o  \
        cPeakFinderTallPoppy.o  \
        cPeakerServer.o  \
        cPelexConfig.o  \
        cPelexMixerComponent.o  \
        cRxPelexWireless.o  \
        cRxTx.o  \
        cSGPOutput.o  \
        cSPO2StateMachine.o  \
        cSignalProcessor.o  \
        cTimeProfile.o  \
        cTimedEvent.o  \
        cUI.o  \
        module2.o  \
        sgp.o  \
        cCalibrate.o  \
        cRaw.o  \
        cSignalProcessor_wrs.o  \
        cStartSequence.o  \
        cStream.o  \
        wrs_cProcessed.o

OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

$(ODIR)/sqlite3.o: sqlite3.c
    gcc  -c -o $@ $<
$(ODIR)/%.o: %.cpp
    g++ -std=c++17 -m64 -fexceptions -D_mingw_ -DMODULE2 -O2 \
        $(DEPFLAGS) \
         -c -o $@ $< $(INCS)

mixer: $(OBJ) 
    g++ -m64 -O2 -s -o ../bin/PelexMixer.exe $^ $(LIBS)

DEPFILES := $(_OBJ:%.o=$(ODIR)/%.d)

$(DEPFILES):

include $(wildcard $(DEPFILES))

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