Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: C/C++ Development Environment for Emacs (tuhdo.github.io)
146 points by tuhdo on Aug 24, 2014 | hide | past | web | favorite | 40 comments



I added a demo on how CEDET works with Boost: http://tuhdo.github.io/static/c-ide/semantic-boost-demo.gif

Here you also have an outline tree the Emacs way, which is very efficient to use when a file contains lots of function/variable definitions: http://tuhdo.github.io/static/part3/helm-semantic-or-imenu.g...


If anyone follows this and happens to like GNU Global/ggtags (I did), there's a Pygments plugin released earlier this year that enables it to use any language that can be parsed by Pygments: https://github.com/yoshizow/global-pygments-plugin

For Arch Linux users, I made a PKGBUILD for it: https://aur.archlinux.org/packages/global-pygments-plugin-gi...

The Pygments plugin is really awesome; it should prove useful even for those who don't use Emacs. For example, gtags lets you use vim/Doxygen/bash/less/etc. to access the tags as well.


While it's good to see the work you put into this article, for the emacs novice it really is far too much of an information dump, in particular it's a little difficult to keep track of all the different things which do the same thing.

Perhaps if you change it in future, think about having a 'basic config' which one can follow?

It would also be nice to know how well these things cope with things like templates and C++11 rvalue references and lambda functions. My past experience is that tags-based tools are fine when your C++ code is basically just C code, but die horribly as soon as you start using C++11 or boost.


I think it's pretty basic. I created a demo repo for people to play with, so they don't have to configure themselves: https://github.com/tuhdo/emacs-c-ide-demo

For C++, CEDET works pretty nicely with templates and libraries like Boost. I haven't used much C++ aside playing with lambda, so I'm not sure. I've looked at rvalue reference, and it seems to return an assignable pointer to do other thing from a function. Currently, you can jump to the function but not the pointer that the function returns.

Currently, I only work with C. But being able to jump anywhere in the Linux kernel source tree and your system include source (such as "/usr/include", "/usr/local/include" is already very useful, don't you agree?


I've looked at rvalue reference, and it seems to return an assignable pointer to do other thing from a function

I'm not sure how you came to that conclusion, but it is not correct. R-values don't have much to do with pointers at all. Though they sometimes do solve problems you would be tempted to use pointers for. I admit I do find it hard to explain briefly what they are though. Maybe these are helpful, though it might be hard to grasp if you don't already properly understand C++: http://www.artima.com/cppsource/rvalue.html http://thbecker.net/articles/rvalue_references/section_01.ht.... This example basically gives an idea of why they are useful: http://thisthread.blogspot.be/2011/03/what-is-rvalue-referen...


Well currently I only work with C not C++ anymore so my knowledge might be outdated. I thought that the rvalue reference is something new in C++11, as I read it here: http://thbecker.net/articles/rvalue_references/section_01.ht.... Your article seems to mention different thing from 2008? In the article I read, a function returns a pointer that you can assign it like this: get_pointer() = 1;


The design of C++11 started way before 2011, hence articles from 2008.

  get_pointer() = 1;
assigning 1 to a pointer isn't the best idea :P so I think you mean you read

  int& get_reference();
  get_reference() = 1;
which is about lvalue references, not rvalue references, and has existed in C++ since the beginning.


Thanks for the explanation.


I agree: this is not C/C++ IDE, this is a C IDE (and maybe a "C-with-classes" IDE).

None of these emacs packages (e.g. CEDET) handle modern C++ well (heavy use of generic programming, boost, c++-11 features, etc.). Even indenting this code is complicated for an editor!

I think the only way to do it is to rely on the compilers to parse the code. Clang has a nice API (and command lines tools) for this. For instance, there are some packages for auto-completition in emacs: http://www.emacswiki.org/emacs/AutoComplete

Last, it would be nice to have a brew/ubuntu package so we can easily install the "IDE" :)


AutoComplete has nothing to do with C/C++. It's just a package for displaying completion candidates, but you must give it a source for displaying. It is pretty outdated compare to company-complete. Company also has a Clang[ backend, available through the command company-clang.

Emacs also has Clang based solutions such as rtags: https://github.com/Andersbakken/rtags that can index code as you type but is complicated to setup. Or you can use Clang to generate a tag database for your source code with clang-ctags: https://github.com/drothlis/clang-ctags , but the performance for creating tag database is not so good for large source tree: it took "98 minutes and a peak memory usage of 140MB" to generate a tag database for entire LLVM source.

As for CEDET, in what way it could not handle library like Boost? Could you be specific? I tried it before with Boost, and it works pretty well (i.e. gives correct completion candidates in a namespace). I used CEDET for getting completion candidates all the time in Boost.


