Skip to content

Commit 3272a85

Browse files
authored
fix(blog): Update the pre-include method (#605)
and explain that it's not recommended in general and refer to the mdns solution
1 parent 036682a commit 3272a85

File tree

1 file changed

+22
-22
lines changed
  • content/blog/2025/11/advanced-porting-libraries-as-components

1 file changed

+22
-22
lines changed

content/blog/2025/11/advanced-porting-libraries-as-components/index.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -225,34 +225,34 @@ By placing a header file with the same name in the same directory (or earlier in
225225

226226
**Pre-include Method**
227227

228-
The pre-include method uses the `-include` compiler flag to automatically include a header before every source file is compiled. This is useful for:
228+
> **Note:** The pre-include method is not recommended in general and should only be used as a last resort when other dependency injection techniques are not feasible.
229229
230-
- Injecting dependency definitions
231-
- Providing platform-specific macros
232-
- Replacing standard library functions
230+
Another approach to dependency injection is the pre-include method, which uses the `-include` compiler flag to automatically include a header before every source file is compiled. This technique can be useful for injecting dependency definitions, providing platform-specific macros, or replacing standard library functions, but it comes with significant drawbacks that make it less desirable than other methods.
233231

234-
Here's an example pattern from a unit test implementation that uses pre-inclusion to inject test dependencies:
232+
The pre-include method works by instructing the compiler to include a header file before processing each source file, effectively injecting code at the top of every compilation unit. This can be particularly useful in test scenarios where you need to mock dependencies across multiple files.
233+
234+
For example, an mDNS test implementation uses pre-inclusion to inject test dependencies. In a Makefile, this might look like:
235+
236+
```makefile
237+
MDNS_C_DEPENDENCY_INJECTION=-include mdns_di.h
238+
239+
mdns.o: ../../mdns.c
240+
@echo "[CC] $<"
241+
@$(CC) $(CFLAGS) -include mdns_mock.h $(MDNS_C_DEPENDENCY_INJECTION) -c $< -o $@
242+
```
243+
244+
The equivalent in CMake:
235245

236246
```cmake
237-
if(CONFIG_MB_UTEST)
238-
set(dep_inj_dir "${CMAKE_CURRENT_LIST_DIR}/freemodbus/unit_test")
239-
list(APPEND priv_include_dirs "${dep_inj_dir}/include")
240-
foreach(src ${srcs})
241-
get_filename_component(src_wo_ext ${src} NAME_WE)
242-
if(EXISTS "${dep_inj_dir}/src/${src_wo_ext}_test.c")
243-
list(REMOVE_ITEM srcs ${src})
244-
list(APPEND srcs "${dep_inj_dir}/src/${src_wo_ext}_test.c")
245-
set_property(SOURCE ${src} APPEND_STRING PROPERTY COMPILE_FLAGS
246-
" -include ${dep_inj_dir}/include/${src_wo_ext}_test.h -include ${dep_inj_dir}/include/dep_inject.h")
247-
endif()
248-
endforeach()
249-
endif()
247+
set(MDNS_C_DEPENDENCY_INJECTION "-include mdns_di.h")
248+
249+
set_source_files_properties(
250+
${CMAKE_CURRENT_SOURCE_DIR}/../../mdns.c
251+
PROPERTIES COMPILE_FLAGS "-include mdns_mock.h ${MDNS_C_DEPENDENCY_INJECTION}"
252+
)
250253
```
251254

252-
This pattern:
253-
1. Checks if a test version of a source file exists
254-
2. Replaces the original source with the test version
255-
3. Pre-includes test headers that provide mock implementations
255+
In both cases, the pattern defines a variable containing the pre-include flag for dependency injection and uses it in the compilation command along with other pre-includes. This ensures that the dependency injection header is included before the source file is compiled, allowing mock implementations to be injected seamlessly. However, because this affects every compilation unit and can lead to unexpected side effects, it should be considered a last resort when cleaner dependency injection methods are not available.
256256

257257
### Linker Wrapping
258258

0 commit comments

Comments
 (0)