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 调用结果
相关推荐
2zcode15 分钟前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩199726 分钟前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
Aaswk1 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite2 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
liuhuizuikeai2 小时前
可视化门禁---Linux/Qt+SqLite篇
linux·运维·qt
Cyber4K3 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
Le_ee3 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
yong99904 小时前
MATLAB读取高光谱图像
开发语言·matlab
2zcode4 小时前
基于MATLAB的肝病风险评估与分期分析系统设计与实现
开发语言·matlab
小小de风呀4 小时前
de风——【从零开始学C++】(五):内存管理
开发语言·c++