天天看點

[轉] ubuntu安裝Sublime Text 3并使用SublimeClang插件

原文位址:https://blog.csdn.net/cywosp/article/details/32721011

3. 安裝強大的SublimeClang插件

    SublimeClang是Sublime Text中唯一的C/C++自動補全插件,功能強大,自帶文法檢查功能,不過最近作者已經停止更新了,目前隻能在Sublime Text 2的Package Control中可以找到并自動安裝,在SublimeText 3中隻能手動通過源碼安裝,其代碼線在https://github.com/quarnster/SublimeClang中。具體安裝步驟如下:

安裝相關軟體

    sudo apt-get install cmake build-essential clang git

    cd ~/.config/sublime-text-3/Packages

    git clone --recursive https://github.com/quarnster/SublimeClang SublimeClang

    cd SublimeClang

    cp /usr/lib/x86_64-linux-gnu/libclang-3.4.so.1 internals/libclang.so      #這一步很重要,如果你的clang庫不是3.4版本的話,請将對應版本的庫拷貝到internals中

    cd src

    mkdir build

    cd build

    cmake ..

    make

一切成功的話将會在SublimeClang/internals目錄中生成libcache.so庫檔案。重新開機Sublime Text,然後按快捷鍵Ctrl + `(Esc下面那個鍵)打開自帶的控制輸出,看看有沒有錯誤,如果沒有錯誤就說明一切OK了。接下來就是配置自己的檔案了,按下ctrl + shift + p快捷鍵,在彈出的輸入框中輸入 sublimeclang settings ,然後選擇帶User那一行,在打開的檔案中輸入如下資訊:

{

    "show_output_panel": false,

    "dont_prepend_clang_includes": true,

    "inhibit_sublime_completions": false,

    "options":

    [

        "-std=gnu++11",

        "-isystem", "/usr/include",

        "-isystem", "/usr/include/c++/*",

        "-isystem", "/usr/include/c++/4.8",

        "-isystem", "/usr/include/c++/4.8/*",

        "-isystem", "/usr/include/boost",

        "-isystem", "/usr/include/boost/**",

        "-isystem", "/usr/lib/gcc/x86_64-linux-gnu/4.8/include",

        "-isystem", "/usr/lib/gcc/x86_64-linux-gnu/4.8/include/*"

    ]

}

注釋:我的gcc版本為4.8,如果你的不是請替換對應的版本,在#include相應的頭檔案後儲存目前檔案,在接下來的操作中将更快的提示所包含在頭檔案的函數或者變量。

  注意:在 SublimeClang.sublime-settings 中新增或更改路徑後,需要重新打開檔案才可以生效,否則已打開的檔案可能因為緩存原因使用的還是原來的配置。

------------------------------------------------------2018-09-23更新------------------------------------------------------

  我在新裝的 ubuntu18.04 系統上進行到最後的 make 編譯步驟時出錯,報錯的資訊如下:

[email protected]:~/.config/sublime-text-3/Packages/SublimeClang/src/build$ make
Scanning dependencies of target cache
[ 50%] Building CXX object CMakeFiles/cache.dir/main.cpp.o
/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:218:39: error: template argument 1 is invalid
 typedef std::vector<shared_ptr<Entry> > EntryList;
                                       ^
/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:218:39: error: template argument 2 is invalid
/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp: In function ‘void trim(EntryList&)’:
/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:601:25: error: qualified-id in declaration before ‘i’
     EntryList::iterator i = mEntries.begin();
                         ^
/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:603:12: error: ‘i’ was not declared in this scope
     while (i != mEntries.end() && (*i)->display[0] == '\t')
            ^
/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:603:26: error: request for member ‘end’ in ‘mEntries’, which is of non-class type ‘EntryList {aka int}’
     while (i != mEntries.end() && (*i)->display[0] == '\t')
                          ^~~
/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:605:18: error: request for member ‘erase’ in ‘mEntries’, which is of non-class type ‘EntryList {aka int}’
         mEntries.erase(i);
                  ^~~~~
/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:606:22: error: request for member ‘begin’ in ‘mEntries’, which is of non-class type ‘EntryList {aka int}’
         i = mEntries.begin();
                      ^~~~~
/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:609:18: error: request for member ‘begin’ in ‘mEntries’, which is of non-class type ‘EntryList {aka int}’
     if (mEntries.begin() == mEntries.end())
                  ^~~~~      

  一開始感到很奇怪,在網上搜了好久沒發現有類似的錯誤(畢竟這個插件本來就很少人用,更别提更少見的編譯錯誤了),插件使用的是 cmake 的編譯方式,我對 cmake 不太熟悉,但看報錯資訊大概知道是在編譯階段出錯的(是以排除了缺少xx庫的原因),可是上面沒有顯示使用的是哪個編譯器,是以也不好手動模拟編譯。于是我隻好快速了解一下 cmake 的機制,然後通過 “Building CXX object CMakeFiles....” 這關鍵的一句在 cmake 生成的檔案 build.make 裡找到了一點線索(grep 即可):

@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Building CXX object CMakeFiles/cache.dir/main.cpp.o"
/usr/bin/c++   $(CXX_DEFINES) $(CXX_FLAGS) -o CMakeFiles/cache.dir/main.cpp.o -c /root/softwares/SublimeClang/src/main.cpp      

  這裡清晰地看到了使用的是 /usr/bin/c++ 編譯器(後來知道了上面的編譯錯誤和編譯器無關),于是我手動編譯模拟該錯誤,确實原樣重制了,無論是用 c++ 或者 g++ 都一樣,是以有點懷疑是不是因為 18.04 自帶的 c++/g++ 版本比較高,或者是系統檔案/庫有沖突的原因。然後我仔細分析了該編譯錯誤,發現是在模闆類定義處

/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:218:39: error: template argument 1 is invalid
 typedef std::vector<shared_ptr<Entry> > EntryList;
                                       ^      

開始出錯的,後面的一大堆錯誤都是因為這裡出錯導緻 EntryList 未定義而引起的,是以隻需處理這一句即可。一開始我懷疑是 shared_ptr 缺少了 std:: 的聲明,便在該行的前面加上 using namespace std;,重新編譯後後還是報同樣的錯誤,然後改成 using std::shared_ptr;,重試還是不行,于是把所有使用到 shared_ptr 的地方都改成 std::shared_ptr,再 make,這次終于成功了,成功編譯出 libcache.so 了,真夠折騰的。感覺是 c++ 命名空間的問題,shared_ptr 在不同的檔案裡定義了兩次,導緻如果不使用 std:: 來引用的話會有問題(using std::shared_ptr 也不行,記得 c++ primer 裡有提及到兩者的差别),後來重制這個 bug 時發現确實是這樣:

/home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:1339:17: error: reference to ‘shared_ptr’ is ambiguous
                 shared_ptr<Entry> entry(new Entry(tmp, representation, insertion));
                 ^~~~~~~~~~
In file included from /usr/include/c++/7/tr1/memory:50:0,
                 from /home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:49:
/usr/include/c++/7/tr1/shared_ptr.h:501:11: note: candidates are: template<class _Tp> class std::tr1::shared_ptr
     class shared_ptr;
           ^~~~~~~~~~
In file included from /usr/include/c++/7/bits/shared_ptr.h:52:0,
                 from /usr/include/c++/7/memory:81,
                 from /home/newdawn/.config/sublime-text-3/Packages/SublimeClang/src/main.cpp:31:
/usr/include/c++/7/bits/shared_ptr_base.h:347:11: note:                 template<class _Tp> class std::shared_ptr
     class shared_ptr;
           ^~~~~~~~~~      

  可以看到在 /usr/include/c++/7/tr1/shared_ptr.h:501:11 和 /usr/include/c++/7/bits/shared_ptr_base.h:347:11 處都有 shared_ptr 的定義,應該是 ubuntu18.04 自帶的 g++7 的系統庫和低版本 g++ 的系統庫略有不同,導緻 SublimeClang 在低版本的機器上能順利編譯成功,而在這裡編譯出錯。

轉載于:https://www.cnblogs.com/Newdawn/p/9539022.html