1

我正在使用 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;
}
4

1 回答 1

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是可选的。)

于 2013-08-30T18:43:49.707 回答