cmath.hc-header

How does `math.h` refer to functions like `sin` or `cos`?


I see a plenty of similar questions, which are duplicates (like 12074147), but most of them are concerned with mathematical functions implementation.

My question is more about C standard headers, like this one:

#include <math.h>

What I'd be happy to understand, how exactly does the math.h refer to any sin or cos implementation when it even doesn't mention them.

At least, I cannot find any such or similar words in the code (i.e. F12 key) and in files that get included in math.h? What am I missing?


Solution

  • In most implementations, the standard headers are implemented as files in a set of system directories, that declare the library macros, types and functions specified in the C Standard. The usually include other files which provide the system specific definitions appropriate for the compiler, library and target system.

    If you did not find a prototype for cos and sin, look again for #include or #include_next directives and you may eventually find the definitions. To help locate the system include files, use the -E command line argument.

    On my system here is the output for <math.h>:

    chqrlie> clang -E - << EOF
    ... #include <math.h>
    ... EOF
    # 1 "<stdin>"
    # 1 "<built-in>" 1
    # 1 "<built-in>" 3
    # 400 "<built-in>" 3
    # 1 "<command line>" 1
    # 1 "<built-in>" 2
    # 1 "<stdin>" 2
    # 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h" 1 3 4
    # 30 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h" 3 4
    # 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 1 3 4
    # 666 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 3 4
    # 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/_symbol_aliasing.h" 1 3 4
    # 667 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 2 3 4
    # 732 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 3 4
    # 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/_posix_availability.h" 1 3 4
    # 733 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h" 2 3 4
    # 31 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h" 2 3 4
    # 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/Availability.h" 1 3 4
    # 165 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/Availability.h" 3 4
    # 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/AvailabilityVersions.h" 1 3 4
    # 166 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/Availability.h" 2 3 4
    # 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/AvailabilityInternal.h" 1 3 4
    # 167 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/Availability.h" 2 3 4
    # 32 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h" 2 3 4
    # 44 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h" 3 4
        typedef float float_t;
        typedef double double_t;
    /*
     * ------ 48 lines elided ------
     */
    # 308 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h" 3 4
    extern float acosf(float);
    extern double acos(double);
    extern long double acosl(long double);
    
    extern float asinf(float);
    extern double asin(double);
    extern long double asinl(long double);
    
    extern float atanf(float);
    extern double atan(double);
    extern long double atanl(long double);
    
    extern float atan2f(float, float);
    extern double atan2(double, double);
    extern long double atan2l(long double, long double);
    
    extern float cosf(float);
    extern double cos(double);
    extern long double cosl(long double);
    
    extern float sinf(float);
    extern double sin(double);
    extern long double sinl(long double);
    
    extern float tanf(float);
    extern double tan(double);
    extern long double tanl(long double);
    /*
     * ------ 283 lines elided ------
     */
    # 2 "<stdin>" 2
    

    Note also that in addition to the above definitions, the compiler may implement its own semantics for standard functions and generate inlined or transformed code for many of them.

    For example, it is not uncommon for compilers to optimize printf("Hello word!\n") as a call to puts("Hello world!") and inline calls to sin(e) and cos(e) for constant argument expressions.