在' c'中使用gcc / g ++在stdc ++中编译的问题。程序

时间:2016-04-15 15:45:50

标签: c gcc g++ std

我有一个问题是无法在stdc ++库中编译到我的c程序中。我在Ubuntu 14.04,gcc 4.9。

我的问题是:是否可以将stdc ++编译成c程序。这也可以使用armhf-linux交叉编译器/库来完成吗?

我使用以下命令行编译:

g++ -c cppfile.cpp
gcc -o cfile -lstdc++ cppfile.o cfile.c

我也尝试使用g ++代替gcc,但我也遇到了同样的错误。

我的主要文件是:

// cfile.c
#include <string.h>
#include <stdio.h>
#include "cppfile.h"

int main()
{
    printf("Hello"); 
    myFunction();

    return 0;
}

我的C ++文件在这里:

// cppfile.cpp
#include <string> 

using namespace std;

extern "C" {
int myFunction()
{
    std::string s = "hi";
    return 1;
}
}

标题文件:

// cppfile.h
int myFunction();

编译输出如下:

cppfile.o: In function `myFunction':
cppfile.cpp:(.text+0xf): undefined reference to `std::allocator<char>::allocator()'
cppfile.cpp:(.text+0x27): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
cppfile.cpp:(.text+0x36): undefined reference to `std::allocator<char>::~allocator()'
cppfile.cpp:(.text+0x4a): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
cppfile.cpp:(.text+0x5f): undefined reference to `std::allocator<char>::~allocator()'
cppfile.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'     collect2: error: ld returned 1 exit status

对不起,如果这是重复的,但我找不到这样的问题。

1 个答案:

答案 0 :(得分:0)

这应该有效:

g++ -c cppfile.cpp
gcc -c cfile.c
g++ -o cfile  cppfile.o cfile.o