From a49dbc89cd6883599174513d09ff23cf84ac4a07 Mon Sep 17 00:00:00 2001 From: Drew Miller Date: Thu, 16 Sep 2021 22:40:18 +0100 Subject: [PATCH 1/4] allow inclusion in C programs --- include/LightGBM/c_api.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/LightGBM/c_api.h b/include/LightGBM/c_api.h index aaf35fe1c12e..5f12daa72164 100644 --- a/include/LightGBM/c_api.h +++ b/include/LightGBM/c_api.h @@ -15,9 +15,16 @@ #include +#ifdef __cplusplus #include #include #include +#else +#include +#include +#include +#include +#endif typedef void* DatasetHandle; /*!< \brief Handle of dataset. */ @@ -1321,7 +1328,9 @@ LIGHTGBM_C_EXPORT int LGBM_NetworkInitWithFunctions(int num_machines, void* reduce_scatter_ext_fun, void* allgather_ext_fun); -#if defined(_MSC_VER) +#ifndef __cplusplus +#define THREAD_LOCAL +#elif defined(_MSC_VER) #define THREAD_LOCAL __declspec(thread) /*!< \brief Thread local specifier. */ #else #define THREAD_LOCAL thread_local /*!< \brief Thread local specifier. */ From d8abfbd019ab0dce0e6bb7f79929d4853acc0b5e Mon Sep 17 00:00:00 2001 From: Drew Miller Date: Fri, 17 Sep 2021 08:50:24 +0100 Subject: [PATCH 2/4] adding documentation to macro --- include/LightGBM/c_api.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/LightGBM/c_api.h b/include/LightGBM/c_api.h index 5f12daa72164..770cedac3f72 100644 --- a/include/LightGBM/c_api.h +++ b/include/LightGBM/c_api.h @@ -1329,7 +1329,7 @@ LIGHTGBM_C_EXPORT int LGBM_NetworkInitWithFunctions(int num_machines, void* allgather_ext_fun); #ifndef __cplusplus -#define THREAD_LOCAL +#define THREAD_LOCAL /*!< \brief Thread local specifier no-op in C builds. */ #elif defined(_MSC_VER) #define THREAD_LOCAL __declspec(thread) /*!< \brief Thread local specifier. */ #else From 53c68816a16f1609ab37dd3364e0b0a9e1f3295c Mon Sep 17 00:00:00 2001 From: Drew Miller Date: Sat, 25 Sep 2021 14:24:17 +0100 Subject: [PATCH 3/4] Support for ANSI C, _Thread_local where available. --- include/LightGBM/c_api.h | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/include/LightGBM/c_api.h b/include/LightGBM/c_api.h index 770cedac3f72..0f56ff9a151d 100644 --- a/include/LightGBM/c_api.h +++ b/include/LightGBM/c_api.h @@ -86,7 +86,7 @@ LIGHTGBM_C_EXPORT int LGBM_SampleIndices(int32_t num_total_row, void* out, int32_t* out_len); -// --- start Dataset interface +/* --- start Dataset interface */ /*! * \brief Load dataset from file (like LightGBM CLI version does). @@ -431,7 +431,7 @@ LIGHTGBM_C_EXPORT int LGBM_DatasetGetNumFeature(DatasetHandle handle, LIGHTGBM_C_EXPORT int LGBM_DatasetAddFeaturesFrom(DatasetHandle target, DatasetHandle source); -// --- start Booster interfaces +/* --- start Booster interfaces */ /*! * \brief Get boolean representing whether booster is fitting linear trees. @@ -1328,8 +1328,16 @@ LIGHTGBM_C_EXPORT int LGBM_NetworkInitWithFunctions(int num_machines, void* reduce_scatter_ext_fun, void* allgather_ext_fun); -#ifndef __cplusplus -#define THREAD_LOCAL /*!< \brief Thread local specifier no-op in C builds. */ +#define INLINE_FUNCTION inline /*!< \brief Inline specifier. */ + +#if (!defined(__cplusplus) && (!defined(__STDC__) || (__STDC_VERSION__ < 201112L))) +#define THREAD_LOCAL /*!< \brief Thread local specifier no-op in C using standards before C11. */ +#if !defined(__STDC__) || (__STDC_VERSION__ < 199901L) +#undef INLINE_FUNCTION +#define INLINE_FUNCTION /*!< \brief inline specifier no-op in C using standards before C99. */ +#endif +#elif !defined(__cplusplus) +#define THREAD_LOCAL _Thread_local /*!< \brief Thread local specifier. */ #elif defined(_MSC_VER) #define THREAD_LOCAL __declspec(thread) /*!< \brief Thread local specifier. */ #else @@ -1349,9 +1357,9 @@ static char* LastErrorMsg() { static THREAD_LOCAL char err_msg[512] = "Everythin * \brief Set string message of the last error. * \param msg Error message */ -inline void LGBM_SetLastError(const char* msg) { +INLINE_FUNCTION void LGBM_SetLastError(const char* msg) { const int err_buf_len = 512; snprintf(LastErrorMsg(), err_buf_len, "%s", msg); } -#endif // LIGHTGBM_C_API_H_ +#endif /* LIGHTGBM_C_API_H_ */ From 0228d4b3e014b482c20791408b0af8c3a5d64164 Mon Sep 17 00:00:00 2001 From: Drew Miller Date: Sat, 25 Sep 2021 19:54:57 +0100 Subject: [PATCH 4/4] fix macro for docs --- include/LightGBM/c_api.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/LightGBM/c_api.h b/include/LightGBM/c_api.h index 0f56ff9a151d..b5542891b063 100644 --- a/include/LightGBM/c_api.h +++ b/include/LightGBM/c_api.h @@ -1328,14 +1328,14 @@ LIGHTGBM_C_EXPORT int LGBM_NetworkInitWithFunctions(int num_machines, void* reduce_scatter_ext_fun, void* allgather_ext_fun); +#if !defined(__cplusplus) && (!defined(__STDC__) || (__STDC_VERSION__ < 199901L)) +#define INLINE_FUNCTION /*!< \brief inline specifier no-op in C using standards before C99. */ +#else #define INLINE_FUNCTION inline /*!< \brief Inline specifier. */ +#endif -#if (!defined(__cplusplus) && (!defined(__STDC__) || (__STDC_VERSION__ < 201112L))) +#if !defined(__cplusplus) && (!defined(__STDC__) || (__STDC_VERSION__ < 201112L)) #define THREAD_LOCAL /*!< \brief Thread local specifier no-op in C using standards before C11. */ -#if !defined(__STDC__) || (__STDC_VERSION__ < 199901L) -#undef INLINE_FUNCTION -#define INLINE_FUNCTION /*!< \brief inline specifier no-op in C using standards before C99. */ -#endif #elif !defined(__cplusplus) #define THREAD_LOCAL _Thread_local /*!< \brief Thread local specifier. */ #elif defined(_MSC_VER)