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 调用结果
相关推荐
Y1rong1 小时前
C++ QT之记事本
开发语言·qt
diegoXie4 小时前
Python / R 向量顺序分割与跨步分割
开发语言·python·r语言
程序员小白条4 小时前
0经验如何找实习?
java·开发语言·数据结构·数据库·链表
liulilittle5 小时前
C++ 浮点数封装。
linux·服务器·开发语言·前端·网络·数据库·c++
IOT-Power5 小时前
QT 串口 源码结构框架
qt
失散135 小时前
Python——1 概述
开发语言·python
萧鼎5 小时前
Python 图像哈希库 imagehash——从原理到实践
开发语言·python·哈希算法
小小8程序员6 小时前
STL 库(C++ Standard Template Library)全面介绍
java·开发语言·c++
立志成为大牛的小牛6 小时前
数据结构——五十六、排序的基本概念(王道408)
开发语言·数据结构·程序人生·算法
老王熬夜敲代码6 小时前
C++中的atomic
开发语言·c++·笔记·面试