Skip to content

Commit

Permalink
allow inclusion in C programs (#4608)
Browse files Browse the repository at this point in the history
* allow inclusion in C programs

* adding documentation to macro

* Support for ANSI C, _Thread_local where available.

* fix macro for docs
  • Loading branch information
drewmiller authored Oct 5, 2021
1 parent e95d5ab commit f3037c1
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions include/LightGBM/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@

#include <LightGBM/export.h>

#ifdef __cplusplus
#include <cstdint>
#include <cstdio>
#include <cstring>
#else
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#endif


typedef void* DatasetHandle; /*!< \brief Handle of dataset. */
Expand Down Expand Up @@ -79,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).
Expand Down Expand Up @@ -424,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.
Expand Down Expand Up @@ -1321,7 +1328,17 @@ LIGHTGBM_C_EXPORT int LGBM_NetworkInitWithFunctions(int num_machines,
void* reduce_scatter_ext_fun,
void* allgather_ext_fun);

#if defined(_MSC_VER)
#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))
#define THREAD_LOCAL /*!< \brief Thread local specifier no-op in C using standards before C11. */
#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
#define THREAD_LOCAL thread_local /*!< \brief Thread local specifier. */
Expand All @@ -1340,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_ */

0 comments on commit f3037c1

Please sign in to comment.