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 调用结果
相关推荐
l1t1 小时前
DeepSeek 4.1总结的Tom Lane 谈塑造 Postgres 三十年历程的架构决策
开发语言·数据库·postgresql·架构
djarmy7 小时前
MAIN.c(1): warning C318: can’t open file ‘STC8G.H’ 报错 解决 分析
c语言·开发语言·mongodb
HEJOO97 小时前
深入理解 Java volatile 关键字:原理、用法与常见误区
java·开发语言·spring
曹牧7 小时前
Java:no content to map due to end of input
java·开发语言
梦梦代码精8 小时前
《回收租赁系统技术选型避坑指南:业务闭环与二开自由度详解》
开发语言·低代码·docker·开源·代码规范
隔窗听雨眠8 小时前
记一次SQL Server数据库性能分析:从CPU100%到单配置修复的完整诊断
开发语言·数据库·php
David猪大卫8 小时前
【C++修炼】智能指针使用及原理
开发语言·c++·经验分享·笔记·学习·考研·面试
charlie1145141918 小时前
现代Qt开发之图像处理进阶:像素操作与格式转换
开发语言·图像处理·qt
李永奉8 小时前
中科蓝讯SDK开发-BT893x 面条耳机连接vivo手机后连接APP端,最后手机端设置页面手动断开蓝牙连接,此时耳机端假断开
c语言·开发语言·嵌入式硬件·物联网·智能手机·电脑
秦老师Q9 小时前
php零基础入门到实战,从语法到框架,一篇搞定
开发语言·php