c++ - 编译 C++ 代码时出错?

标签 c++ gcc g++ compiler-errors

这是我的test.cpp:

#include <iostream.h>
class C {
public:
C();
~C();
};

int main()
{
C obj;
return 0;
}

当我使用命令 g++ test.cpp 编译它时,我得到这个错误信息:

    In file included from /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/iostream.h:31,
                     from test.cpp:1:
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the  header for the  header for C++ includes, or  instead of the deprecated header . To disable this warning use -Wno-deprecated.
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/ccoYkiAS.o:test.cpp:(.text+0x131): undefined reference to `C::C()'
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/ccoYkiAS.o:test.cpp:(.text+0x13c): undefined reference to `C::~C()'
    collect2: ld returned 1 exit status

gcc test.cpp 编译会给出类似甚至更多的信息:

    In file included from /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/iostream.h:31,
                     from test.cpp:1:
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the  header for the  header for C++ includes, or  instead of the deprecated header . To disable this warning use -Wno-deprecated.
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0xd): undefined reference to `std::basic_string, std::allocator >::size() const'
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x60): undefined reference to `std::basic_string, std::allocator >::operator[](unsigned int) const'
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x9f): undefined reference to `std::basic_string, std::allocator >::operator[](unsigned int) const'
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0xce): undefined reference to `std::basic_string, std::allocator >::operator[](unsigned int) const'
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x131): undefined reference to `C::C()'
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x13c): undefined reference to `C::~C()'
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x165): undefined reference to `std::ios_base::Init::Init()'
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x180): undefined reference to `std::ios_base::Init::~Init()'
    collect2: ld returned 1 exit status

注意我还没有设置LD_LIBRARY_PATH:

    bash-3.2$ echo $LD_LIBRARY_PATH

    bash-3.2$ 

最佳答案

您已经声明了 C 构造函数和析构函数的存在,但尚未提供实现。尝试:

class C {
public:
    C() {}
    ~C() {}
};

并且,对于 C++ 程序,使用 g++ 进行编译(如您的第一次尝试)。

关于c++ - 编译 C++ 代码时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2361530/

相关文章:

c++ - 如何将一种类型的 unique_ptr 复制到另一种类型

c++ - 如何将 merge_sort 算法连接到 Vector 和用户定义的 dynamic_array? - 去做

gcc - ARM 汇编器的奇怪 GCC 行为。 AND SEQ 指令

gcc - x86_64 上的软浮点

c++ - 错误 : ‘make_reverse_iterator’ is not a member of ‘std’

C++:从外部在类内声明枚举器,因此可以在私有(private)成员中使用

c++ - 持续集成服务能否在本地构建 Linux 包?

c++ - "Bad"GCC优化性能

c++ - g++ 内联调用 always_inline "int _rdrand16_step()"失败

C++11 for_each 和 lambdas 优化