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

标签 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;
}

最佳答案

通常,您会使用 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 是可选的。)

关于c++ - 编译应用程序时出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18539515/

相关文章:

c++ - 有人可以向我解释一下 C++ 中复杂的函数指针类型吗

c++ - Windows:构建同一 DLL 的 32 位和 64 位版本

c - 将 void * 值设置为 intptr_t 变量是否需要显式转换?

无法让简单的 GTK3 应用程序运行

c - 使用扩展汇编语法在实模式下调用 BIOS 服务存在困难

c++ - 在 CMake 中使用预编译的头文件

从命令行中的地址调用函数

c++ - 使用unsigned int时出现无限循环

c++ - Mat队列中的访问冲突

c++ - 在 Mac OSX 中模拟 ioquake3 的按键事件