How could my code compile correctly without necessary headers?

前端 未结 3 1063
情书的邮戳
情书的邮戳 2021-01-18 03:19

I use the functions fork(),exec()...

But how can this program be compiled without including some extra headers (like sys/types.h, sys/wait.h).

I use ubuntu 1

3条回答
  •  一向
    一向 (楼主)
    2021-01-18 03:59

    caf's answer is only partially correct.

    Under the rules specified by the C89/C90 standard (commonly called "ANSI C"), a call to a function with no visible declaration is legal. The compiler assumes that the function returns an int result and takes arguments of the (promoted) type(s) given in the call. If the call is not consistent with the function definition, the behavior is undefined. For such a call, it's entirely the programmer's responsibility to get the call right; the compiler likely will not tell you if you make a mistake.

    The 1999 ISO C standard dropped this "implicit int" rule, and made a call to an undeclared function a constraint violation, requiring a diagnostic. (The diagnostic may be a non-fatal warning, or it can cause compilation to fail.) Once the diagnostic has been printed, if the compiler creates an executable, its behavior is undefined.

    Unfortunately, many compilers still don't enforce modern C rules by default. Most of them can be made to do so with appropriate compile-time options.

    For gcc in particular, you can compile with

    gcc -std=c99 -pedantic
    

    or

    gcc -std=c11 -pedantic
    

    (The latter didn't exist yet when caf's answer was written.) Or, if you also want to use gcc-specific extensions, you can use -std=gnu99 or -std=gnu11. The default is currently -std=gnu89. This will likely be changed to -std=gnu11 in a future release of gcc.

    gcc-compatible compilers like clang will generally follow the same rules. For other compilers, you'll have to consult their documentation to find out how to persuade them to enforce modern C rules. (Microsoft's C compiler has very incomplete support for standards later than C90.)

提交回复
热议问题