Pregunta

I want to know what is the difference between preprocessor directives and libraries in C?

So far, I know preprocessor directives are used to include external files. (I mean these external files can be Libraries?)

And libraries are ready made compiled programs already written for us. (Thus we need preprocessor directives to include them in C?)

¿Fue útil?

Solución

Preprocessor directives appear in source code. There are many different directives. One of them is #include, which is used to include a header file. Header files contain a collection of declarations, often for functions and types (and sometimes variables) found in a library. However, a header is not a library.

Libraries are collections of object files that have already been compiled. The C standard does not recognize that libraries exist (but it does recognize the preprocessor and defines the required behaviour). Libraries are listed on the linker (compiler) command line, often using the -lname notation to locate a library name, with the -L option used to specify places to search for libraries.

Note that the majority of the functions defined in the standard C library are found by the linker without it needing to be told where to look for them. The exception are the maths function which, for historical reasons (primarily related to machines that did not always have floating point arithmetic built-in — sometimes they had FP coprocessors, such as the Intel 80386 + 80387, or sometimes they needed software emulation of the missing hardware). On many systems, you need to specify -lm to link the maths library; on others, you don't (the code is in the main system C library).

Generally speaking, headers are not found in the same directories as libraries (it would be a messy, unprofessional project that installed headers into the same directory as its libraries).

Especially in C++, there are libraries that do not have pre-compiled object files; the services are defined exclusively through the header. These are much less common in C. It is most sensible to regard the files as headers, not libraries. A header defines a set of services that can be used by the compiler. A library provides the support for such services. (If you think about it, or take a look in your system, you will find that <stdio.h> does not include the source for fprintf() — to take one example of many — but it does declare fprintf() in such a manner that your program can use it and so that the actual function from the standard C library will be used at runtime.)

Dynamic linking (the loading of shared objects, aka shared libraries or dynamic link libraries (DLLs)) where the library file(s) are not loaded until runtime, after the program is started up (after main() has been called) are another whole platform-specific bag'o'worms.

Otros consejos

Pre processors commands do lots of things, one of those is include files, such as header files. Libraries mostly provide compiled code to do things for you, this is very very different. However, most libraries will require your code to include a header file from the library so that you code will know about the types and function available in the library.

there are a lot of preprocessor directives , I'll list some of theme here :

  • #define : is used to define constants or marcro (with or without arguments )
  • #include: which include files (using the " ") or libraries (using < >)
  • #if and #ifdef : used to compile only parts of code when certain conditions are filled (they are allways followed by #endif)
  • ...

you can find out a lot more about preprocessor directive here

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top