makefile and header files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HT-Lab

    #1

    makefile and header files

    Hi All,

    Not strictly a C++ question but I am sure this is a common compile issue :-)

    I have a generic makefile (see below) that I would like update such that if
    I make some changes to a header file the associated cpp file is recompiled.
    I tried using constructs like:

    ..h.cpp:
    touch $<

    thinking that if make detects a change in file.h it will touch file.cpp
    which will then result in .cpp.o rule being executed. I tried this and some
    other permutations but they all failed.

    Any idea how I can fix this issue?

    Thanks,
    Hans.


    # Specify all source
    SRCS = sc_main.cpp

    # Target file
    TARGET = run

    # Variable that points to SystemC installation path
    SYSTEMC = /usr/local/systemc-2.2

    INCDIR = -I. -I.. -I$(SYSTEMC)/include
    LIBDIR = -L. -L.. -L$(SYSTEMC)/lib-cygwin
    LIBS = -lsystemc -lm

    CC = g++
    CFLAGS = -g -Wno-deprecated -Wall
    OBJS = $(SRCS:.cpp=.o)

    EXE = $(TARGET).exe

    ..SUFFIXES: .cpp .o

    $(EXE): $(OBJS)
    $(CC) $(CFLAGS) $(INCDIR) $(LIBDIR) -o $@ $(OBJS) $(LIBS) 2>&1 | c++filt

    all: $(EXE)

    ..cpp.o:
    $(CC) $(CFLAGS) $(INCDIR) -c $<

    clean:
    rm -f $(OBJS) *~ $(EXE) *.bak $(TARGET).exe.s tackdump



  • Michael DOUBEZ

    #2
    Re: makefile and header files

    HT-Lab a écrit :
    Hi All,
    >
    Not strictly a C++ question but I am sure this is a common compile issue :-)
    >
    I have a generic makefile (see below) that I would like update such that if
    I make some changes to a header file the associated cpp file is recompiled.
    I tried using constructs like:
    >
    .h.cpp:
    touch $<
    >
    thinking that if make detects a change in file.h it will touch file.cpp
    which will then result in .cpp.o rule being executed. I tried this and some
    other permutations but they all failed.
    >
    Any idea how I can fix this issue?
    No but if all you need is recompilation because of header change, the
    usual method is to add the rule:

    monobjet.o: monheader.h myheader.h meinheader.h

    Using gcc, you can automatically generate dependency files through the
    -M (or -MM) flags (man gcc for more info).

    Michael

    Comment

    • Lionel B

      #3
      Re: [OT] makefile and header files

      On Thu, 12 Jul 2007 11:40:25 +0200, Michael DOUBEZ wrote:
      HT-Lab a écrit :
      >Hi All,
      >>
      >Not strictly a C++ question but I am sure this is a common compile
      >issue :-)
      >>
      >I have a generic makefile (see below) that I would like update such
      >that if I make some changes to a header file the associated cpp file is
      >recompiled. I tried using constructs like:
      >>
      >.h.cpp:
      > touch $<
      >>
      >thinking that if make detects a change in file.h it will touch file.cpp
      >which will then result in .cpp.o rule being executed. I tried this and
      >some other permutations but they all failed.
      >>
      >Any idea how I can fix this issue?
      >
      No but if all you need is recompilation because of header change, the
      usual method is to add the rule:
      >
      monobjet.o: monheader.h myheader.h meinheader.h
      >
      Using gcc, you can automatically generate dependency files through the
      -M (or -MM) flags (man gcc for more info).
      If it's of interest to anyone, here's a really neat 'make' idiom that
      updates dependencies "on the fly" (can't remember where I found it... I
      think GNU autotools does something similar):

      SRCS = ...
      OBJS = $(patsubst %.cpp,%.o,$(SRC S))
      DEPS = $(patsubst %.o,%.d,$(OBJS) )

      CFLAGS = ...

      ....

      $(OBJS): %.o: %.cpp
      $(CXX) -c -MMD -MP $(CFLAGS) $< -o $@
      @sed -i -e '1s,\($*\)\.o[ :]*,\1.o $*.d: ,' $*.d

      -include $(DEPS)

      This creates a .d dependency file for every .o file in the same pass as
      compilation (the point is you only ever need updated dependencies at the
      *next* build...). The odd-looking 'sed' line makes the .d file itself
      depend on relevant source and headers, while the -MP adds a 'phony'
      target for all prerequisite headers to stop 'make' complaining if you
      remove a header. The -include stops 'make' complaining if any .d files
      are not found (eg. on initial build).

      It's all pretty seamless - write once and never touch anything (pun
      intended) again.

      --
      Lionel B

      Comment

      • Rui Maciel

        #4
        Re: makefile and header files

        HT-Lab wrote:
        Hi All,
        >
        Not strictly a C++ question but I am sure this is a common compile issue
        :-)
        >
        I have a generic makefile (see below) that I would like update such that
        if I make some changes to a header file the associated cpp file is
        recompiled. I tried using constructs like:
        >
        .h.cpp:
        touch $<
        >
        thinking that if make detects a change in file.h it will touch file.cpp
        which will then result in .cpp.o rule being executed. I tried this and
        some other permutations but they all failed.
        You don't need any complex solution to be able to do this. As I understand,
        he makefile structure is basically this:

        <makefile>
        target: files
        command
        </makefile>

        The target command is executed if any file in the given list is modified.
        So, the only thing you have to do is include the header file as one of the
        target's files. That's pretty much the very basic concept behind a
        makefile: define a target, define a list of files that trigger that target
        and associate a series of commands to that target.

        Hope this helps
        Rui Maciel

        Comment

        • Victor Bazarov

          #5
          Re: makefile and header files

          HT-Lab wrote:
          Not strictly a C++ question but I am sure this is a common compile
          issue :-)
          Nope. Many here don't even use makefiles. Resolution: add the .h
          file as a dependency of your object file.
          I have a generic makefile [..]
          Or post to the newsgroup for your OS or your compiler.

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • James Kanze

            #6
            Re: makefile and header files

            On Jul 12, 6:34 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
            HT-Lab wrote:
            Not strictly a C++ question but I am sure this is a common compile
            issue :-)
            Nope. Many here don't even use makefiles.
            I agree (for once) that the question really isn't on topic (and
            that it would be better asked in a group for the OS), but
            seriously, does anyone really develope software without using
            makefiles? (The problem is, of course, that make is not always
            make. Even between two different Unixes, the makefiles often
            aren't compatible, and this time, Microsoft fits right in with
            Unix---their make isn't compatbile with anyone elses either:-).)

            --
            James Kanze (Gabi Software) email: james.kanze@gma il.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • Victor Bazarov

              #7
              Re: makefile and header files

              James Kanze wrote:
              On Jul 12, 6:34 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
              >HT-Lab wrote:
              >>Not strictly a C++ question but I am sure this is a common compile
              >>issue :-)
              >
              >Nope. Many here don't even use makefiles.
              >
              I agree (for once) that the question really isn't on topic (and
              that it would be better asked in a group for the OS), but
              seriously, does anyone really develope software without using
              makefiles? (The problem is, of course, that make is not always
              make. Even between two different Unixes, the makefiles often
              aren't compatible, and this time, Microsoft fits right in with
              Unix---their make isn't compatbile with anyone elses either:-).)
              Well, they do have 'NMAKE' utility, but usually when one develops
              using MS Visual Studio, they don't use NMAKE, they use the 'cl'
              that knows how to deal with "dsw" ("sln") and "dsp" ("vcproj")
              files, which are a sort of makefile, but not really. They just
              have slightly different information in them, although the purpose
              is the same, essentially.

              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask


              Comment

              • HT-Lab

                #8
                Re: makefile and header files

                Thanks for all replies guys,

                Regards,
                Hans.

                "Victor Bazarov" <v.Abazarov@com Acast.netwrote in message
                news:f75ttv$rna $1@news.datemas .de...
                James Kanze wrote:
                >On Jul 12, 6:34 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
                >>HT-Lab wrote:
                >>>Not strictly a C++ question but I am sure this is a common compile
                >>>issue :-)
                >>
                >>Nope. Many here don't even use makefiles.
                >>
                >I agree (for once) that the question really isn't on topic (and
                >that it would be better asked in a group for the OS), but
                >seriously, does anyone really develope software without using
                >makefiles? (The problem is, of course, that make is not always
                >make. Even between two different Unixes, the makefiles often
                >aren't compatible, and this time, Microsoft fits right in with
                >Unix---their make isn't compatbile with anyone elses either:-).)
                >
                Well, they do have 'NMAKE' utility, but usually when one develops
                using MS Visual Studio, they don't use NMAKE, they use the 'cl'
                that knows how to deal with "dsw" ("sln") and "dsp" ("vcproj")
                files, which are a sort of makefile, but not really. They just
                have slightly different information in them, although the purpose
                is the same, essentially.
                >
                V
                --
                Please remove capital 'A's when replying by e-mail
                I do not respond to top-posted replies, please don't ask
                >

                Comment

                Working...