开发者

What's wrong withe following C code!

开发者 https://www.devze.com 2023-03-22 09:00 出处:网络
I tried the following code in C as well as C++ .file1 is a c file .file2 is a c++ file and file3 is a header file for name magling.

I tried the following code in C as well as C++ .file1 is a c file .file2 is a c++ file and file3 is a header file for name magling.

file1.c

#include<stdio.h>
#include<stdlib.h>
#include "file3.hpp"

int main(int argc,char **argv)
{
int a[5];
int i;
for(i=0;i<5;i++)
    a[i] = i;
printf("%d",a[17]);
return 0;
}

file2.cpp

#include "file3.hpp"

int printtrial(int number)
{
return number;
}

file3.hpp

#ifdef __cplusplus
extern "C"
{
#endif

extern int printtrial(int number);

#ifdef __cplusplus
}
#endif

I compile it using the following commands:

gcc -c file1.c
g++ -c file2.cpp
gcc -o output file1.o file2.o

On this it gives the error:

file2.o:(.eh_开发者_运维问答frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Can anyone tell me what's going on!


As one of your files is compiled as c++ use g++ for linking phase.

See: What is __gxx_personality_v0 for?


C and C++ executables require the presence of some libraries, which are included during the linking stage:

gcc -o output file1.o file2.o

The problem here is that you are trying to link a C++ file using a C linker. gcc simply fails to locate some libraries required by the C++ runtime. To solve this you must use g++, like yi_H said.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号