Skip to content

Commit efea3c3

Browse files
committed
another approach for windows
1 parent 1aae88e commit efea3c3

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

src/cpp11tesseract_types.h

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,59 @@
1-
// bypass C++ functions that are not CRAN-compliant
2-
#ifndef SYMBOL_INTERCEPTORS_H
3-
#define SYMBOL_INTERCEPTORS_H
1+
// Include R's C API for Windows to intercept problematic symbols
2+
#ifdef _WIN32
3+
// Include R headers first
4+
#include <R.h>
5+
#include <Rinternals.h>
46

5-
// Only apply these redirections when building for Windows
6-
#if defined(_WIN32)
7+
// Use a namespace to avoid conflicts
8+
namespace tesseract_r_wrapper {
9+
// Safe versions that don't terminate R
10+
inline void safe_abort() {
11+
REprintf("Internal error detected in tesseract (abort call intercepted)\n");
12+
}
13+
14+
inline void safe_exit(int status) {
15+
REprintf("Exit requested with status %d (intercepted)\n", status);
16+
}
717

18+
inline int safe_rand() { return 0; }
19+
inline void safe_srand(unsigned int seed) {}
20+
} // namespace tesseract_r_wrapper
21+
22+
// Only include the C++ header interceptors after defining our safe functions
823
#include <cstdlib>
924
#include <iostream>
1025

26+
// Override problematic functions after including standard headers
27+
#ifdef abort
28+
#undef abort
29+
#endif
30+
#define abort tesseract_r_wrapper::safe_abort
31+
32+
#ifdef exit
33+
#undef exit
34+
#endif
35+
#define exit(x) tesseract_r_wrapper::safe_exit(x)
36+
37+
#ifdef rand
38+
#undef rand
39+
#endif
40+
#define rand tesseract_r_wrapper::safe_rand
41+
42+
#ifdef srand
43+
#undef srand
44+
#endif
45+
#define srand(x) tesseract_r_wrapper::safe_srand(x)
46+
47+
// Redirect cout/cerr
1148
#define cerr \
1249
if (0) std::cerr
1350
#define cout \
1451
if (0) std::cout
1552

16-
inline void R_friendly_abort() {
17-
Rprintf("Internal error detected (abort intercepted)\n");
18-
}
19-
inline void R_friendly_exit(int status) {
20-
Rprintf("Exit requested with status %d (intercepted)\n", status);
21-
}
22-
23-
#define abort R_friendly_abort
24-
#define exit(x) R_friendly_exit(x)
25-
26-
inline int R_friendly_rand() { return 0; }
27-
inline void R_friendly_srand(unsigned int seed) {}
28-
#define rand R_friendly_rand
29-
#define srand(x) R_friendly_srand(x)
53+
#endif // _WIN32
3054

31-
#endif
32-
#endif
33-
34-
// Try multiple include paths for better cross-platform compatibility
35-
#if __APPLE__
3655
// On macOS, try multiple include paths
56+
#if __APPLE__
3757
#if __has_include(<leptonica/allheaders.h>)
3858
#include <leptonica/allheaders.h>
3959
#elif __has_include(<allheaders.h>)

0 commit comments

Comments
 (0)