简体   繁体   中英

Issues compiling in stdc++ using gcc/g++ in a 'c' program

I have an issue of not being able to compile in the stdc++ library into my c program. I'm on Ubuntu 14.04, gcc 4.9.

My question is: is it possible to compile in the stdc++ into ac program. And can this also be done using the armhf-linux cross compiler/libraries?

I'm using the following command line to compile:

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

I've also tried using g++ instead of gcc but I get the same errors.

My main file is:

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

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

    return 0;
}

My C++ file is here:

// cppfile.cpp
#include <string> 

using namespace std;

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

Header file:

// cppfile.h
int myFunction();

The compile output is as follows:

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

I'm sorry if this is a duplicate, but I couldn't find a question like this.

This should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM