Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
呼叫C++;包含来自C的STL数据类型的函数 我在C++中有一个函数,使用STL的数据类型,如向量 > map < /C> >。以下是一些示例代码:_C++_C_Gcc - Fatal编程技术网

呼叫C++;包含来自C的STL数据类型的函数 我在C++中有一个函数,使用STL的数据类型,如向量 > map < /C> >。以下是一些示例代码:

呼叫C++;包含来自C的STL数据类型的函数 我在C++中有一个函数,使用STL的数据类型,如向量 > map < /C> >。以下是一些示例代码:,c++,c,gcc,C++,C,Gcc,mylib.cpp #include "mylib.h" #include<vector> using namespace std; int summation(int n) { vector<int> numbers; int sum = 0; for(int i = 1; i <=n; i++) numbers.push_back(i); for(int j = 0; j < numbers.si

mylib.cpp

#include "mylib.h"
#include<vector>
using namespace std;

    int summation(int n) {
    vector<int> numbers;
    int sum = 0;
    for(int i = 1; i <=n; i++) 
        numbers.push_back(i);
    for(int j = 0; j < numbers.size(); j++)
        sum += numbers[j];

    return sum;
    }

使用命令< /p>将C++文件编译成ObjjCT代码

g++ -o mylib.o -c mylib.cpp
然后,我编写了一个C程序,以便使用它的函数
summation

main.c

#include<stdio.h>
#include "mylib.h"

int main() {
    int n;
    scanf("%d", &n);
    printf("%d", summation(n));
    return 0;
}
我得到以下错误

/tmp/ccAMN2ld.o: In function `main':
main.c:(.text+0x33): undefined reference to `summation'
mylib.o: In function `std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&)':
mylib.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x26e): undefined reference to `__cxa_begin_catch'
mylib.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x2d7): undefined reference to `__cxa_rethrow'
mylib.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x2df): undefined reference to `__cxa_end_catch'
mylib.o: In function `std::vector<int, std::allocator<int> >::_M_check_len(unsigned long, char const*) const':
mylib.cpp:(.text._ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc[_ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc]+0x5b): undefined reference to `std::__throw_length_error(char const*)'
mylib.o: In function `__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)':
mylib.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[_ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim]+0x1c): undefined reference to `operator delete(void*)'
mylib.o: In function `__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)':
mylib.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x2c): undefined reference to `std::__throw_bad_alloc()'
mylib.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x3c): undefined reference to `operator new(unsigned long)'
mylib.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
/tmp/ccAMN2ld.o:在函数“main”中:
main.c:(.text+0x33):对“总和”的未定义引用
mylib.o:在函数“std::vector::_M_insert_aux(u gnu_cxx::u normal_iterator,int const&)”中:
mylib.cpp:(.text.\znst6vectorisaiee13\u M\u insert\u auxEN9\u gnu\u cxx17\u normal\u iteratorIPiS1\u EERKi[\u znst6vectorisaiee13\u M\u insert\u auxEN9\u gnu cxx17\u normal\u iteratorIPiS1\u erki]+0x26e):对“cxa\u begin\u catch”的未定义引用
mylib.cpp:(.text.\u znst6vectorisaiee13\u M\u insert\u auxEN9\u gnu\u cxx17\u normal\u iteratorIPiS1\u EERKi[\u znst6vectorisaiee13\u M\u insert\u auxEN9\u gnu cxx17\u normal\u iteratorIPiS1\u EERKi]+0x2d7):未定义的“cxa\u rethrow”引用
mylib.cpp:(.text.\znst6vectorisaiee13\u M\u insert\u auxEN9\u gnu\u cxx17\u normal\u iteratorIPiS1\u EERKi[\u znst6vectorisaiee13\u M\u insert\u auxEN9\u gnu cxx17\u normal\u iteratorIPiS1\u EERKi]+0x2df):未定义的“cxa\u end”引用
mylib.o:在函数“std::vector::_M_check_len(无符号长,字符常量*)常量”中:
mylib.cpp:(.text.znkst6vectoriaisaiee12_um_ucheck_ulenempkc[znkst6vectoriaisaiee12_um_ucheck_ulenempkc]+0x5b):未定义对“std::u throw_ulength_uerror(char const*)”的引用
mylib.o:在函数“\uu gnu\u cxx::new\u allocator::deallocate(int*,unsigned long)”中:
mylib.cpp:(.text.ZN9_ugnu_cx13new_分配器10deallocateepim[ZN9_ugnu_cx13new_分配器10deallocateepim]+0x1c):对“运算符删除(void*)”的未定义引用
mylib.o:在函数“\uu gnu\u cxx::new\u allocator::allocate(unsigned long,void const*)”中:
mylib.cpp:(.text.ZN9_ugnu_x13;new_allocatorie8allocateempkv[ZN9_ugnu_x13;new_allocatorie8allocateempkv]+0x2c):未定义对“std:u throw_bad_alloc()的引用
mylib.cpp:(.text.ZN9_uugnu_cxx13new_分配器8allocateempkv[ZN9_ugnu_cxx13new_分配器8allocateempkv]+0x3c):对“运算符新(无符号长)”的未定义引用
mylib.o:(.eh_frame+0x4b):未定义对“\uuuugxx\u personality\uv0”的引用
collect2:错误:ld返回了1个退出状态
我必须使用gcc来编译C文件。有没有办法让它发挥作用

我试图寻找解决方案,但无意中发现了下面的链接


但是找不到解决问题的方法。

我已经用g++构建了所有文件,没有收到链接错误。能否将所有源文件构建为gcc或g++。

使用C编译器自行编译C文件:

gcc -c cfile.c
g++ -o a.out main.o cfile.o mylib.o

然后将所有文件与C++编译器链接:

gcc -c cfile.c
g++ -o a.out main.o cfile.o mylib.o

<>请注意,必须使用C++编译器编译具有<代码>主< /代码>函数的文件。任何其他文件都可以编译,但您需要<代码>外部“C”< /C>声明,以便能够调用C++的C代码或创建C++函数,可以从C< /P>调用。为什么C编译器知道如何编译C++组件。也许你需要编译C++组件,然后链接二进制文件。这个问题看起来像是你链接到的那些。答案描述了一般的可能解决方案,并且你需要在具体代码中实现它们,就是这样。使用<代码> G++<代码>链接代码(不是<代码> GCC < /代码>——程序是C++程序,因为它里面有一些C++)。通常使用<代码> G++<代码>编译包含主()/<代码>的文件,因为它在C++中具有特殊的属性(与C不同),尽管您可以使用C编译器创建包含“代码>主代码”(/CODE)的对象文件。可以尝试编译<代码>主代码> C <代码> > <代码>主体> o <代码> >代码> GCC < /代码>,然后链接
g++-o program main.o mylib.o
——你可能会幸运地逃脱。否则,创建
cpp main.cpp
包含
extern“C”int C_main();int main(){return c_main();}
(或
extern“c”int c_main(int argc,char**argv);int main(int argc,char**argv){return c_main(argc,argv);}
)并用
g++
编译。在现有的
main.c
中,将函数更改为
int c_main(void){…您的代码…}
(或具有参数的等效项),不要忘记声明函数(因为它不再被称为
main()
)关键是把程序与<代码> g++< /COD>链接,以便得到正确的C++库链接。相比之下,与
gcc
链接将是一项痛苦的工作。如有疑问,请尝试使用
g++-v
gcc-v
执行不同的链接操作,并查看为执行链接而调用的命令之间有多大差异。