1.變長數組
bool fun(int cnt)
{unsigned char data[cnt];return true;
}
?????????在 C 語言中,變長數組(Variable Length Arrays,VLA)是 C99 標準引入的特性,允許使用變量來定義數組的長度。因此,在 C 版本的代碼中,可以使用您提供的方式來動態申請數組,前提是編譯器支持 C99 標準。
2.c++的函數作為回調函數傳給c
class UdpTask : public QObject
{Q_OBJECTpublic:static UdpTask &instance(){static UdpTask self;return self;}void registration();signals:void sendImage(QImage image);
private:explicit UdpTask(QObject *parent = nullptr);//~UdpTask();UdpTask(const UdpTask &self);const UdpTask &operator=(const UdpTask &self);static void receivedDataHandle(int frame_id, void* buffer, int buffer_len);};
void UdpTask::receivedDataHandle(int frame_id, void* buffer, int buffer_len)
{/* 發送信號 */memcpy(instance().image->bits(), &buffer, buffer_len);emit instance().sendImage(*instance().image);
}
extern "C" {// C 語言寫的回調注冊函數
typedef void(*eth_recv_func_t)(int frame_id, void* buffer, int buffer_len);
ETHTRANSFERLIB_API int StartReceiveData(eth_recv_func_t func, int data_size);
}void UdpTask::registration(int len)
{StartReceiveData(receivedDataHandle, len);
}
這里用了?C++ 類的靜態函數作為回調函數,
靜態的單例函數?instance()
?用于獲取?UdpTask
?的唯一實例。
是為了在靜態函數中調用其他變量(如image,sendimage等),否則其他變量都需要申請為靜態的。
3.動態加載dll
當某些靜態庫的函數名稱與頭文件中的聲明不匹配時,如下圖。一般為編譯不規范沒有加def文件導致的,我們利用下面命令讀一下dll中的信息
dumpbin /headers your_dll_file.dll
?這是一段利用qt加載動態庫的方法
#include <QLibrary>
#include <QDebug>int main()
{// 創建 QLibrary 對象并加載動態庫QLibrary myLib("./lib/xxx.dll");if (myLib.load()) {// 獲取函數地址 可以用之前出現亂碼字符串來找到真正的函數void* symbol = myLib.resolve("?myFunctionName@@YAXXZ");//這樣就可以調用了symbol();// 卸載動態庫myLib.unload();} return 0;
}