Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of a cross-platform header file in C language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "a cross-platform header file analysis in c language". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "a cross-platform header file analysis in c language" together!

I. Foreword

When we write code in general, especially when we build wheels (providing library files for others), we encounter various requirements scenarios:

Some people need to use Linux, some people need to use Windows; some people use C language development, some people use C++ to develop; some people use dynamic libraries, some people use static libraries;

Especially in Windows systems, functions exported from library files need to use_declspec(dllexport) to declare functions, while users need to use_declspec(dllimport) to declare functions when importing them.

This header file, along with a few macros passed during compilation, perfectly handles the various requirements just described.

II. Headers

First directly on the code, you can try to analyze it first, and then we will analyze different use scenarios one by one.

The main purpose of this header file is to define a macro: MY_API, and then add this macro to the declaration of each function or class to be exported in the library file. For example:

void MY_API do_work();

Here is the header file:

_Pragma("once")

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)

#define MY_WIN32

#elif defined(linux) || defined(__linux) || defined(__linux__)

#define MY_LINUX

#endif

#if defined(MY_WIN32)

#ifdef MY_API_STATIC

#ifdef __cplusplus

#define MY_API extern "C"

#else

#define MY_API

#endif

#else

#ifdef MY_API_EXPORTS

#ifdef __cplusplus

#define MY_API extern "C" __declspec(dllexport)

#else

#define MY_API __declspec(dllexport)

#endif

#else

#ifdef __cplusplus

#define MY_API extern "C" __declspec(dllimport)

#else

#define MY_API __declspec(dllimport)

#endif

#endif

#endif

#elif defined(MY_LINUX)

#ifdef __cplusplus

#define MY_API extern "C"

#else

#define MY_API

#endif

#endif III. Predefined macros

Suppose you need to write a library file and make it available to others. After defining the header file above, other files should include this header file.

1. platform macro definition

Macro definitions are predefined for different platforms, for example:

Windows platform: WIN32, _WIN32, WIN32;

Linux platform: linux, __linux, linux;

On a given platform, not all of these macros are defined; it is likely that only one of them is defined.

For uniformity, we unify these possible macros at the beginning of the header file, define our own platform macro definition: MY_WIN32 or MY_LINUX, and later use this platform macro defined by ourselves when we need to distinguish between different platforms.

Of course, it can be extended to other platforms, such as MY_MAC, MY_ARM, etc.

2. compiler macro definition

If the library code is written in C++ and the user is using C, then the library function needs to be extern "C" declaration, so that the compiler does not rewrite the name of the function.

The compiler g++ predefines the macro__cplusplus, so in the header file, it takes advantage of this macro by adding the extern "C" declaration to MY_API.

IV. Windows Platform Scene Analysis 1. compile build library file

(1)Generating static libraries

In the static library, there is no need for the declaration of__declspec(dllexport/dllimport), so only need to distinguish the compiler (gcc or g++), in the compilation options define the macro MY_API_STATIC, you can get the final MY_API is:

gcc compiler: #define MY_API

g++ Compiler: #define MY_API extern "C"

(2)Generate dynamic libraries

In the compile options, define the macro MY_API_EXPORTS, so that the final MY_API will become:

gcc compiler: #define MY_API __declspec(dllexport)

g++ Compiler: #define MY_API extern "C" __declspec(dllexport)

2. using the library

In applications that use the library, you also need to include this header file in your code, and then add the macros defined in the compile options to generate the corresponding MY_API macro definition.

(1)Using static libraries

You need to define MY_API_STATIC in the compilation options to get the final MY_API as:

gcc compiler: #define MY_API

g++ Compiler: #define MY_API extern "C"

(2)Using Dynamic Libraries

No macro definition is required in the compilation options to get the final MY_API as:

gcc compiler: #define MY_API extern "C" __declspec(dllimport)

g++ Compiler: #define MY_API __declspec(dllimport)

This is equivalent to declaring an import library function.

V. Linux Platform Scenario Analysis

Linux platform is much simpler, only need to pay attention to compiler problems, and there is no export and import points.

Thank you for reading, the above is "c language in a cross-platform header file analysis" of the content, after learning this article, I believe we have a cross-platform header file analysis of c language this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report