Skip to content

Instantly share code, notes, and snippets.

@babetoduarte
Forked from zarzen/spacemacs-cpp.md
Created September 9, 2017 23:21
Show Gist options
  • Select an option

  • Save babetoduarte/e01fe5130e9cbf36fbc670b29da2c61b to your computer and use it in GitHub Desktop.

Select an option

Save babetoduarte/e01fe5130e9cbf36fbc670b29da2c61b to your computer and use it in GitHub Desktop.

Revisions

  1. @zarzen zarzen revised this gist Dec 30, 2016. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions spacemacs-cpp.md
    Original file line number Diff line number Diff line change
    @@ -123,6 +123,5 @@ So the target executable file will contain debug information.
    The GUD interface in Spacemacs has a problem, which cause the
    current line indicator disappeared.

    ## Todo<a id="orgheadline15"></a>

    ### Try `rtags` for better code navigation.<a id="orgheadline14"></a>
    ## Rtags for code navigation
    https://github.com/lujun9972/emacs-document/blob/master/emacs-common/%E5%9C%A8spacemacs%E4%B8%AD%E4%BD%BF%E7%94%A8rtags.org
  2. @zarzen zarzen revised this gist Mar 23, 2016. 1 changed file with 112 additions and 15 deletions.
    127 changes: 112 additions & 15 deletions spacemacs-cpp.md
    Original file line number Diff line number Diff line change
    @@ -1,31 +1,128 @@
    - [Setup Cpp/C Development Environment in Spacemacs](#sec-1)
    - [1. Add c/c++ layer](#sec-1-1)
    - [Notes](#sec-1-1-1)
    - [2. Makefile and Source code in same directory](#sec-1-2)
    <div id="table-of-contents">
    <h2>Table of Contents</h2>
    <div id="text-table-of-contents">
    <ul>
    <li><a href="#orgheadline16">1. Setup Cpp/C Development Environment in Spacemacs</a>
    <ul>
    <li><a href="#orgheadline2">1.1. 1. Add c/c++ layer</a>
    <ul>
    <li><a href="#orgheadline1">1.1.1. Notes</a></li>
    </ul>
    </li>
    <li><a href="#orgheadline3">1.2. 2. Makefile and Source code in the same directory</a></li>
    <li><a href="#orgheadline7">1.3. 3. Makefile in different directory</a>
    <ul>
    <li><a href="#orgheadline6">1.3.1. Notes</a></li>
    </ul>
    </li>
    <li><a href="#orgheadline10">1.4. 4. Clang Complete</a>
    <ul>
    <li><a href="#orgheadline9">1.4.1. Usage</a></li>
    </ul>
    </li>
    <li><a href="#orgheadline13">1.5. 5. Debug</a>
    <ul>
    <li><a href="#orgheadline11">1.5.1. Compile source files with <code>-g</code> flag</a></li>
    <li><a href="#orgheadline12">1.5.2. <code>SPC :</code> -&gt; <code>gdb</code> invoke gdb inside Spacemacs</a></li>
    </ul>
    </li>
    <li><a href="#orgheadline15">1.6. Todo</a>
    <ul>
    <li><a href="#orgheadline14">1.6.1. Try <code>rtags</code> for better code navigation.</a></li>
    </ul>
    </li>
    </ul>
    </li>
    </ul>
    </div>
    </div>

    # Setup Cpp/C Development Environment in Spacemacs<a id="orgheadline4"></a>
    # Setup Cpp/C Development Environment in Spacemacs<a id="orgheadline16"></a>

    Based on Eivind Fonn's tutorial [<https://www.youtube.com/watch?v=OjbkCEkboA8>]

    ## 1. Add c/c++ layer<a id="orgheadline2"></a>

    Simply add this `(c-c++ :variables c-c++-enable-clang-support t)`
    in `dotspacemacs-configuration-layers`, like this:

    ```
    ...
    dotspacemacs-configuration-layers
    '( ...
    (c-c++ :variables c-c++-enable-clang-support t)
    ...
    )
    ...
    ```
    ...
    dotspacemacs-configuration-layers
    '( ...
    (c-c++ :variables c-c++-enable-clang-support t)
    ...
    )
    ...

    ### Notes<a id="orgheadline1"></a>

    This configuration will enable `clang` support, so you have to
    guarantee `clang` has been installed.

    ## 2. Makefile and Source code in same directory<a id="orgheadline3"></a>
    ## 2. Makefile and Source code in the same directory<a id="orgheadline3"></a>

    This is the simplest situation, that you can open any source code file,
    then type `SPC c c` for compilation.
    `SPC c C` is for specifying compilation command, sometimes need this
    for enabling debug mode.

    ## 3. Makefile in different directory<a id="orgheadline7"></a>

    In this situation we need to specify the location of `Makefile`.
    `.dir-locals.el` file is needed for this purpose. `.dir-locals.el`
    should be placed in project root. The file content looks like this:

    ((c++-mode (helm-make-build-dir . "build/")))

    ### Notes<a id="orgheadline6"></a>

    1. The path `build/` is relative to project root directory.

    2. Emacs will ask if the variable `helm-make-build-dir` is safe.

    Put the configuration in `.spacemacs` to prevent this.

    (put 'helm-make-build-dir 'safe-local-variable 'stringp)

    ## 4. Clang Complete<a id="orgheadline10"></a>

    If the project depends on third party libraries or header files in special locations,
    `clang` will not figure out how to analyze source codes and provide auto-complete tips. We need
    the `.clang_complete` file to specify the compilation flags. That
    file can be generated by `cc_args.py`, which is a tool provided by
    `clang-complete` project. Otherwise, The simplest way to install `cc_args.py`, I
    think is move the file to `/usr/local/bin/`.

    ### Usage<a id="orgheadline9"></a>

    The video by Eivind Fonn demonstrates how to use `cc_args.py` with
    `cmake`. [28:00 - 29:40].

    CXX="cc_args.py g++" cmake ...#other flags

    I find the codes below can work with `Makefile` and `make` system directly, but
    it will invoke the compilation process. And the compilation process
    always been interrupted by errors, though it can generate the
    `.clang_complete` file. This may cause incomplete compilation flags
    in `.clang_complete`.

    CXX="cc_args.py g++" make all

    1. Turn off Warnings about unused variables

    Add `-Wno-unused-parameter` to `.clang_complete` file.

    ## 5. Debug<a id="orgheadline13"></a>

    ### Compile source files with `-g` flag<a id="orgheadline11"></a>

    So the target executable file will contain debug information.

    ### `SPC :` -> `gdb` invoke gdb inside Spacemacs<a id="orgheadline12"></a>

    The GUD interface in Spacemacs has a problem, which cause the
    current line indicator disappeared.

    ## Todo<a id="orgheadline15"></a>

    ### Try `rtags` for better code navigation.<a id="orgheadline14"></a>
  3. @zarzen zarzen revised this gist Mar 22, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion spacemacs-cpp.md
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ in `dotspacemacs-configuration-layers`, like this:
    dotspacemacs-configuration-layers
    '( ...
    (c-c++ :variables c-c++-enable-clang-support t)
    &#x2026;
    ...
    )
    ...
    ```
  4. @zarzen zarzen revised this gist Mar 22, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions spacemacs-cpp.md
    Original file line number Diff line number Diff line change
    @@ -11,13 +11,13 @@ Simply add this `(c-c++ :variables c-c++-enable-clang-support t)`
    in `dotspacemacs-configuration-layers`, like this:

    ```
    &#x2026;
    ...
    dotspacemacs-configuration-layers
    '( &#x2026;
    '( ...
    (c-c++ :variables c-c++-enable-clang-support t)
    &#x2026;
    )
    &#x2026;
    ...
    ```

    ### Notes<a id="orgheadline1"></a>
  5. @zarzen zarzen revised this gist Mar 22, 2016. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions spacemacs-cpp.md
    Original file line number Diff line number Diff line change
    @@ -10,16 +10,15 @@
    Simply add this `(c-c++ :variables c-c++-enable-clang-support t)`
    in `dotspacemacs-configuration-layers`, like this:

    <div class="elisp">
    ```
    &#x2026;
    dotspacemacs-configuration-layers
    '( &#x2026;
    (c-c++ :variables c-c++-enable-clang-support t)
    &#x2026;
    )
    &#x2026;

    </div>
    ```

    ### Notes<a id="orgheadline1"></a>

  6. @zarzen zarzen renamed this gist Mar 22, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. @zarzen zarzen revised this gist Mar 22, 2016. 1 changed file with 29 additions and 13 deletions.
    42 changes: 29 additions & 13 deletions spacemacs-cpp.org
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,32 @@
    * Setup Cpp/C Development Environment in Spacemacs
    ** 1. Add c/c++ layer
    Simply add this ~(c-c++ :variables c-c++-enable-clang-support t)~
    in ~dotspacemacs-configuration-layers~, like this:
    #+begin_elisp lisp
    ...
    - [Setup Cpp/C Development Environment in Spacemacs](#sec-1)
    - [1. Add c/c++ layer](#sec-1-1)
    - [Notes](#sec-1-1-1)
    - [2. Makefile and Source code in same directory](#sec-1-2)

    # Setup Cpp/C Development Environment in Spacemacs<a id="orgheadline4"></a>

    ## 1. Add c/c++ layer<a id="orgheadline2"></a>

    Simply add this `(c-c++ :variables c-c++-enable-clang-support t)`
    in `dotspacemacs-configuration-layers`, like this:

    <div class="elisp">
    &#x2026;
    dotspacemacs-configuration-layers
    '( ...
    '( &#x2026;
    (c-c++ :variables c-c++-enable-clang-support t)
    ...
    &#x2026;
    )
    ...
    #+end_elisp
    *** Notes
    This configuration will enable ~clang~ support, so you have to
    guarantee ~clang~ has been installed.
    &#x2026;

    </div>

    ### Notes<a id="orgheadline1"></a>

    This configuration will enable `clang` support, so you have to
    guarantee `clang` has been installed.

    ## 2. Makefile and Source code in same directory<a id="orgheadline3"></a>

    This is the simplest situation, that you can open any source code file,
    then type `SPC c c` for compilation.
  8. @zarzen zarzen created this gist Mar 22, 2016.
    16 changes: 16 additions & 0 deletions spacemacs-cpp.org
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    * Setup Cpp/C Development Environment in Spacemacs
    ** 1. Add c/c++ layer
    Simply add this ~(c-c++ :variables c-c++-enable-clang-support t)~
    in ~dotspacemacs-configuration-layers~, like this:
    #+begin_elisp lisp
    ...
    dotspacemacs-configuration-layers
    '( ...
    (c-c++ :variables c-c++-enable-clang-support t)
    ...
    )
    ...
    #+end_elisp
    *** Notes
    This configuration will enable ~clang~ support, so you have to
    guarantee ~clang~ has been installed.