r/C_Programming • • Oct 16 '26

Question Why not specifying what to include ?

Hi, sorry if there is English mistake, it's not my first language.

I found online that the #include is replaced by preprocessor by the whole file being included, and I wondered why C isn't made so we can specify only parts of the files to include (like `import { x, y, z } from "file"` in typescript) so the compiled file is lighter ?

20 Upvotes

28 comments sorted by

View all comments

1

u/mcsuper5 Oct 16 '26

If you only need one or two simple functions, you can just include the prototype before it is called instead of including the header. I'm saying it can work for some functions. No guarantees it is safe even if it works. Standard headers may just indicate prototypes for some functions, but may define some macros usually used when calling the code

These days, loading several hundred lines that usually don't even need to be compiled isn't a big deal.

Since gcc allows you to tell it the code has already been preprocessed. (see "-fpreprocessed" in the gcc manual) (*.i files are treated as mostly preprocessed c.) You could use another method of preprocessing the code. I don't know how standard that is.

You can also call the preprocessor separately. man cpp.

You could use a custom preprocessor, but you'd probably need custom headers and want to automate the preprocessing. (If you do something like this, INCLUDE an explanation in your source of the tool chain and any/all needed files for the tool chain. Nothing like going back to project 15 years later and not remembering how it worked. Been there and done that!)