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.


[參考連結]

2015年8月25日 星期二

如何透過 adb command 取得Android CPU 資訊

        將手機連上電腦後在 console Mode 下執行 "adb shell cat /proc/cpuinfo" 即可得到如下訊息,如果手機是四核心則會出現四次一樣的訊息。

~$ adb shell cat /proc/cpuinfo

processor : 0
vendor_id : GenuineIntel
cpu family : 6
model  : 90
model name : Intel(R) Atom(TM) CPU  Z3560  @ 1.00GHz
stepping : 0
microcode : 0x34
cpu MHz  : 500.000
cache size : 1024 KB
physical id : 0
siblings : 4
core id  : 0
cpu cores : 4
apicid  : 0
initial apicid : 0
fpu  : yes
fpu_exception : yes
cpuid level : 11
wp  : yes
flags  : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes rdrand lahf_lm 3dnowprefetch ida arat epb dtherm tpr_shadow vnmi flexpriority ept vpid tsc_adjust smep erms

[參考鏈結]

2015年5月26日 星期二

[Error] Invalid conversion from 'int' to 'main(int, char**)::fruit_tea' [-fpermissive]


當你嘗試編譯將 int 數值指定給 enum 型態的變數時,會出現編譯錯誤訊息如下:
Invalid conversion from 'int' to 'main(int, char**)::fruit_tea' [-fpermissive]

範例程式如下:
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) 
{
 enum fruit_tea { apple, banana, orange } taste;
 taste = 10;
 std::cout << taste << std::endl;
 return 0;
}

解決方法就是將 fpermissive flag 加入到編譯的選項中,下面的圖示是使用 OrWell Dev-C++ IDE,設定方法步驟:點選 project 右鍵 >> Project Options... >> Parameters 在 C++ compiler中填入 -fpermissive 即可。重新編譯程式時,原本錯誤的地方就變成是 Warning 了。


下面有關 fpermissive flag 的解釋擷取至 3.5 Options Controlling C++ Dialect 。主要是讓原本編譯器將某些不合適的 code 判斷為錯誤的訊息改成為警告訊息,以便讓程式可以編譯成功。

-fpermissive
Downgrade some diagnostics about nonconformant code from errors to warnings. Thus, using -fpermissive will allow some nonconforming code to compile. 

上面的例子,想要將整數值指定給列舉型態變數;因為指定的整數值未必在列舉型態的範圍內,所以C++ 禁止將 整數值指定給 列舉型態變數,但可以將列舉型態變數值指定給整數型態變數,因為列舉型態也是一種整數型態。

基本上不建議使用 fpermissive flag 來解此類的問題,如上面所說 整數值未必在 列舉型態的範圍中,允許指定整數值給列舉型態可能會造成不必要的麻煩。


[相關鏈結]

2015年5月21日 星期四

Error C4335: Mac file format detected: please convert the source file to either DOS or UNIX format

最近把一個 Third Party Lib include 到專案內;使用VS 2010 編譯時卻出現下列問題,在 Ubuntu下編譯卻不會這樣的問題發生。比較快的方式是將所有的 Third Party 的 Source code 修正成 Windows 或者 UNIX 的檔案格式。但這樣不是很理想,我們不應該修改 Third Party 的Source code 除非必要。這樣以後要 Upgrade 才不會遇到太多問題。
 
error C4335: Mac file format detected: please convert the source file to either DOS or UNIX format

要解決這個問題又不改到 Third Party Source Code 的方法就是直接把那個 C4335 關閉即可,下面是如何關閉的圖示:


  • 點選專案 > 右鍵 > 選擇 Properties
  • Configuration Properties > C/C++ > Advanced > Disable Specific Warnings 填入 4335 即可。



2015年1月21日 星期三

讓 Code::Blocks IDE 輸出中繼檔案

     編譯 C/C++ 檔案時你可以設定 flag 讓編譯器產邊中繼檔案,一般  Code::Blocks 未做任何設定時只會輸出 .o 檔,如果你編譯器是使用 GCC,那麼你可以設定編譯器的 -save-temps 來輸出 .ii 與 .s 檔。.ii 就是前置處理器完後的 source 檔,.s 就是 asm 檔。如果使用 -save-temps 則輸出的檔案與你的 cpp 檔案放在同一個目錄下,而設定為 -save-temps=obj 則會輸出到 obj 的目錄下。下面是 Code::Blocks 設定畫面:




2015年1月8日 星期四

如何用 Code::Blocks IDE 編譯 C++11 程式

      假設你在 Code::Blocks 中,專案使用的是 g++ 編譯器來編譯,你可以點選專案右鍵後選擇 "Build options..." 然後將 -std=c++11 選項勾選即可編譯 c++11 feature 的程式了。
設定畫面如下:



測試範例程式如下:(來源 C++11 Wiki Range-based_for_loop)

#include <iostream>

using namespace std;

int main()
{
    //Range-based for loop
    int my_array[5] = {1, 2, 3, 4, 5};
    for (auto &x : my_array) {
        cout << x << " ";
    }
    cout << endl;
    return 0;
}

       注意:專案的編譯器設定會被 全域的編譯器設定所覆蓋,假設你在專案中設定 -std=c++11,但在全域編譯器中設定了 -std=c++98,則編譯最後是使用 c++98來編譯程式。 檢查 Build log 你會發現,編譯的指令為 mingw32-g++.exe -Wall -fexceptions -std=c++11 -g -std=c++98 ...。

Code::Blocks IDE 如何設定 Boost C++ libraries

        先到 Boost C++ libraries 網站下載 Boost libs 壓縮檔案,然後將其解壓縮,解壓縮後的目錄結構如下:

     然後開啟 Code::Blocks IDE,執行 Settings > Compiler... 項目,切換到 Search directories 也面分別設定 Compiler 與 Linker 的路徑即可,設定畫面如下:
  • 設定 Compiler 搜尋路徑

  • 設定 Linker 搜尋路徑


設定好後,可以透過下列的範例程式來驗證是否設定正確:

#include <iostream>
using std::cout;
using std::endl;

#include  <boost\shared_ptr.hpp>
using boost::shared_ptr;

int main()
{
    shared_ptr<int>sp(new int(5));
    cout << *sp << endl;
    return 0;
}