# How to make a source file compile before any others within a target

**URL:** https://discourse.cmake.org/t/how-to-make-a-source-file-compile-before-any-others-within-a-target/14231
**Category:** Code
**Tags:** os:windows, comp:msvc, gen:ninja
**Created:** [June 12, 2025, 4:18pm UTC](https://discourse.cmake.org/t/how-to-make-a-source-file-compile-before-any-others-within-a-target/14231 "2025-06-12T16:18:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![huangqinjin](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/f0a364/32.png) [@huangqinjin](https://discourse.cmake.org/u/huangqinjin)
#### Post date: [June 12, 2025, 4:18pm UTC](https://discourse.cmake.org/t/how-to-make-a-source-file-compile-before-any-others-within-a-target/14231/1 "2025-06-12T16:18:50Z")

</div>

I have a target `hello` with two source files `a.cpp` and `b.cpp`. And both contain the `#import` directive:

```c++
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")  

```

which will generate the header file `msado15.tlh` during compilation.

Ninja builds source files in parallel which causes race on the header file generation.  
This is related to [Developer Community](https://developercommunity.visualstudio.com/t/10736559).

I tried `OBJECT_DEPENDS`, it worked well, but the path to the output object file is implementation detail.

```cmake
set_source_files_properties(b.cpp PROPERTIES OBJECT_DEPENDS "CMakeFiles/hello.dir/a.cpp.obj")

```

Using PCH should work also. But is there an official way to make a source file compile before any others?

---

<div class="post-metadata">

### Author: ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)
#### Post date: [June 12, 2025, 10:47pm UTC](https://discourse.cmake.org/t/how-to-make-a-source-file-compile-before-any-others-within-a-target/14231/2 "2025-06-12T22:47:21Z")

</div>

`#import` is effectively a code generation step, ie it has outputs that further steps in the build graph depend on.

Worse, it is a code generation step where the output of the step is dependent on the content of the input to the step. The generated header files are based on what set of symbols is present in the `#import` directive.

CMake has no general purpose mechanism for modeling this, it’s a known weakness and oft-requested feature. `OBJECT_DEPENDS` is a reasonable work around. Normally you could get the object name with `$<TARGET_OBJECTS:tgt>` but we don’t support generator expressions in `OBJECT_DEPENDS`, see:

- [https://gitlab.kitware.com/cmake/cmake/-/issues/22034](https://gitlab.kitware.com/cmake/cmake/-/issues/22034)
- [https://gitlab.kitware.com/cmake/cmake/-/issues/19467](https://gitlab.kitware.com/cmake/cmake/-/issues/19467)

So ya, it’s a hard problem (even MSBuild doesn’t solve it very well), your solution isn’t bad.

---

<div class="post-metadata">

### Author: ![huangqinjin](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/f0a364/32.png) [@huangqinjin](https://discourse.cmake.org/u/huangqinjin)
#### Post date: [June 29, 2025, 2:34pm UTC](https://discourse.cmake.org/t/how-to-make-a-source-file-compile-before-any-others-within-a-target/14231/3 "2025-06-29T14:34:55Z")

</div>

I found that even the document says `OBJECT_DEPENDS` is a list of full-paths to files, it doesn’t need to be actually. With `OBJECT_OUTPUTS` and `OBJECT_DEPENDS`, I can accomplish the goal perfectly:

```cmake
set_source_files_properties(a.cpp PROPERTIES OBJECT_OUTPUTS "hello_first_obj")
set_source_files_properties(b.cpp PROPERTIES OBJECT_DEPENDS "hello_first_obj")

```

`hello_first_obj` can be any string, it doesn’t need to be a path to a file. At least for Ninja generator it works.
