IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 如何在c里面调用c++库(C/C++混合编程) -> 正文阅读

[C++知识库]如何在c里面调用c++库(C/C++混合编程)

一、概述

在实际开发中经常会遇到需要c/c++混合编程的情况。这种可以分为两类

1. C++程序调用C库

这种情况直接调用就行,c++兼容c

2. C程序调用C++库

C++相比C多了class,多了函数重载,因此在C++的编译器中,为了适应这种变化,会在编译过程中自动给函数增加前缀和后缀。这样导致生成的库文件中的函数名与头文件里面的函数名不对应,因此C程序调用C++库时,会提示找不到函数

我们可以用编译器自带的nm来查看.a内部的信息,比如Xilinx自带的工具为arm-none-eabi-nm

执行命令
arm-none-eabi-nm C:/debug/libLeoTest/Debug/liblibLeoTest.a

这是C编译器的结果

00000000 T getLibVersion
0000001c T getSum

这是C++编译器的结果

00000000 T _Z13getLibVersionv
0000001c T _Z6getSumii

2. extern "C"的用法

要实现这个目的,最重要的就是在要引出的函数名前面增加 extern “C”

如果是批量增加,可以使用

extern "C" {

}

而在c语言里面引用的头文件是不需要加这个的,所以一般我们会用宏定义把这块保护起来

#ifdef __cplusplus
extern "C" {
#endif

.....
.....
.....

#ifdef __cplusplus
}
#endif

在C++里面宏__cplusplus默认是生效的,函数都被extern “C” { }包起来了
在C里面宏__cplusplus未定义,忽略了这几行
这样就实现了C和C++共用同一个头文件,避免写两遍重复的代码

下面我们创建一个例子来实际操作一下

二、创建C++ demo库

1. 创建工程

Vitis里面选File -> New -> Library Project
库的类型改为Static library(静态库,扩展名.a)

在这里插入图片描述

模板选C++
在这里插入图片描述

2. 库源文件

libmain.cpp

#include "libLeoTest.h"
int getLibVersion(void)
{
	return 123;
}

int getSum(int a, int b)
{
	return a + b;
}

libclass.h

#ifndef SRC_LIBCLASS_H_
#define SRC_LIBCLASS_H_


class test_class
{
public:
	int a;
	int b;
	int sum(){
		return a+b;
	}

};
#endif

libclass.cpp

#include "libclass.h"

extern "C" int getClassSum(int a, int b)
{
	test_class class1;

	class1.a = a;
	class1.b = b;
	return class1.sum();
}

libLeoTest.h

#ifndef SRC_LIBLEOTEST_H_
#define SRC_LIBLEOTEST_H_

#ifdef __cplusplus
extern "C" {
#endif

int getSum(int a, int b);
int getClassSum(int a, int b);
int getLibVersion(void);



#ifdef __cplusplus
}
#endif


#endif

三、创建c主程序

1. 创建工程并增加main函数

按照hello world的模板创建一个C工程

hellloworld.c

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"

#include "libLeoTest.h"



int main()
{
	int version;
	int sum;
	int classsum;
	
    version = getLibVersion();
    sum = getSum(1, 2);
    sumclass = getClassSum(3, 4);

    return 0;
}

2. 设置库文件的搜索路径

2. 1 在工程名上右键,选择Properties(注意不要选上面的xxxx_system)

2. 2 选择Settings -> ARM v7 gcc linker -> Libraries

2. 3 增加库名称

注意如果库的文件名是liblibLeoTest.a,则输入的库名称应该是libLeoTest

2. 4 增加库搜索路径

选择库文件所在的路径

在这里插入图片描述

3. 编译

然后编译就可以正常使用c++的库文件了

附录: nm的用法

nm --help
Usage: nm [option(s)] [file(s)]
 List symbols in [file(s)] (a.out by default).
 The options are:
  -a, --debug-syms       Display debugger-only symbols
  -A, --print-file-name  Print name of the input file before every symbol
  -B                     Same as --format=bsd
  -C, --demangle[=STYLE] Decode low-level symbol names into user-level names
                          The STYLE, if specified, can be `auto' (the default),
                          `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'
                          or `gnat'
      --no-demangle      Do not demangle low-level symbol names
  -D, --dynamic          Display dynamic symbols instead of normal symbols
      --defined-only     Display only defined symbols
  -e                     (ignored)
  -f, --format=FORMAT    Use the output format FORMAT.  FORMAT can be `bsd',
                           `sysv' or `posix'.  The default is `bsd'
  -g, --extern-only      Display only external symbols
  -l, --line-numbers     Use debugging information to find a filename and
                           line number for each symbol
  -n, --numeric-sort     Sort symbols numerically by address
  -o                     Same as -A
  -p, --no-sort          Do not sort the symbols
  -P, --portability      Same as --format=posix
  -r, --reverse-sort     Reverse the sense of the sort
      --plugin NAME      Load the specified plugin
  -S, --print-size       Print size of defined symbols
  -s, --print-armap      Include index for symbols from archive members
      --size-sort        Sort symbols by size
      --special-syms     Include special symbols in the output
      --synthetic        Display synthetic symbols as well
  -t, --radix=RADIX      Use RADIX for printing symbol values
      --target=BFDNAME   Specify the target object format as BFDNAME
  -u, --undefined-only   Display only undefined symbols
  -X 32_64               (ignored)
  @FILE                  Read options from FILE
  -h, --help             Display this information
  -V, --version          Display this program's version number

nm: supported targets: elf64-x86-64 elf32-i386 elf32-iamcu elf32-x86-64 a.out-i386-linux pei-i386 pei-x86-64 elf64-l1om elf64-k1om elf64-little elf64-big elf32-little elf32-big pe-x86-64 pe-bigobj-x86-64 pe-i386 plugin srec symbolsrec verilog tekhex binary ihex
Report bugs to <http://www.sourceware.org/bugzilla/>.
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-09 12:22:52  更:2022-05-09 12:23:26 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 0:55:18-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码