简体   繁体   中英

How to override system c library headers?

I am working on an embedded project in C. The toolchain provided by our vendor includes a limited C Library that is missing some POSIX standard functions. For example they have a time.h but some POSIX functions are left out. I want to include some C libraries from source in my project that use #include <time.h> and expect those functions to exist. Is there a way to get these libraries to compile without having to modify their source code or is there no choice but to create another header perhaps named time_suppl.h with the additional functions and edit all the library source files to include that whenever they include time.h ?

The answer is that it depends on your compiler, but there is usually a way to do it without modifying the toolchain or the library you are trying to compile. Instead you simply change the include search path used by the compiler. Here are two useful examples of how to do it:

  • For GCC it is simple, using -I option will add to the search path for system headers with a higher priority so it's as simple as using that parameter to point to a location with files that have the exact same same as system headers such as time.h . The complete documentation at gcc.gnu.org says:

Add the directory dir to the list of directories to be searched for header files. See Search Path. Directories named by -I are searched before the standard system include directories.

  • For the proprietary ARMCC compiler there is a -J option that allows you to override the system header search path completely which is described in more depth in their documentation .

This is more in addition to @satur9nine's answer, but you may be able to do this without copying the whole of time.h . If you're using GCC, you can use the #include_next preprocessor directive to include the "next" instance of a file in the search path. Take the following example:

In /home/me/test_include/time.h:

#include_next <time.h> // Will pull in the standard time.h
int myNewTimeFunction();

If you then compile using gcc -I/home/me/test_include , then your new function will have a declaration when you #include <time.h> .

This is a GCC extension - YMMV with other compilers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM