Điều gì xảy ra trong preprocessing, compilation và linking, và vì sao translation unit không có main vẫn compile được nhưng hosted executable có thể link thất bại?
Xem đáp án và giải thích
Về mặt khái niệm, preprocessing giải quyết directive và tạo token stream cho một translation unit. Compilation parse, kiểm tra semantic, tối ưu và thường sinh object file chứa code, data, symbol cùng unresolved reference. Linking kết hợp object file và library, resolve reference, thực hiện relocation rồi tạo executable hoặc library được yêu cầu. Một source file chỉ là một translation unit nên vẫn có thể compile thành object mà không định nghĩa main; nó có thể là component thư viện được hoàn thiện ở nơi khác. Hosted C++ program cần một definition main global hợp lệ, và bước link executable thường báo thiếu entry point hoặc unresolved main nếu không có definition đó. Nhiều external definition lại vi phạm ODR và thường tạo duplicate-symbol diagnostic. Freestanding implementation cùng platform startup rule có thể dùng entry contract khác, vì vậy cần phân biệt rule của hosted C++ program với low-level entry symbol của operating system.
Về mặt khái niệm, preprocessing giải quyết directive và tạo token stream cho một translation unit. Compilation parse, kiểm tra semantic, tối ưu và thường sinh object file chứa code, data, symbol cùng unresolved reference. Linking kết hợp object file và library, resolve reference, thực hiện relocation rồi tạo executable hoặc library được yêu cầu. Một source file chỉ là một translation unit nên vẫn có thể compile thành object mà không định nghĩa main; nó có thể là component thư viện được hoàn thiện ở nơi khác. Hosted C++ program cần một definition main global hợp lệ, và bước link executable thường báo thiếu entry point hoặc unresolved main nếu không có definition đó. Nhiều external definition lại vi phạm ODR và thường tạo duplicate-symbol diagnostic. Freestanding implementation cùng platform startup rule có thể dùng entry contract khác, vì vậy cần phân biệt rule của hosted C++ program với low-level entry symbol của operating system.
Hãy liên hệ trực tiếp kết luận với kiểu, lifetime, ownership, overload resolution hoặc library contract xuất hiện trong câu hỏi. Nếu kết quả phụ thuộc implementation hoặc vi phạm precondition, cần nói rõ thay vì chỉ đoán output.