# Running a batch script before object files are compiled

**URL:** https://discourse.cmake.org/t/running-a-batch-script-before-object-files-are-compiled/14069
**Category:** Usage
**Created:** [May 9, 2025, 2:46pm UTC](https://discourse.cmake.org/t/running-a-batch-script-before-object-files-are-compiled/14069 "2025-05-09T14:46:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![LarryOates](https://discourse.cmake.org/user_avatar/discourse.cmake.org/larryoates/32/3654_2.png) [@LarryOates](https://discourse.cmake.org/u/LarryOates)
#### Post date: [May 9, 2025, 2:46pm UTC](https://discourse.cmake.org/t/running-a-batch-script-before-object-files-are-compiled/14069/1 "2025-05-09T14:46:21Z")

</div>

I have a batch script which generates a .c file (fsdata\_custom.c)  
I need to run this script every time I build my aplication but before the application object files are built.

So far I have:

# Build FS data for display webpage

add\_custom\_target(createCustomFSOnChange DEPEND ALL  
COMMAND ${CMAKE\_SOURCE\_DIR}/Scripts/create\_fsdata\_custom.bat ${CMAKE\_BUILD\_TYPE}  
COMMENT “Validating HMTL FS”)

add\_executable(${EXECUTABLE} ./core/src/main.c)  
add\_dependencies(${EXECUTABLE} createCustomFSOnChange)

The script runs but after the fsdata\_custom.c had been built into an object so the old version is linked into the executable.  
I would really apreciate some help on where I’m going wrong!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 9, 2025, 3:35pm UTC](https://discourse.cmake.org/t/running-a-batch-script-before-object-files-are-compiled/14069/2 "2025-05-09T15:35:06Z")

</div>

You want to use `add_custom_command`.

```cmake
add_custom_command(
  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/fsdata_custom.$<CONFIG>.c"
  DEPENDS "${CMAKE_SOURCE_DIR}/Scripts/create_fsdata_custom.bat"
  COMMAND "${CMAKE_SOURCE_DIR}/Scripts/create_fsdata_custom.bat" "$<CONFIG>"
  COMMENT "Validating HMTL FS")
add_executable("${EXECUTABLE}" core/src/main.c "${CMAKE_CURRENT_BINARY_DIR}/fsdata_custom.$<CONFIG>.c")

```

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [May 10, 2025, 3:54am UTC](https://discourse.cmake.org/t/running-a-batch-script-before-object-files-are-compiled/14069/3 "2025-05-10T03:54:59Z")

</div>

@ben.boeckel `CONFIG` is not a known keyword for the `add_custom_command()` command. I think you probably mean `COMMAND` instead?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 12, 2025, 5:11am UTC](https://discourse.cmake.org/t/running-a-batch-script-before-object-files-are-compiled/14069/4 "2025-05-12T05:11:46Z")

</div>

D’oh, yes. Updated.