Regarding the C++ parser, here's the current state: It does not support any C++11. Especially 'auto' is problematic, since type inference is quite a hard problem.

Basic templates are handled, but things like template specialization or default template parameters still break things. I'm currently working on fixing this; it's been in my personal branch for quite some time now, but I haven't got around to merge it into mainline.

Actually, still one of the most problematic things is preprocessor handling. If the parser doesn't work, it is often due to some preprocessor macros Semantic does not know about.


Thanks for the info. You must be David Engster, the current CEDET maintainer.

Yes, I agree with the preprocessor handling since we still have manually tell Semantic what preprocessor exists in the current project. But CEDET is still very usable for most of the things and I love it.


The name is correct, but I'm not CEDET's maintainer; that would still be Eric Ludlam, its original author.


It is not really linked to boost, but meta-programming make many things many difficult for an IDE (whatever the IDE is). For instance, I could have this class:

template<typename X> struct Test { void test(const X& x) { typename X::type_t x = x.get_something(); } };

When I type this, there is no way for the compiler to know what X will be (and even for the programmer). As a consequence, the IDE cannot help me much: no auto-completion is possible on x, no way to jump to the definition of get_something() [there are probably several implementations], no way to jump to the definition of type_t, etc.

The issue is that this kind of code is very common in modern C++ (I think 90% of my code is probably templated by something) [and look at the code of boost].

So, there is probably no issue in _using_ some boost libraries or the STL, but programming "boost-like" or "STL-like" libraries probably needs different tools.


there is always the clang-completion-mode which uses clang/llvm infratructure for more comprehensive emacs integration...


This actually convinced me to never use Emacs as C/C++ IDE ever. The amount of unnecessary complexity is astounding.


What's so complex? I wrote in the very beginning of the guide:

"In this guide, I will help you to setup an efficient working C/C++ environment. Despite looking long, the setup is short and easy (mostly copy/paste Emacs Lisp code into your init.el); most of the guide are explanations and demonstrations of many useful features. "

Most of the text are just explanations to help you understand what you are doing, why you need these features.

Or, you can have a simple recipe with 42 steps for setting up a working environment with Linux kernel in Ecliplse with barely any explanation, just learn by heart: http://wiki.eclipse.org/HowTo_use_the_CDT_to_navigate_Linux_... . The guide is merely for NAVIGATING the Linux kernel. Even with the first section of my guide with setting up ggtags or helm-gtags that you can do in 5 minutes, you can freely roam the linux kernel.


I can understand where he is coming from. At least in my case the question was - how come there is no package that does all this by default? Install the package and that's it... I want to be productive in my project before I get productive in init.el customization. Nice to have but it comes second. Anyway, I've bitten the bullet and after realizing that I'm not that smart to choose `the` packages for C dev myself, I decided (again) to see what others do with their setups.

Nice timing this time, because of these:

1) https://tuhdo.github.io/emacs-tutor.html

2) https://www.youtube.com/watch?v=HTUE03LnaXA

Since I want to use it for navigation more than for programming these two made my day because I've found about global and how to integrate it with emacs. The rest came as a bonus. Now I can go on with my work and not worry about customization until I have to.

I've been using Emacs for about eight years but never as a power user.

EDIT: Thanks for your work. This tutorial has some new ideas I want to try.


Are you a emacs user. i.e Apart from c++ development do you use emacs for any other editing?.


I'm an emacs user and I don't use any of that stuff, or really anything beyond c-mode (and c++-mode).


It's fine that you use the default. But when you seek more power, it's always available.


Nowadays I use Emacs only when I cannot use an IDE and VI for quick editing files, specially since it is the only proper editor in many UNIX systems.

Otherwise IDEs, since the MS-DOS/Amiga days.


I love emacs, and use it daily for lots of text-editing, but since I've been learning Qt, I've been using Qt Creator and seems much easier to get certain things (which granted might be available in emacs, or visual studio but haven't checked how).

For example adding function class member in the .h or .cpp file, and then adding the other part automatically. Or changing arguments, and little balloon comes up, you pres alt+enter - and gives you choices how to make the changes.

Then switch between header/cpp (right visual studio doesn't have this built-in) and few other things make Qt Creator a very good choice.


You can switch between .h and .cpp easily with ff-other-file. Bind it to a key:

(add-hook 'c-mode-common-hook (lambda() (local-set-key (kbd "C-c o") 'ff-find-other-file)))

If the .h is in the same directory as .c/.cpp, then Emacs automatically opens. Otherwise, you prompt for the .h file. Once the .h file is opened, subsequent execution of the command switches between the two files.


