Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7192117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:52:26+00:00 2026-05-28T19:52:26+00:00

I need to write a pre-build makefile which is called separately from the main

  • 0

I need to write a pre-build makefile which is called separately from the main build file. This make file should make a walk in a directory where it is called. There is a list of directories in another make file called ‘sources.mk’ with a variable which describe the directory:

SUBDIRS := \
. \
directory1 \
directory2 \

Now, I need to run a loop through this list and in this loop I need to call an utility which will process all file with a ‘h’ extension. I wrote this:

include Default/sources.mk

find_files:
    for prefix in $(SUBDIRS); do \
        for file in *.h; do \
            C:/QtSDK/Desktop/Qt/4.7.4/mingw/bin/moc.exe $$prefix/$$file; \
        done \
    done

Run command: make -f premake.mk

I don’t describe the errors, there are a lot of them, I was trying different makefiles, but I am a newbie at it and these attempts failed. Please, review my code and/or suggest other methods.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:52:27+00:00Added an answer on May 28, 2026 at 7:52 pm

    Your problem is probably just this one simple thing: You’re looking for file in *.h in the current directory, not in the subdirectory. Try this instead:

    for prefix in $(SUBDIRS); do \
        for file in $$prefix/*.h; do \
            C:/QtSDK/Desktop/Qt/4.7.4/mingw/bin/moc.exe $$file; \
        done \
    done
    

    With that said, a much better way of doing this is to use make to handle the processing of all of the files (and deciding whether or not all of them need to be reprocessed!), rather than using an explicit loop in the rule. You’d start with a list of header files, as Eldar Abusalimov’s answer suggests:

    moc_headers := $(wildcard $(SUBDIRS:%=%/*.h))
    

    The inner piece of that manipulates the SUBDIRS list into a form directory1/*.h, directory2/*.h, and so on, and then the wildcard function expands all the *.h patterns.

    Then, you generate the list of output files from them:

    moc_mocfiles := $(patsubst %.h, %_moc.cpp, $(moc_headers))
    

    This takes that expanded list of header files directory1/header1.h, directory1/header2.h, and so on, and substitutes the %.h pattern with %_moc.cpp. (Note that, because these names all have the directory name as part of the name, you can’t easily use the more common moc_%.h name pattern, because you’d get moc_directory1/header1.cpp, not the desired directory1/moc_header1.cpp. There are ways to get around that, but it’s easier to avoid the problem.) In any case, this gives you a list of output files: directory1/header1_moc.cpp, directory1/header2_moc.cpp, and so on.

    Now that you have a list of output files, Make knows how to iterate over those pretty easily. You just declare that list as a prerequisite of some other target that you’re making, for instance:

    find_files: $(moc_mocfiles)
    

    And, finally, you give make a generic rule for making a *_moc.cpp file from a *.h file:

    %_moc.cpp: %.h
         C:/QtSDK/Desktop/Qt/4.7.4/mingw/bin/moc.exe $< -o $@
    

    There, the first line indicates “this is how you make a file that fits the %_moc.cpp pattern, if you have a file fitting the %.h pattern to make it from”. In the second line, the $< becomes the input file (the %.h file), and the $@ becomes the output file. Here, you’re explicitly telling moc.exe with the -o option to spit out a file with the %_moc.cpp name rather than whatever it uses by default.

    So, putting all this together, when you make the find_files target, make will realize that it needs to make all those %_moc.cpp files in the moc_mocfiles list, and for each one it will see that it has a possible rule that fits, it will see that the rule applies because the corresponding %.h file exists, and it will apply the rule.

    This also has the advantage that, if the %_moc.cpp file already exists and is newer than the %.h file, indicating that it’s already up-to-date, it won’t bother regenerating it next time you run make. It will only regenerate the files corresponding to the %.h files you’ve edited.

    (Oh, and one last thing: When cutting-and-pasting all these things from this answer, make sure you get your tabs in the right places!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to do the following: Write pre-commit hook in Perl Hook should check
I need some help understanding this bit of code pre { white-space: pre; white-space:
I need write an update statement that used multiple tables to determine which rows
I need to write a Delphi application that pulls entries up from various tables
I need to write a java script. This is supposed to validate if the
I need to write code that picks up PGP-encrypted files from an FTP location
I use homebrew,write this order to terminal.app to install postgresql. `brew install postgresql` <pre>==>
I need to write a lib in my iOS app. The statement should be
I need to write a 'server' in c# (.Net 2.0) which would process requests
I need to write a program used internally where different users will have different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.