Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow inclusion in C programs #4608

Merged
merged 8 commits into from
Oct 5, 2021
11 changes: 10 additions & 1 deletion 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 @@ -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 /*!< \brief Thread local specifier no-op in C builds. */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems C11 also has thread_local, refer to https://en.wikipedia.org/wiki/C11_(C_standard_revision)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The follow-up changes take advantage of _Thread_local from C11. They also make the C API header compatible with ANSI C. If the changes to the comments disrupt doc generation, they could be rolled back in favor of abandoning support for C versions prior to C99. The attached test.c.gz can be gunzipped in c_api_test/ and used to test both code generation and compilation with the below:

Code generation:
gcc test.c -ansi -I../../include/ -E - should exclude inline and _Thread_local
gcc test.c -std=c99 -I../../include/ -E - should include inline and exclude _Thread_local
gcc test.c -std=c11 -I../../include/ -E - should include inline and _Thread_local
g++ test.c -I../../include/ -E - should include inline and thread_local

Remove the -E switch on the above to confirm compilation for each setting.

#elif defined(_MSC_VER)
#define THREAD_LOCAL __declspec(thread) /*!< \brief Thread local specifier. */
#else
#define THREAD_LOCAL thread_local /*!< \brief Thread local specifier. */
Expand Down