Libraries and Preprocessing in C / C ++

2

I've always been surprised at the syntax of #include and #define , since they differ greatly from the syntax of the rest of the language. I have found that this is due to the fact that these commands are intended for the preprocessor and not the compilation process itself.

I also discovered that compiler handbooks like the GCC Handbook discourage the use of the preprocessor, suggesting that the programmer gives preference to the mechanisms of language itself. Example: In the section about the #pragma command, which modifies the parameters for compilation, if possible replace it with function attributes.

However, although the compiler's arguments seem to me to be consistent, I've never found a way to use libraries in C / C ++ without the #include preprocessor command. Also, to avoid including duplicate headers, you must use include guards or the command #pragma once , both dependent on the preprocessor.

  • In Pascal and Object Pascal we have uses , in C # using and in Java import , among other examples. I know that C / C ++ languages are constantly being developed and updated to this day, so why has this feature never been added?

    I understand the use of the preprocessor for conditional compilation, especially in the development of compatible functions across multiple OSs. But using libraries would not be a very basic function to be in charge of the preprocessor? What is the justification for this?

    Since I studied the subject, I feel that whenever I use libraries in C / C ++ I am doing a "gambiarra".

        
  • asked by anonymous 19.02.2018 / 03:05

    1 answer

    3

    Without #include it really gets really complicated, even if it's possible. I do not know if they talk to not use it, I guess they talk about other things.

      
  • Because C was created almost 50 years ago and this was not so simple, they did not think about it well. C ++ had to maintain compatibility.

      

    In Pascal and Object Pascal we have the uses

    At first there was none of this.

    C changes little, the idea is to keep it stable. C ++ will have something like this soon, but I do not know if it will completely eliminate the use of PP.

      

    I understand the use of the preprocessor for conditional compilation

    In C ++ you could do a lot of work, and you may not need it in 100% of cases.

      

    Using libraries would not be a very basic function to be in charge of the preprocessor? What is the justification for this?

    It is not trivial to insert in the language, it is trivial in C because it is not part of the language, and consequently does not give many guarantees, if you do wrong, babau.

        
    19.02.2018 / 03:56