From my blog original link: http://finaldie.com/wordpress? P = 21
Well, when you see the title, you must think that I made a big mistake. I didn't follow the include. h standard paradigm to write programs, but I still want to talk about the advantages here.
Now we have a requirement to write a set of event libraries to adapt to different platforms (Linux, FreeBSD... ), OK. The obvious problem here is that we need a set of abstract interfaces so that different platforms can follow them and compile them on different platforms. c file, but how to find different implementation files during the compilation period? Because different implementation files need to be specified during the compilation period, should they be written in makefile as parameters? Obviously, it is not intelligent enough. We still hope to build auto. So we come up with a seemingly perfect solution to create headers with different names for different platforms, such as using ev_epoll.h in Linux,
FreeBSD uses ev_kqueue.h, but these two files use identical interface definitions. Generally, this is no problem. We only need to include different headers in the framework based on different platforms ~ . However, there is a potential problem. Once we modify the abstract interface definition, we must not only modify the response implementation file (which is unavoidable), but also cascade the headers for maintaining the response, this looks terrible, and then we come up with another upgrade solution. In these headers of different platforms, We uniformly include a global header (including abstract descriptions ), we feel nice to use this solution ~
However, once modified, we still need to modify the global header, although the cost is very small ~
Okay, I 've talked a lot about it. I started to get into the question. Can we remove this header? Because this header has no value in nature and needs to be seen externally, putting multiple of them will make the user's head big. Please let us know the main character of today: "include. C ", let's first understand include. In fact, it doesn't care about include, as long as the syntax is correct ~ Using this feature, we can directly include. C (the specific implementation file of the corresponding platform, such as ev_epoll.c/ev_kqueue.c). You can understand that their code is copied paste to the include location,
Then, we do not need any header file dependencies, nor need to modify an interface for modification. There is no necessary header at all.
,
Finally, let's briefly describe the process of compiling such a file, because we already include. c, so we don't need to write this again when writing makefile. c file. Otherwise, various declarations may occur. If the variable does not exist, the include. c files will help us copy them to paste, so we don't have to worry about it. Isn't it easy?
Have fun ~