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 调用结果
相关推荐
爱编码的小八嘎7 分钟前
C语言完美演绎9-1
c语言
geovindu11 分钟前
go: Proxy Pattern
开发语言·后端·设计模式·golang·代理模式
langsiming16 分钟前
【无标题】
java·开发语言·数据库
꧁细听勿语情꧂18 分钟前
合并两个有序表、判断链表的回文结构、相交链表、环的链表一和二
c语言·开发语言·数据结构·算法
Rust语言中文社区19 分钟前
【Rust日报】2026-04-24 Vizia 0.4 发布——纯 Rust 声明式响应式 GUI 框架
开发语言·后端·rust
普通网友28 分钟前
JavaScript:ESLint+Prettier 规范代码格式
开发语言·javascript·ecmascript
t***54429 分钟前
如何在 Dev-C++ 中切换编译器
java·开发语言·c++
jiayong2341 分钟前
第 38 课:任务列表里高亮当前正在查看详情的任务
开发语言·前端·javascript·vue.js·学习
澈2071 小时前
构造函数与析构函数完全指南
开发语言·c++
W23035765731 小时前
C++ 高并发线程池实战(二):动态缓存线程池 + 调用者运行拒绝策略完整版实现
开发语言·c++·缓存