Vim + YouCompleteMe + C: a minimal .ycm_extra_conf.py?

18,079

Solution 1

I was searching for this too and seems here we haven't get a good solution. Even this is a very old question I hope this might help someone. The following works for me,

import os
import ycm_core

flags = [
  '-Wall',
  '-Wextra',
  '-Werror',
  '-Wno-long-long',
  '-Wno-variadic-macros',
  '-fexceptions',
  '-ferror-limit=10000',
  '-DNDEBUG',
  '-std=c99',
  '-xc',
  '-isystem/usr/include/',
  ]

SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', ]

def FlagsForFile( filename, **kwargs ):
  return {
  'flags': flags,
  'do_cache': True
  }

By the way, that long config file by the default bothers me so much. I should give credit for this post, http://cocoaspice.logdown.com/posts/302432-youcompleteme-toss-notes

Solution 2

Nothing at all is perfectly valid as long as the sources can be compiled by simply clang++ -c source (c vs. c++ is decided from extension). YCM happily completes in scratch tests created in random directories for me.

The -x c is not needed. If the source has extension .c or .h, it is assumed to be C and if it has extension .C, .cc, .cpp, .cxx, .H, .hh, .hpp or .hxx it is assumed C++. Only if you have C++ headers with just .h you need -x c++-header.

Newest clang (4.9) already defaults to c11 and c++11, so you shouldn't need those either.

So you only need any -I flags and the warnings are useful.

Solution 3

To give you a working example, here is the configuration I'm using for Arduino projects.

https://github.com/WeAreLeka/Bare-Arduino-Project/blob/master/.ycm_extra_conf.py

In the flags I've put all the Arduino libraries provided by the IDE and needed to compile my code.

I've also written a little function to find the other libraries in my /lib dir that I'm using in my project and to add them automatically to flags. It's line 57.

It's helpful if you use a lot of libs and don't want to modify your conf file each time.

Without the -I /path/to/lib/folder you won't get autocompletion.

Hope this helps :)

Share:
18,079
kekkonen
Author by

kekkonen

Updated on June 05, 2022

Comments

  • kekkonen
    kekkonen about 2 years

    I've already got Vim with YouCompleteMe plugin (compiled with semantic support for C-family languages), which I use for Python etc. Now I'd like to try it with C (I've never developed with C before, so I've got a slightly fuzzy idea about some details, like the necessary flags.)

    To use YCM's semantic completion features with C, I need to provide it a .ycm_extra_conf.py file; the YCM user guide points to YCM's own .ycm_extra_conf.py as a reference (link).

    Would the following (based on the aforesaid .ycm_extra_conf.py) produce "a minimal working setup" for C (to which I could then point g:ycm_global_ycm_extra_conf):

    The flags:

    flags = [
    '-Wall', '-Wextra', '-Werror',
    '-std=c11',
    '-x', 'c'
    ]
    

    and FlagsForFile function without the final_flags.remove( '-stdlib=libc++' ) line.

    Otherwise the example file would remain as-it-is. I believe that -isystem flags are strictly YCM-related, is that correct?

  • Vanilla Godzilla
    Vanilla Godzilla over 6 years
    If you're using c++, add a 'c++' flag instead of 'c'. Otherwise it will tell you "you can't use throw with exceptions disabled" and other weird errors.