并行构建时QMake的PRE_TARGETDEPS失效问题求助
It sounds like you're hitting a classic parallel build dependency race condition—QMake logs show your script ran, but the compiler still grabs the stale generated.cpp before the script finishes updating it. Let's break down how to fix this properly:
1. Explicitly Link Your Custom Target to the Generated File
The key oversight here is probably not telling QMake exactly what file your Python script produces. Without this, QMake can't track the file's timestamp to enforce build order in parallel mode. Update your .pro file like this:
# Define your custom generation target generate_source.target = update_generated_cpp generate_source.commands = python $$PWD/your_resource_script.py # Critical: Tell QMake this target produces generated.cpp generate_source.output = $$OUT_PWD/generated.cpp # Mark the file as auto-generated so QMake doesn't treat it as a static source GENERATED += $$OUT_PWD/generated.cpp # Add the target to QMake's extra targets QMAKE_EXTRA_TARGETS += generate_source # Make your main target depend on the generation step PRE_TARGETDEPS += update_generated_cpp
Using $$OUT_PWD ensures the file is generated directly in your Android build directory (not the source tree), which avoids sync issues with the NDK's build cache.
2. Force the Generated File to Depend on Your Script
For parallel builds, you need to go a step further—tie generated.cpp itself to your custom target, so the compiler won't touch it until the script finishes:
# Add the generated file to your sources (you probably already do this) SOURCES += $$OUT_PWD/generated.cpp # Explicitly make generated.cpp depend on your generation target $$OUT_PWD/generated.cpp.depends = update_generated_cpp
This creates a direct dependency chain: compile generated.cpp → wait for update_generated_cpp target → run Python script. No more race conditions.
3. Clean Up Stale Build Artifacts
Android's NDK build system loves caching old files, even if you update the source. Add a clean step to your .pro to wipe the generated file when you run make clean:
QMAKE_CLEAN += $$OUT_PWD/generated.cpp
You can also modify your Python script to delete the old generated.cpp before writing the new one—this ensures there's no chance of the compiler picking up a partial or stale file mid-build.
4. Debugging Tips to Verify Fixes
- Add timestamp logging to your Python script: Print the current time and the path of the file you're writing, then check if this timestamp matches the file's modification time in your build directory.
- Temporarily disable parallel builds in QtCreator (Settings → Build & Run → Build Steps → uncheck "Parallel build") to confirm the issue is indeed race-related. If the build works correctly without parallelism, your dependency setup was the problem.
By explicitly defining what your script produces and linking that file directly to your build targets, you'll eliminate the parallel build race condition and ensure generated.cpp is always fresh when the compiler runs.
内容的提问来源于stack exchange,提问作者jpo38

