makedepend equivalent for use with nmake?

2020-02-11 01:29发布

问题:

Just wondering if there is a 'makedepends' equivalent that ships with visual studio that I can use with nmake. Does anyone know?

回答1:

You can use the /showIncludes switch to cl.exe to list the headers #included by your source files. Nested includes are indicated by indentation with spaces. You can also turn on syntax-checking mode with the /Zs switch, to increase speed and avoid creation of .obj files.

If you have Perl and a version of uniq (e.g. from GnuWin32) installed, the following one-liner will dump the list of unique headers used by myfile.cpp:

cl /Zs /showIncludes /EHsc myfile.cpp | perl -ne "print if s/^Note: including file: *//" | sort | uniq

It should not be too difficult to pipe this through another script that creates the relevant nmake rules.



回答2:

I assume you use NMAKE to build project like me. I'm in need of makedepend-like tool in Windows, too. So, I use MinGW to generate header dependencies. First create Makefile to generate dependencies, which I named it Makedepends, like this:

    OBJS=... list object files in your project...

    all: Makefile.deps

    Makefile.deps: $(OBJS:.obj=.dep)
        cat $+ > $@
        rm -f $+

    %.dep: %.cpp
        g++ -MM -MG -MT$(@:.dep=.obj) -o$@ $<

In your Makefile that will be used by NMAKE, add this line at bottom:

    !INCLUDE Makefile.deps

When you want to create dependencies, run GMAKE like this:

    make -fMakedepends

And then, you can build your project with NMAKE as usual:

    nmake

PS: Sorry for poor language, I'm suck at writing. -_-



回答3:

.SUFFIXES:
.SUFFIXES: .c

all: x.obj

# Sample batch-mode rule which both compiles and produces .dep files suitable for NMAKE.
# Also works around the fact that CL.EXE spits diagnostics in stdout instead of stderr.
# This is equivalent to -MD -MP -MT$@ -MF$(@R).dep in GNU Make + GCC.
CCOMMAND = $(CC) $(CFLAGS) /c $<
.c.obj::
!IF "$(MAKEFLAGS:S=)" == "$(MAKEFLAGS)"
    @echo " $(CCOMMAND)"
!ENDIF
    @$(COMSPEC) /E:ON /V:ON /C "$(CCOMMAND) /showIncludes & echo Exit: !ERRORLEVEL!" | \
        $(COMSPEC) /E:ON /V:ON /C "for /f "tokens=1,* delims=]" %%A in ('find /v /n ""') do \
        @if %%~xB == .c (set _=%%~nB&rem.>!_!.dep&echo %%B) else for /f "tokens=1,2,3,*" %%C in ("%%B") do \
        @if %%C == Note: ((echo !_!.obj: "%%F"&echo "%%F":) >> !_!.dep) \
        else if %%C == Exit: (exit /b %%D) else echo %%B"

# Include the generated deps.
!IF ![(for %i in (*.dep) do @echo !INCLUDE %i) >Build.tmp]
!       INCLUDE Build.tmp
!       IF ![del Build.tmp]
!       ENDIF
!ENDIF