簡體   English   中英

編譯C ++代碼時出錯?

[英]Error in compiling C++ code?

這是我的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進行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++進行編譯(如您的第一次嘗試)。

更換

#include <iostream.h>

通過

#include <iostream>

並提供類C的構造函數和析構函數的實現(至少為空)。

由於您沒有提供實際的問題,因此我不得不猜測您想知道的內容。 無論如何,我的2c是:

  • 不要使用iostream.h ,該標頭是預標准的並且已經過時。 使用<iostream>代替
  • 您沒有為C的構造函數和析構函數提供任何實現,而鏈接器在抱怨這些實現。

您需要定義C構造函數和析構函數:

C::C()
{
}

C::~C()
{
}

另外,堅持使用g ++進行編譯。 如果仔細觀察,使用gcc編譯時遇到的錯誤包括使用g ++時得到的所有錯誤以及其他錯誤。

您要包含iostream.h而不是iostream ,這就是為什么您會收到有關此包含的警告的原因。 另外,您已經為C聲明了構造函數和析構函數,但實際上並沒有在任何地方實現它。 因此,鏈接器抱怨未定義的符號。

您需要為C的方法添加實現,例如:

C::C() {
  // ...
}

使用#include <iostream>而不是#include <iostream.h>

您應該正確閱讀錯誤。

關於LD_LIBRARY_PATH的注釋-在編譯或鏈接時不關心您(然后,鏈接器將查找-L給定的路徑以及一些標准路徑,例如/ usr / lib)。

重要的是運行應用程序時-系統將首先在LD_LIBRARY_PATH中指定的路徑中搜索共享庫。

http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM