# weak function 這裡提供了一個使用弱函數(Weak Function)的範例,該範例包含SDK和Driver兩個部分。 在SDK部分,定義了一個函數 usbd_cdc_mount_cb,當USB CDC裝置掛載時,它調用 log_usbd_cdc_mounted 函數。這是一個示意性的SDK功能。 在Driver部分,使用 __attribute__((weak)) 屬性定義了一個弱函數 usbd_cdc_mount_cb。這個弱函數的目的是提供一個默認的實現,但如果SDK部分提供了自己的實現,則使用SDK中的實現。這樣做允許SDK在需要時替換Driver中的函數。 下面是代碼片段: c ```c // ------- SDK ------- void log_usbd_cdc_mounted(void) { // 實際的 CDC 掛載處理 // ... } void usbd_cdc_mount_cb(void) { log_usbd_cdc_mounted(); } // ------- Driver ------- __attribute__((weak)) void usbd_cdc_mount_cb(void) { // 默認的 CDC 掛載處理 // ... } ``` 請注意,使用弱函數的主要優勢在於可以在SDK或其他地方重新定義這個函數,而不需要修改Driver的原始代碼。這種機制使得代碼更具彈性,並且能夠輕松擴展或定制不同的功能。
×
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