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 调用结果
相关推荐
@atweiwei6 分钟前
LangChainRust:用 Rust 构建高性能 LLM 应用的完整指南
开发语言·人工智能·ai·rust·大模型·llm·agent
itman30114 分钟前
C语言printf输出格式:%d %f %s等用法详解
c语言·字符串·printf·格式化输出·整数
老约家的可汗17 分钟前
搜索二叉树的概念及使用
java·开发语言
不知名的老吴19 分钟前
R语言4.3.0安装包百度网盘中文版下载与详细安装指南
开发语言·r语言
棉猴21 分钟前
python海龟绘图之计算夹角towards()
开发语言·python·turtle·海龟绘图·towards
张人玉26 分钟前
SMT 贴片机上位机项目
开发语言·c#
我不是懒洋洋26 分钟前
【经典题目】链表OJ(相交链表、环形链表、环形链表II、随机链表的复制)
c语言·开发语言·数据结构·链表·ecmascript·visual studio
ん贤29 分钟前
口述Map
开发语言·面试·golang
YuanDaima204830 分钟前
Python 数据结构与语法速查笔记
开发语言·数据结构·人工智能·python·算法
asdzx6737 分钟前
C#:从 URL 下载 PDF 文档到本地
开发语言·pdf·c#