编译应用程序时出现奇怪的错误

时间:2013-08-30 18:40:56

标签: c++ gcc

我正在使用Fedora 18(使用Gnome),我已经安装了gcc和gcc-c ++,当我使用gcc -o slowka.o slowka.cpp命令时,我看到了以下错误:

slowka.cpp:(.text+0x1b): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
slowka.cpp:(.text+0x8d): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
slowka.cpp:(.text+0xa0): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccp7fTFJ.o: In function `__static_initialization_and_destruction_0(int, int)':
slowka.cpp:(.text+0xdb): undefined reference to `std::ios_base::Init::Init()'
slowka.cpp:(.text+0xea): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccp7fTFJ.o: In function `bool std::operator==<char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*)':
slowka.cpp:(.text._ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_EPKS3_[_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_EPKS3_]+0x1f): undefined reference to `std::string::compare(char const*) const'
/tmp/ccp7fTFJ.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status

我不知道是什么原因。我的代码:

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main()
{
printf("Witaj w aplikacji dodającej słówka ! Czy chcesz włączyć aplikację w tryb permanentny (t/N) ?\n");
string x;
scanf("%s", &x);
if(x != "t" && x != "T")
{
printf("Wybrano tryb \"jednego słówka\" !\n");
return 0;
}
return 0;
}

1 个答案:

答案 0 :(得分:4)

通常,您使用C ++编译器链接C ++程序:

g++ -o slowka.o slowka.cpp

但是,如果您想要一个目标文件,则需要指定-c

g++ -c -o slowka.o slowka.cpp

或者也许:

gcc -c -o slowka.o slowka.cpp

(输出名称将由编译器自动推断,因此-o slowka.o是可选的。)