# [最佳化你的程式碼] include guard (引用防護) > 作者: Aria > 2024/08/22 ## 目的: - 避免重複包含 (#include)library 在一次使用多個.c/.h檔時,你很容易忘記你包含(#include)了哪些library, 或是重複包含(#include)哪些library,為了避免重複包含 (#include)library,使用include guard來防護重複包含 (#include)library的事情發生。 ### 錯誤訊息: :::danger - error LNK1169: one or more multiply defined symbols found - 在MISRA C++: 2008中: - Rule 16-0-2 #define and #undef of macros should be done only at global namespace. ::: ### 使用方式: ``` #ifndef XXX_H #define XXX_H ... #endif ``` ### 其他類似解決方式: #pragma once ### Reference: [1] https://dotblogs.com.tw/thkaw/2013/12/13/134083 [2] http://ccckmit.wikidot.com/cp:includeguard
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up