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 调用结果
相关推荐
吃好睡好便好7 小时前
用while循环语句求和
开发语言·学习·算法·matlab·信息可视化
TechWayfarer7 小时前
查询IP所在地的3种方案:从API到离线库,风控场景怎么选?
开发语言·网络·python·网络协议·tcp/ip
摇滚侠7 小时前
Java 零基础全套教程,集合框架,笔记 153-163
java·开发语言·笔记
王璐WL7 小时前
【C语言入门级教学】函数的概念2
c语言·数据结构·算法
程序员榴莲8 小时前
Python 单例模式
开发语言·python·单例模式
L、2188 小时前
CANN算子开发调试实战:从“Segmentation Fault“到定位根因的完整流程
java·开发语言
狗凯之家源码网8 小时前
基于PHP的多语言跨境电商B2B2C商城系统技术解析
开发语言·php
比特森林探险记9 小时前
go 语言中的context 解读和用法
开发语言·后端·golang
古城小栈9 小时前
Rust 调用 C 语言库 实战指南(企业级)
c语言·开发语言·rust