1

I have a working C IDE without Semantic (too slow for me), from instructions here, with completion provided by company-gtags (I think. Maybe clang does something too?).

Completion at point works for functions throughout the project, but not for variable names. I.e., if param_n is a global variable, I get no completions for param, even when I force completions. I expect that gtags knows about param_n, because it shows all of the references to it when I call global -sx param_n from the command line.

How do I get completion, without Semantic, for local and global variables, whenever they are in scope?

2
  • 1
    The "whenever they are in scope" part will be hard with ggtags. You'll have to use one of the smarter backends, like irony or ctags, instead.
    – Dmitry
    Commented May 24, 2016 at 19:58
  • Sorry, I meant rtags, not ctags.
    – Dmitry
    Commented May 24, 2016 at 22:58

1 Answer 1

1

I have followed the directions at this blog which supplements the C ide stuff by Tudho to install rtags and irony (as suggested by Dmitry) and it provides the features I asked for.

I'll expand with a summary of steps I had to take to get it all working on a Mac:

  • Install a recent version of the clang compiler with macports (likely optional)
  • Set environment variable CC=clang so that the most basic cmake files will call clang
  • make and install rtags v2.3 from rtags the repository. Detailed instructions and quick start guide worked but not with Apple XCode's clang (which lacked a link to libclang). follow instructions at the repository to check it works. Not clear to me whether you need to install rtags package in emacs from melpa, but I did that too.
  • write a very basic cmake file. If you are a cmake newbie like me you might have a simple C project with no libraries, with a root directory structured like this:

~~~~

% tree . 
└── src
    ├── bar
    │   ├── bar.c
    │   └── bar.h
    └── foo
        ├── foo.c
        └── foo.h

In this case, perhaps the following cmake file will work for you. Call it CMakeLists.txt and put it in the top directory.

cmake_minimum_required(VERSION 3.5)
project(myProjectName)
file(GLOB_RECURSE SRC_FILES src/*.c)
add_executable(myProjectName ${SRC_FILES})

Put build commands for the project in the directory ./build by calling

cmake -H. -Bbuild

Then export the compile command to a file that rtags will use

cmake build -DCMAKE_EXPORT_COMPILE_COMMANDS=1

And build the project

cmake --build build

Now, to make sure things are working cleanly, clear projects and quit the rdm daemon if it was running with

rc -C
rc -q

and restart the daemon

rdm &

and read that .json file you exported from cmake

rc -J build

You should see output like this:

% rc -J build                                                                                                                                                         Cprogrammingglanguage/wtf (hcn ⚡) alejandro
[Server] Compilation database loading...
 % [  3%] 1/35 23:40:23 ~/Google Drive/Work Cloud/... in 211ms. (549 syms, 780 symNames, 28 includes, 17 of 22 files, symbols: 792 of 1445, 1840 cursors, 21 queried 86ms) (37/111/49ms). (priority 0)
[  6%] 2/35 23:40:24 ~/Google Drive/Work Cloud/.../code/main/src/inrflow/reporting.c in 226ms. (1354 syms, 521 symNames, 15 includes, 6 of 87 files, symbols: 1714 of 4676, 7220 cursors, 5 queried 2ms) (119/61/25ms). (priority 0)

Note the following pitfall: can't have spaces in the path to the project. I worked around this by symlinking to the project folder from a space-less path.

Now you are finally able to start following the rest of the instructions in 1 about configuring rtags, company-rtags, rtags + helm, irony-mode, company-irony, company-irony-c-headers, and flycheck-rtags, and cmake-ide, which mostly follows the documentation for each package.

I tend to fumble through these things and would like to help other users, so constructive comments are welcome.

3
  • Nice write-up! This seems to be a good cause for a bug report, however: "can't have spaces in the path to the project".
    – Dmitry
    Commented May 30, 2016 at 21:29
  • Clang for completion is horrible thing.
    – dev1223
    Commented Dec 19, 2017 at 18:48
  • It depends on the use case: I work on +10 year old GNU/Linux so most of the modern stuff is hard to install and maintain. While the OS is old, the hardware is very powerful, so using clang is not bad at all for me Commented Mar 20, 2018 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.