aboutsummaryrefslogtreecommitdiff
path: root/include/flatcc/portable/pcrt.h
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2023-07-16 02:03:33 +0200
committerToni Uhlig <matzeton@googlemail.com>2023-07-16 02:03:33 +0200
commit5a40295c4cf0af5ea8da9ced04a4ce7d3621a080 (patch)
treecb21506e7b04d10b45d6066a0ee1655563d5d52b /include/flatcc/portable/pcrt.h
Squashed 'flatcc/' content from commit 473da2a
git-subtree-dir: flatcc git-subtree-split: 473da2afa5ca435363f8c5e6569167aee6bc31c5
Diffstat (limited to 'include/flatcc/portable/pcrt.h')
-rw-r--r--include/flatcc/portable/pcrt.h48
1 files changed, 48 insertions, 0 deletions
diff --git a/include/flatcc/portable/pcrt.h b/include/flatcc/portable/pcrt.h
new file mode 100644
index 0000000..0226be6
--- /dev/null
+++ b/include/flatcc/portable/pcrt.h
@@ -0,0 +1,48 @@
+#ifndef PCRT_H
+#define PCRT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/*
+ * Assertions and pointer violations in debug mode may trigger a dialog
+ * on Windows. When running headless this is not helpful, but
+ * unfortunately it cannot be disabled with a compiler option so code
+ * must be injected into the runtime early in the main function.
+ * A call to the provided `init_headless_crt()` macro does this in
+ * a portable manner.
+ *
+ * See also:
+ * https://stackoverflow.com/questions/13943665/how-can-i-disable-the-debug-assertion-dialog-on-windows
+ */
+
+#if defined(_WIN32)
+
+#include <crtdbg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static int _portable_msvc_headless_report_hook(int reportType, char *message, int *returnValue)
+{
+ fprintf(stderr, "CRT[%d]: %s\n", reportType, message);
+ *returnValue = 1;
+ exit(1);
+ return 1;
+}
+
+#define init_headless_crt() _CrtSetReportHook(_portable_msvc_headless_report_hook)
+
+#else
+
+#define init_headless_crt() ((void)0)
+
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* PCRT_H */