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 调用结果
相关推荐
小猫咪怎么会有坏心思呢5 分钟前
华为OD机试-最短木板长度-二分法(A卷,100分)
java·开发语言·华为od
wo32586614511 分钟前
浪潮交换机配置track检测实现高速公路收费网络主备切换NQA
开发语言·网络·php
知识中的海王16 分钟前
Python html 库用法详解
开发语言·python
獨枭33 分钟前
配置 macOS 上的 Ruby 开发环境
开发语言·macos·ruby
飞由于度39 分钟前
C#中清空DataGridView的方法
开发语言·c#
朝朝又沐沐1 小时前
基于算法竞赛的c++编程(18)string类细节问题
开发语言·c++·算法
Wangshanjie_981 小时前
【C语言】-递归
c语言
黄雪超2 小时前
JVM——对象模型:JVM对象的内部机制和存在方式是怎样的?
java·开发语言·jvm
爱学习的capoo2 小时前
matlab自控仿真【第一弹】❀传递函数和输出时域表达式
开发语言·matlab
EverBule2 小时前
Python 训练 day46
开发语言·python