顯示具有 C++ 標籤的文章。 顯示所有文章
顯示具有 C++ 標籤的文章。 顯示所有文章

2015年9月21日 星期一

如何更新 Ubuntu 12.04 上的 gcc 與 g++

當安裝好 Ubuntu 12.04 其預設安裝的 gcc 與 g++ 版本為 4.6 你可以透過 g++ --version 來查詢你系統上的版本:
linux:~$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

更新步驟可依序執行下列指令即可:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-5 g++-5

為了讓 update-alternatives 知道我們系統上有 2 套 gcc 與 g++ 編譯器,我們必須為每套 gcc 與 g++ 建立安裝紀錄;然後再透過 config 來設定系統所要載入的版本,設定方法如下:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 40 --slave /usr/bin/g++ g++ /usr/bin/g++-5
sudo update-alternatives --config gcc

當你執行 sudo update-alternatives --config gcc 會看到如下的選單畫面,而在此畫面我選擇了 2:
linux:~$ sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-4.6   60        auto mode
  1            /usr/bin/gcc-4.6   60        manual mode
  2            /usr/bin/gcc-5     40        manual mode

Press enter to keep the current choice[*], or type selection number: 2       
update-alternatives: using /usr/bin/gcc-5 to provide /usr/bin/gcc (gcc) in manual mode.

完成上面設定後執行 g++ --version 你應該可以看到如下面的訊息:
linux:~$ g++ --version
g++ (Ubuntu 5.1.0-0ubuntu11~12.04.2) 5.1.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


[參考連結]

2014年3月4日 星期二

Field `x' has incomplete type?

       當你有兩個Class,CLASS_A與CLASS_B時,如果再主程式中並沒有用include將其header file include進來時,而使用forward reference 時會出現下列的錯誤訊息:

aggregate `CLASS_B bb' has incomplete type and cannot be defined
aggregate `CLASS_A aa' has incomplete type and cannot be defined

範例:
main.cpp
class CLASS_A;
class CLASS_B;

int main()
{
     CLASS_A a;
     CLASS_B b;
     return 0;
}

CLASS_A.h
class CLASS_A{
     public:
            CLASS_A(){}
};

CLASS_B.h
class CLASS_B{
     public:
            CLASS_B(){}
};

         另外,你也許用CLASS_B的物件當做 CLASS_A建構的參數,你也許會看到下面的錯誤訊息:
aggregate `CLASS_B bb' has incomplete type and cannot be defined 
variable `CLASS_A aa' has initializer but incomplete type

範例:
main.cpp
class CLASS_A;
class CLASS_B;

int main()
{
     CLASS_B b;
     CLASS_A a(&b);
     return 0;
}

CLASS_A.h
class CLASS_B; // Forward reference declare

class CLASS_A{
     public:
            CLASS_A( CLASS_B *pB){}
};

      這些是典型的忘記將 header file inlude進來的問題,只要將該檔案的header file include到 main.cpp中即可。會發生 incomplete type 是因為編譯器在編譯時需要知道物件的大小,但是我們 在還不知道class定義在何處,就將其拿來宣告物件使用。 其詳細的 incomplete type 的解釋請參考 下面相關鏈結。

[Keyword]
    incomplete type ; incomplete type + C++ + Class

[相關鏈結]
Incomplete class declarations (C++ only)
Incomplete Type
Forward declarations in C++
C++ Tip: Forward Declaration
forward declaration 唯一的好处只是可以让编译快一点