运行C++;(使用STL)C应用程序中的函数 我创建了一个C++库,其中使用了一些STL功能的C函数。我想在C应用程序中包含这个库

运行C++;(使用STL)C应用程序中的函数 我创建了一个C++库,其中使用了一些STL功能的C函数。我想在C应用程序中包含这个库,c++,c,gcc,stl,C++,C,Gcc,Stl,我已经尽可能地将问题简化为以下4个文件 main.c #包括“aaa.h” #包括 int main() { printf(“版本:%u.%u\n”,GetAPIMajorVersion(),GetAPIMinorVersion()); 返回0; } aaa.h \ifndef AAA\u H #定义AAA_H #ifdef_uucplusplus #定义DllExport外部“C” #else/\uu cplusplus #定义DllExport #endif/\uu cplusplus #

我已经尽可能地将问题简化为以下4个文件

main.c

#包括“aaa.h”
#包括
int main()
{
printf(“版本:%u.%u\n”,GetAPIMajorVersion(),GetAPIMinorVersion());
返回0;
}
aaa.h

\ifndef AAA\u H
#定义AAA_H
#ifdef_uucplusplus
#定义DllExport外部“C”
#else/\uu cplusplus
#定义DllExport
#endif/\uu cplusplus
#包括
DllExport uint32_t getapimajorvision();
DllExport uint32_t getapiminorvision();
#endif//AAA_H
aaa.cpp

#包括“aaa.h”
#包括
#包括
//建造和工作良好。
uint32_t GetAPIMajorVersion()
{
std::string val=“hello world”;
返回值size();
}
//生成错误消息
uint32\u t getapiminorvision()
{
病媒试验;
测试。推回(真);
返回测试。size();
}
我使用以下脚本来构建库和应用程序

build.sh

<代码> >构建C++库 g++-m64-Wall-O3-c-fmessage length=0-fPIC-MMD-MP aaa.cpp-o aaa.o ar rcs libaaa.a aaa.o #构建可执行文件 gcc-m64-墙壁-静态干管.c-o干管-L./-laaa 当我尝试构建C应用程序时,我遇到以下错误

.//libaaa.a(aaa.o): In function `GetAPIMinorVersion':
aaa.cpp:(.text+0xeb): undefined reference to `operator delete(void*)'
aaa.cpp:(.text+0x1c7): undefined reference to `operator delete(void*)'
.//libaaa.a(aaa.o): In function `std::vector<bool, std::allocator<bool> >::_M_insert_aux(std::_Bit_iterator, bool)':
aaa.cpp:(.text._ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb[_ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb]+0x1d8): undefined reference to `operator new(unsigned long)'aaa.cpp:(.text._ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb[_ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb]+0x339): undefined reference to `operator delete(void*)'
aaa.cpp:(.text._ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb[_ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb]+0x3cb): undefined reference to `std::__throw_length_error(char const*)'.//libaaa.a(aaa.o):(.data.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
我应该如何构建这个包含STL函数的库,使库函数可以从C应用程序调用

编辑

  • 我最后使用的编译器是
  • 在Arm Keil IDE/编译器中似乎没有一个选项可以将stdc++作为库包含在内。据我所知,我无法将构建C应用程序的命令更改为
    gcc-m64-Wall-static main.C-o main-L./-laaa-lstdc++

您可以尝试构建C++共享库,-LSTDC++< /C> 因此,让
-laaa
成为一个共享库
libaaa.So
(来自源文件
aaa1.cc
aaa2.cc
,并拥有)您将使用以下内容构建:

 g++ -fPIC -O3 -g aaa1.cc -o aaa1.pic.o
 g++ -fPIC -O3 -g aaa2.cc -o aaa2.pic.o
 g++ -fPIC -shared -O3 -g aaa1.pic.o aaa2.pic.o -lstdc++ -o libaaa.so
你也可以设置一些

读德雷珀的

最后我使用的编译器是Arm-Keil

您最好使用最新版本的交叉编译器(或交叉编译器)。要么您自己从的源代码(2018年秋季)构建交叉编译器,要么在Linux发行版上安装一些交叉编译器。例如,Debian/Sid具有
gcc-8-arm-linux-gnueabi
gcc-8-arm-linux-gnueabihf

根据经验,硬件供应商提供古老的交叉编译器(在软件工程方面并不擅长)。这就是为什么我建议在命令行上使用最新的GCC交叉编译器

您最好将应用程序链接到
g++

我的下一个猜测是std::vector::push_back()函数可能引发异常

异常需要(for)级别的一些支持。如果你的库抛出了一些异常,主程序必须与<代码> G++< /C>链接(如果你想从C中使用C++库,它就不应该在外部抛出异常)。

但是,可以很小心地构建一个C++代码库,它可以从代码> GCC < /COD>编译的C代码中使用。是这样一个库(但它不会在外部抛出异常)

据我所知,我无法将构建C应用程序的命令更改为gcc-m64-Wall-static main.C-o main-L./-laaa-lstdc++


你当然可以。您需要避免使用Arm-Kell,直接在命令行上使用适当的交叉编译器(可以是其中提供的交叉编译器,或者最好是从GCC源代码或Clang源代码构建的较新的交叉编译器)。

您的lib归档文件只包含您生成的代码;没有你需要的C++运行时。我们的期望是,消费计划将提供这一点。我怀疑如果你研究、学习了如何创建共享对象库(.so),我可能会解决你的问题。使用
g++
来创建可执行文件,而不是
gcc
<代码> G++< /COD>链接,提供一些由C++代码提供的函数库,包括默认的代码< >运算符New()//>代码实现。如果目标系统只有一个C99编译器,那么如果从源代码编译,就不能使用C++。为此,您需要使用交叉编译器(构建系统,它需要C和C++功能,以其他系统为目标)。您可能需要将 C++ +/Cudio>运行库添加到链接阶段<代码> -LSTDC++< /COD>。(请参阅)结束接受您的建议,转而使用其他编译器。屁股痛得厉害,但它起作用了。为什么这么痛?Arm Kell有一些旧的gcc在里面
 g++ -fPIC -O3 -g aaa1.cc -o aaa1.pic.o
 g++ -fPIC -O3 -g aaa2.cc -o aaa2.pic.o
 g++ -fPIC -shared -O3 -g aaa1.pic.o aaa2.pic.o -lstdc++ -o libaaa.so