Will C compiler attach the whole object code of the header file?

When you compile a C source code does the compiler convert the whole header file to object code or just the functions of the header file you use?

Like for example, in the header file #include stdio.h, there are printf(); and scanf(); but even to use one of them, you have to include the header file “stdio.h”, so when you compile a C source code which uses only printf(); but doesn’t use scanf();, will the compiler also include the object code of scanf(); and other functions of “stdio.h” in the file?

2

The compiler will generate code for everything.

The linker may (and usually does) not include code of unused functions in the final executable.

Implementation, of course, varies from linker to linker.

1

Indirectly, yes.

To know what happens to the code in the header file one must understand how the compiler handles them.

The #include directive

The #include directive is a preprocessor directive. Even before the compiler starts to analyze and translate the code it will process all #includes by inserting the whole code in the given header file to the position of the #include statement. (You can see what the preprocessor spits out when you use e.g. gcc -E ... on your source files.)

That is why include guards are important.

So the answer to your question is:
Yes, the code in header files is translated to an object file – to be precise to the object file of the first translated source file which includes it in a translation unit.

The definitions in the header files have to be known in every translation unit which uses the definitions. If you translate your programm file by file and link it afterwards, you will have the code in the header file compiled multiple times. That is why you should put the implementation to a separate source file. If you do that you only will declare each function in every translation unit but not translate the implementation. All files will then be linked to the one object file with the implementation.

The compiler doesn’t do anything with other object files. It merely compiles code to object files one translation unit at a time. It’s the linker that takes your new object file, looks for symbols like “printf”, and then “attaches” the implementation of printf from stdio.h’s object file. In theory the linker could throw in the whole stdio.h object file, but in practice it should leave out all scanf code in the final executable since you don’t need it.

So the literal answer to your question is: No, the compiler doesn’t do anything like that. If you were using “compiler” to informally denote your entire toolchain, then the answer is: Yes at first, but only until it finishes checking which parts of the object file you do and don’t need.

Note that this only describes static linking, where you combine all object files into a single executable before executing anything. Dynamic linking is a very different story.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật