C/C++ libraries and preprocessing

Asked

Viewed 286 times

-1

I’ve always wondered the syntax of #include and of #define, as they differ greatly from the syntax of the rest of the language. I found that this is because these commands are intended for the preprocessor and not the build process "itself".

I also found that compiler manuals like Manual of the GCC discourage the use of the preprocessor, suggesting that the programmer give preference to the mechanisms of the language itself. Example: in section on command #pragma, modifying the parameters for compilation, it is recommended whenever possible to replace it with function attributes.

However, although the arguments for preference for the compiler seem coherent to me, I have never found a way to use libraries in C/C++ without the pre-processing command #include. In addition, in order to avoid the inclusion of duplicate headers, it is necessary to use include Guards or the command #pragma once, both dependent on the preprocessor.

Because C/C++ languages do not support the inclusion of libraries using compiler commands only?

In Pascal and Object Pascal we have the uses, in C# usingand in Java the import, among other examples. I know that the C/C++ languages are in constant development and updating until today, so why has this type of feature never been added?

I understand the use of the preprocessor for conditional compilation, especially in the development of compatible functions among several Sos. But wouldn’t using libraries 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".

1 answer

4

Without the #include really gets very complicated, even if possible. I do not know if they say not to use even this, I imagine they speak of other things.

Because C/C++ languages do not support the inclusion of libraries using compiler commands only?

Because C was created almost 50 years ago and it wasn’t that simple, they didn’t think it through. C++ needed to maintain compatibility.

In Pascal and Object Pascal we have the uses

At first I had none of that.

C changes little, the idea is to keep it stable. C++ will have something like this soon, but I don’t know if it will totally eliminate the use of PP.

I understand the use of the preprocessor for conditional compilation

In C++ you could already in large part, and then you can in 100% cases you don’t need it.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.