Skip to content

Instantly share code, notes, and snippets.

@zarzen
Last active January 23, 2024 13:06
Show Gist options
  • Star 68 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save zarzen/6218683614c9a645ad5b to your computer and use it in GitHub Desktop.
Save zarzen/6218683614c9a645ad5b to your computer and use it in GitHub Desktop.
C++ development environment setup in Spacemacs

Setup Cpp/C Development Environment in Spacemacs

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

1. Add c/c++ layer

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)
  ...
  )
...

Notes

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

2. Makefile and Source code in the same directory

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

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

  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

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

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

Compile source files with -g flag

So the target executable file will contain debug information.

SPC : -> gdb invoke gdb inside Spacemacs

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

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

@Javyre
Copy link

Javyre commented Jun 28, 2016

Thank you so much!
This is very helpful and straight forward

@abel-abel
Copy link

Thanks!

@babetoduarte
Copy link

Thanks, this is awesome!

@pbesedm
Copy link

pbesedm commented Jan 31, 2018

Thanks!

@Horki
Copy link

Horki commented Oct 12, 2018

Is there a option for executing(that excludes terminal in emacs), something like execute in Rust layer?

@NovaSurfer
Copy link

NovaSurfer commented Feb 13, 2019

Hello, I have some problem with directory local variables. I want to configure spacemacs for C/C++ projects.
I'm following tutorials from C/C++ in Spacemacs and C++ dev env setup. Both of this tutorials suggests to
create file in project's root directory .dir-locals.el with following code:

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

And to permanenly mark these value as safe I've added:

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

to my ~/.spacemacs config inside dotspacemacs/user-config

My test project directory now looks as following:

project
│ .dir-locals.el
│ main.cpp
└───build
│   │   Makefile
│   │   cmake_install.cmake
│   │   CMakeCache.txt
│   └───CMakeFiles

But, when I'm try to compile (SPC-c-c) I've an error: Wrong type argument: arrayp, nil.
I'm running on:

  • Manjaro Linux 18.0.2 (Kernel: 4.19.20-1)
  • GNU Emacs 26.1
  • Spacemacs 0.200.13@26.1

@ctrlPain
Copy link

I have the same problem @NovaSurfer had.
Also wen I configure it like shown in the video by Elvin Fonn https://www.youtube.com/watch?v=OjbkCEkboA8&t=262s

Spacemacs 0.300.0@26.2

@NovaSurfer
Copy link

NovaSurfer commented Jun 15, 2019

@ctrlPain, try to create git repository first, inside this folder

@ctrlPain
Copy link

ctrlPain commented Jun 15, 2019

Thanks dude, that solved it.

@ABellpain
Copy link

Hello, I'm new to spacemacs and I don't understand how to us the cc_args.py file that was described in the video. Can anyone give me a step by step explanation or guide on how to install/download the file and how to use it ? I'm sorry if it is too much but I really want to use spacemacs because it looks fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment