Skip to content

Commit b4740f1

Browse files
Fix ort_genai_c.h compilation in C mode by using conditional includes (microsoft#1516)
The `ort_genai_c.h` header was failing to compile when used as a C file due to the inclusion of `<cstddef>`, which is a C++-only header. ## Problem ```c // This would fail when compiling as C #include "ort_genai_c.h" ``` Error: ``` onnxruntime-genai/src/ort_genai_c.h:7:10: fatal error: 'cstddef' file not found #include <cstddef> ^~~~~~~~~ 1 error generated. ``` ## Solution Replaced the unconditional `#include <cstddef>` with conditional includes: ```c #ifdef __cplusplus # include <cstddef> #else # include <stddef.h> # include <stdbool.h> #endif ``` This ensures: - **C++ compilation**: Uses `<cstddef>` (provides `size_t`) - **C compilation**: Uses `<stddef.h>` (provides `size_t`) and `<stdbool.h>` (provides `bool`) ## Testing Verified the fix works with both clang and gcc in C and C++ modes: - ✅ C compilation with clang - ✅ C compilation with gcc - ✅ C++ compilation with clang++ - ✅ C++ compilation with g++ The change is minimal (7 lines added, 1 removed) and preserves all existing functionality while enabling the header to be used from pure C code as intended. Fixes microsoft#1512. --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: baijumeswani <[email protected]> Co-authored-by: Baiju Meswani <[email protected]>
1 parent d28cf8e commit b4740f1

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/ort_genai_c.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
#pragma once
55

66
#include <stdint.h>
7+
8+
#ifdef __cplusplus
79
#include <cstddef>
10+
#else
11+
#include <stddef.h>
12+
#include <stdbool.h>
13+
#endif
814

915
#ifdef __cplusplus
1016
extern "C" {
@@ -689,4 +695,4 @@ OGA_EXPORT OgaResult* OGA_API_CALL OgaSetActiveAdapter(OgaGenerator* generator,
689695
#ifdef __cplusplus
690696
}
691697
#endif
692-
//! @}
698+
//! @}

0 commit comments

Comments
 (0)