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 调用结果
相关推荐
q567315236 分钟前
企业级 HTTP 代理采购选型:技术评估清单 15 项
开发语言·网络·爬虫·网络协议·http·隧道ip·代理ip
天天进步201536 分钟前
Python全栈项目--智能办公自动化系统
开发语言·python
cm04Z9c911 小时前
C#摸鱼实录——IoC与DI案例详解
开发语言·c#
long3162 小时前
Java 新手入门与实战开发指南
java·开发语言
摩西蒙2 小时前
计算机网络
开发语言·计算机网络·php
山峰哥2 小时前
数据库性能救星:Explain执行计划深度拆解
服务器·开发语言·数据库·sql·启发式算法
影视飓风TIM3 小时前
Linux下C程序编译:gcc 动态链接与静态链接全解
linux·c语言
花生了什么事o4 小时前
synchronized 与 ReentrantLock:Java 锁机制原理与实现对比
java·开发语言
aramae4 小时前
C++ IO流完全指南:从C标准库到C++流式编程
服务器·c语言·开发语言·c++·后端
ZHOU_WUYI4 小时前
4. light wam 模型loss计算过程
开发语言·人工智能·python