C language scrap 4: Use _Pragma to gently discard API

I. Introduction

Imagine this working scenario: you are writing a function library for a project , and others call the functions provided in the library, and then you find that function A in the library is
redundant
.

With a perfect plot, you just want to discard this function A. At this time , you definitely can't delete it directly , because you don't know how many other people called this function.

How to deal with this situation better?

Let's talk about this issue in this short article.

Second, the operation process

1. The first version of the library

There are only 3 test files : api.h, api.c and main.c

  1. api.h and api.c: library files, compiled libapi.so;
  2. main.c: Generate an executable program, using the library libapi.so generated above;

api.h contents of the documents: a statement of the two functions.

api.c file content: the definition of the two functions.

Compile the library file libapi.so . Compilation instructions:

gcc -fPIC -shared api.c -o libapi.so

Main.c file content:

Compile the executable file:

gcc main.c -o main -L./ -Wl,-rpath=./ -lapi

The simplicity of the above code is equivalent to helloworld.

2. The second version of the library

Now, you think the init function is redundant . If you want to remove it, you can modify it like this.

In the api.c file, delete the init() function.

The content of the api.h file is changed to the following:

The key code is this line:

#define init()               (1) API_DEPRECATED

Now that the api.c file has deleted this function , but this function is called in the main.c file, the init symbol is provided in the form of a macro definition .

In other words:

In the first version, init in the main.c file is a function , which is processed by the compiler, and the address of this function is found from the libapi.so library during the link phase ;

In the second version, init is defined as a macro , which is replaced by the latter in the preprocessing stage (1) API_DEPRECATED.

(1) is the expression in the macro substitution. Because this function may be used in the judgment of if conditions, it needs to return a value.
API_DEPRECATED is another macro definition, which is expanded to allow the compiler to print out a message when compiling the executable program.

When compiling the executable file, the compiler outputs the following paragraph:

gcc main.c -o main -L./ -Wl,-rpath=./ -lapi

This has achieved the original purpose! That is to remind users: this function has been deprecated, it is best not to use it!

Three _Prama other usage

_Pragma is similar to the Microsoft specific __pragma keyword, except that it is part of the standard. It was introduced for C in C99 . For c++, it was introduced in c++11.
It allows instructions to be placed in macro definitions.

1. Dealing with repeated inclusion of header files

In the header file, in order to prevent repeated inclusion , there are generally 3 ways to deal with it:

(1) The first approach:

#ifdef   MY_API
#define  MY_API

// 头文件内容

#endif

(2) The second processing method

#pragma once

// 头文件内容

The above two methods can prevent the same header file from being included repeatedly, but there are still some differences .

The first way: the preprocessor still needs to search for the file, then open the file, and after reading the content of the file, check whether MY_API has been defined.

The second way: It can speed up the compilation speed, because this is a high-end mechanism; the compiler will automatically compare the file name , without the need to judge #ifndef and #endif in the header file, so that the intermediate search is omitted , Open and read operations.

(3) The third approach

_Pragma("once")

The difference between this method and the second method is:

#pragma: is a preprocessing instruction, used to convey some information outside the language standard to the compiler, and cannot be used in macros;
_Pragma: is an operator that belongs to the language standard, so it can be nested in macros, just As in the example above;

#pragma is an extension of the compiler, that is to say, it is determined by the compiler . Maybe the compiler A supports it, but the compiler B may not support it, although this possibility is relatively small.

The _Pragma operator is a standard at the language level . Since it is a standard, the compiler must follow the standard , so this method is also recommended.

Remember Hou Jie teacher said in C ++ video lesson: We write code, not only to ensure function properly on, and put the code written in great atmosphere ! I feel that using _Pragma may be more atmospheric than #ifndef.

2. Output compilation information

#pragma message("the #pragma way")
_Pragma ("message( \"the _Pragma way\")") 

The content and output information of the above two lines are the same. Note that the nested double quotation marks need to be escaped with a backslash.

That's All! Have a great weekend!


Good articles should be forwarded ; the more you share, the luckier you are!


Recommended reading

[C language]
1. C language pointer-from the underlying principle to fancy skills, with pictures and codes to help you explain thoroughly
2. The original gdb underlying debugging principle is so simple
3. Step by step analysis-how to use C to achieve object-oriented programming
4. A weapon to improve code compulsion: macro definition-from entry to abandonment
5. Use setjmp and longjmp in C language to implement exception capture and coroutine

[Application design]
1. They all say that the software architecture should be layered and divided into modules, and how to do it (1)
2. They all say that the software architecture needs to be layered and divided into modules, and how to do it (2)
3. IoT gateway Development: MQTT message bus-
based design process (Part 1) 4. IoT gateway development: MQTT message bus-based design process (Part 2)
5. My favorite method of communication between processes-message bus

[Operating System]
1. Why do spacecraft and missiles prefer to use microcontrollers instead of embedded systems?

[Internet of Things]
1. Those things about encryption and certificates
2. Deepen the LUA scripting language, let you fully understand the principle of debugging

[Nonsense] 1. Based
on my failed career experience: a few tips for technicians who are new to the workplace

Guess you like

Origin blog.csdn.net/u012296253/article/details/115366935