Eclipse构建完成后能否自动运行指定命令?(CDT环境)
Your goal to trigger zc-bg-notify after Eclipse CDT finishes building your STM32 project (using an external Makefile in the Debug directory) is totally achievable. Let’s walk through the steps to make this work, including capturing project names, warning/error counts, and integrating the notification into your build workflow.
Step 1: Modify Your Debug Makefile to Capture Build Results
Since you’re using an external Makefile, the cleanest way to trigger the notification is to add post-build logic directly to it. This ensures we can accurately capture GCC’s warning/error output and include your project name.
Here’s a snippet to add to your Makefile (adjust the build commands to match your existing setup):
# Enable single-shell execution to preserve variable values across commands .ONESHELL: # Your primary build target (e.g., 'all') all: # Run existing build commands, redirecting all output to a log file gcc $(CFLAGS) -o $(TARGET) $(SOURCES) 2>&1 | tee build_output.log # Count warnings and errors from the build log WARN_COUNT=$(grep -c "warning:" build_output.log) ERROR_COUNT=$(grep -c "error:" build_output.log) # Grab the project name (uses the parent folder of your Debug directory) PROJECT_NAME=$(notdir $(CURDIR)/..) # Trigger the formatted notification zc-bg-notify -q "Build finished for *$(PROJECT_NAME)*... with $$WARN_COUNT warnings $$ERROR_COUNT errors"
Key Notes for the Makefile:
2>&1redirects GCC’s stderr (where warnings/errors are printed) to stdout, soteecan capture it inbuild_output.log.- Use
$$WARN_COUNTinstead of$WARN_COUNT—Make interprets single$as its own variable syntax, so double dollars pass the shell variable correctly. $(notdir $(CURDIR)/..)pulls the parent folder name of your Debug directory (your project name); tweak this if your directory structure differs.
Step 2: Configure Eclipse’s External Builder
Ensure Eclipse runs the modified Makefile correctly:
- Open Run > External Tools > External Tools Configurations.
- Create a new Program configuration:
- Location: Path to your
makeexecutable (e.g.,/usr/bin/makeor the path from your GCC toolchain). - Working Directory: Set to
${workspace_loc:/YourProjectName/Debug}(replaceYourProjectNamewith your actual project name). - Arguments: Add
all(or your primary build target) to trigger the modified target.
- Location: Path to your
- Save the configuration and test it—you should see the notification pop up in your active Zsh shells after the build completes.
Alternative: Eclipse Post-Build Steps (No Makefile Edits)
If you prefer not to modify your Makefile, add a post-build step in Eclipse:
- Go to Project > Properties > C/C++ Build > Settings > Build Steps.
- In the Post-build steps tab, add this command:
PROJECT_NAME=$(basename "${PWD}/..") WARN_COUNT=$(grep -c "warning:" "${PWD}/build_output.log") ERROR_COUNT=$(grep -c "error:" "${PWD}/build_output.log") zc-bg-notify -q "Build finished for *${PROJECT_NAME}*... with ${WARN_COUNT} warnings ${ERROR_COUNT} errors" - Ensure your Makefile still redirects build output to
build_output.log(as shown in Step 1) so this command can parse results.
Final Checks
- Confirm
zc-bg-notifyis in your system’sPATH(use its full path like/usr/local/bin/zc-bg-notifyif it’s not). - Test the build manually in the Debug directory first (run
make all) to verify the notification works before relying on Eclipse.
内容的提问来源于stack exchange,提问作者One Other

