Qt调用C函数

一.Qt或者C++中调用C函数方法

1.如果在Qt或者C++文件中直接调用.c文件中的C函数是会报错的,如:

error: undefined reference to `vendor_app_handle()'

2.这种情况有两种解决方法:

(1)在头文件中直接使用

#ifdef __cplusplus

extern "C" {

#endif

#include "xxx.h"

int vendor_app_handle(void);

#ifdef __cplusplus

}

#endif

(2) 在需要调用c函数的cpp文件中包含对应的.h文件,然后就可以直接调用c函数了

二.完整代码示例

1.function.h

#ifndef FUNCTION_H

#define FUNCTION_H

#ifdef __cplusplus

extern "C" {

#endif

#include <stdio.h>

int add(int a, int b);

char *rstr(char *str);

#ifdef __cplusplus

}

#endif

#endif

2.function.c

#include "function.h"

int add(int a, int b)

{

return a+b;

}

char *rstr(char *str)

{

return str;

}

  1. mainwindow.cpp 调用结果
相关推荐
tkevinjd36 分钟前
C++中线程库的基本操作
开发语言·c++
CodeWithMe1 小时前
【C/C++】不同防止头文件重复包含的措施
c语言·开发语言·c++
子豪-中国机器人1 小时前
C++ 信息学奥赛总复习题答案解析
开发语言·c++·算法
oioihoii1 小时前
C++11列表初始化:从入门到精通
java·开发语言·c++
zdy12635746881 小时前
python第48天打卡
开发语言·python
whoarethenext1 小时前
使用 C++/OpenCV 创建动态流星雨特效 (实时动画)
开发语言·c++·opencv
whoarethenext1 小时前
使用 C/C++的OpenCV 实现模板匹配:从基础到优化
c语言·c++·opencv
Mr...Gan2 小时前
TypeScript
开发语言·javascript·typescript
合方圆~小文2 小时前
架空线路图像视频监测装置
c语言·c++·人工智能·嵌入式硬件·硬件工程·模拟退火算法
GN已被占用3 小时前
Qt 窗口
qt