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 调用结果
相关推荐
你不是我我13 小时前
【Java 开发日记】我们来说一说 Redis IO 多路复用模型
java·开发语言·redis
想七想八不如1140813 小时前
408操作系统 PV专题
开发语言·算法
浩瀚地学13 小时前
【Java】ArrayList
java·开发语言·经验分享·笔记
阿杰同学13 小时前
Java 设计模式 面试题及答案整理,最新面试题
java·开发语言·设计模式
这样の我13 小时前
java 模拟chrome指纹 处理tls extension顺序
java·开发语言·chrome
yong999013 小时前
基于MATLAB的雷达压制干扰仿真
开发语言·matlab
catchadmin13 小时前
现代高效 PHP 开发的最佳实践
开发语言·后端·php
AnAnCode14 小时前
【时间轮算法-实战】Java基于Netty的 `HashedWheelTimer`快速搭建时间轮算法系统
java·开发语言·算法·时间轮算法
liu****14 小时前
12.C语言内存相关函数
c语言·开发语言·数据结构·c++·算法