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 调用结果
相关推荐
4***17544 小时前
3.3 Python图形编程
开发语言·python·pygame
橙子圆1234 小时前
java之拦截器和适配器模式
java·开发语言
时空系4 小时前
第3篇:数据的运算——让数据动起来 Rust中文编程
开发语言·后端·rust
Shadow(⊙o⊙)5 小时前
智能指针、循环引用、锁、删除器
开发语言·c++·后端·visual studio
leoufung5 小时前
LeetCode 30:Substring with Concatenation of All Words 题解(含 C 语言 uthash 实现)
c语言·leetcode·c#
Sylvia-girl5 小时前
C++模板【上】
开发语言·c++
2zcode5 小时前
基于MATLAB多特征融合与SVM的金属表面缺陷检测系统
开发语言·支持向量机·matlab
2zcode5 小时前
基于MATLAB脑电信号的帕金森病抑郁症检测研究
开发语言·matlab·抑郁症·帕金森病
untE EADO5 小时前
Java进阶之路,Java程序员职业发展规划
java·开发语言
爱编码的小八嘎5 小时前
C语言完美演绎9-6
c语言