Qt Creator is way easy to code and debug than visual studio. My Advice is use Qt Creator for Qt development and avoid visual studio. Also Qt Creator is much faster than visual studio.


After a few years with PyCharm I'm not excited to manually write wads of elisp to get basic functionality. Instead I pay the JetBrains people a negligible sum per year (and the basic functionality is open sourced now). They are working on a C++ IDE now: http://www.jetbrains.com/objc/features/cpp.html

This is refactoring capability another commercial Emacs plugin had in 2007: http://www.xref.sk/xrefactory/emacs.html


I have similar thoughts here. I have been a tester for their new C++ IDE and previously used emacs. I haven't used emacs since starting the beta test program. It handles modern C++ (11) well but there are still some rough edges (support for all of boost & STL for instance) they are working on. Additionally, it has quick-fixes, code analysis, code generation, and awesome navigation out of the gate....no github repo of configuration required.

That being said, once they release this in production, I'll be buying it on day one and won't look back at emacs.


If you work on a Mac, Xcode is the best IDE for C++. I initially thought that it handled only objective-c, but was quite surprised by how efficient it is in handling C++ syntax.


I primarily use Eclipse CDT (C/C++ Development Tools) these days, because it's free and runs on Linux/Mac.

However, my favorite C++ IDE so far has been Visual Studio with the Visual Assist X plug-in. Visual Studio by itself is not that impressive. But the folks who built Visual Assist X, have done an impressive job. It adds a lot of features to Visual Studio that take it far beyond any IDE.


Because such refactoring is not really needed. You can do easily with sed.

What's so difficult about using Elisp to configure your editor? If you know how to turn on a light switch, you already know how to configure Emacs at its basic level!!! Most of Emacs lisp configuration is like this:

(semantic-mode 1) ;; 1 means turn the feature on

If you want to turn it off:

(semantic-mode 0) ;; 0 means turn the feature off

Most of Emacs plugins work this way, with a few more Elisp code for setting key bindings and other things. Anyway, the package maintainers always give you config Elisp code. Your only work is to copy/paste into your config file and it's done.

Your IDEs appears nice at first since they hide most of the complexity in a menu item called "Options". But when you need to adjust something, and when you click the "Options" menu, things are not so nice anymore. For example, try Eclipse.


If you do that kind of refactoring with sed, I won't believe you take refactoring seriously.


How do you refactor complex C code with myriad of macros? I think we should avoid writing code that having to seriously change its structure later. If we want to refactor, it should be minimal and manageable.

And if you mean seriously refactoring as in this article from Qt Creator: http://qt-project.org/doc/qtcreator-2.6/creator-editor-refac... that includes reformatting code, generating getter/setter or automatically detect and fix mistake for you, then no. I have never write such code in the first place that having to back later and I believe we should not fall into the use cases listed in that article.

My only use case for refactoring is to rename variable/function names.


Refactorings are used to adapt your code to newly acquired knowledge and/or changed requirements. That are things that CAN'T BE PLANNED because you simply don't know them yet. Therefore you can't plan how minimal you'll have to refactor.

Let's take the "rename function" refactoring. You claimed sed was sufficient. What about functions with the same name in different namespaces or different visibilities?

The same goes for move parameter. Sometimes you want to move a parameter so the order adheres to a guideline that you've accidentially forgotten or that newly emerged. Again: What about ambiguos function names?

Refactorings such as pull method up (e.g. to create an interface)/ push method down are probably not even possible with sed.

Those are small and manageable refactorings. But even a small refactoring such as rename function can mess up your code if not done correctly.


With sed, really?!?

I guess you never used refactoring tools. Good luck doing all their use cases with sed.


@tuhdo Are you using Emacs in terminal? Do you prefer it use the GUI version?


I am using both GUI and terminal. I prefer the GUI for my local use. But when working, I have to connect to remote server only through SSH under the terminal. Instead of using Tramp (which is not practical for the current latency), I setup Emacs on the remote servers and use it from there when I connect to the servers.


Do any ROR devs use emacs? Are there articles on good configurations for Ruby or Rails? I'm trying to get into emacs. I see the value, but it is REALLY overwhelming getting started.


You can use the popular Emacs Prelude: https://github.com/bbatsov/prelude . It's an Emacs setup that improves the original Emacs. The author is also a ROR developer, so he sure knows how to configure it properly.

To learn the basics of Emacs, start with my mini-manual: http://tuhdo.github.io/emacs-tutor.html



I use VIM, though :( .

- Jonathan




Registration is open for Startup School 2019. Classes start July 22nd.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: