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 调用结果
相关推荐
星光一影19 分钟前
基于PHP+MySQL+Uniapp的上门家政服务系统源码
开发语言·mysql·uni-app·php
Antonio91520 分钟前
【Swift】 UIKit:UIGestureRecognizer和UIView Animation
开发语言·ios·swift
D***t13124 分钟前
PHP在API开发中的框架选择
开发语言·php
H***997624 分钟前
Java虚拟现实案例
java·开发语言·vr
Tan_Ying_Y36 分钟前
synchronized和ReentrantLock的区别是什么?他们的底层原理是什么?
开发语言·c#
ChineHe43 分钟前
Golang并发编程篇002_Go并发基础
开发语言·后端·golang
默恋~微凉43 分钟前
shell(八)——WEB与Nginx
开发语言·前端·php
lqj_本人1 小时前
鸿蒙Qt网络通信:HTTPS握手失败与证书陷阱
qt·https·harmonyos
lsx2024061 小时前
Go 语言类型转换
开发语言
t***L2662 小时前
JavaScript在机器学习中的库
开发语言·javascript·机器学习