Open declaration feature in VI - c++

I'm a vi user for coding in Python. I love it: powerful, lowlevel, minimal gui without useless buttons, etc.
Now I'm approaching C++ with an opensource project that—as usual happened in opensource project—has poor documentation and sometimes (well, a lot of times) I have to open declaration and implementation and see what a particular function does.
In Eclipse this is really easy to do due to the Open Declaration (shortcut F3) feature. But I don't like the CDT plugin for Eclipse. I have a makefile project and it indicates some error in importing external .cpp files inside this opensource framework that requires a custom structure for directory and build path (and it's annoying to create a project every time in Eclipse for this custom structure).
I wish to have the same feature in vi. Do you know of anything?

Vim doesn't offer a feature like that by itself. A rudimentary way would be to use gd in command mode. If you want more sophisticated support, take a look at ctags and cscope. Those tools offer which you ask for and integrate nicely into Vim. A quick google search brought me to this link, which explains how to use cscope within Vim. Using ctags quite as easy: generate the ctags database/tags file and use Ctrl+] on an identifier to jump to its declaration. See :help ctags for more information.

One option: Use QT Creator with FakeVim mode enabled. You can follow declarations with F2.

Related

cmake project include dirs detection in vim editor [duplicate]

I have a cmake project. I want to do the following easily
search the declaration, definition and references of any variable, function, etc. under the cursor, which may be declared in an external header file whose path is added using INCLUDE_DIRECTORIES in CMakeLists.txt
rename a variable, function, etc. that is declared in the project
How can I set this up?
You can try to use vim plugin cmake4vim in order to integrate CMake to Vim.
This plugin helps to work with cmake targets and allows to generate compilation database file (compile_commands.json). A lot of plugins use this file for code completion, jump to definition and etc. (for example YCM)
Also you can use vim lsp plugins (for example vim-lsp) these plugins use language servers for code completion, refactoring and another good features.
But CMake project integration (cmake cache generation, project compilation, etc.) and search the declaration, definition and etc are different tasks. And different plugins and tools solve these tasks.
You can tell Vim where to look for includes by adding entries to the path option. I don't have enough experience with Cmake to know how to pull paths from CMakeLists.txt, though.
See :help 'path'.
Assuming a properly set path, it is possible to use the built-in :dsearch and related commands to search for definitions across includes.
The define option has a prescriptive name but it could be used to find any specific pattern so you could alter it to match declarations, too, or really anything.
See :help include-search and :help 'define'.
Vim has no built-in concept of "reference". :isearch and friends should work for that but they will probably be too noisy.
Renaming is usually done with something like:
:grep foo paths
:cwindow
:cdo s/foo/bar/gc
YouCompleteMe will help you. It uses compilation_database.json, witch can be generated by cmake.
This plugin also provides autocompetion for many languages.
I use functions in vim and assign them to a hotkey.
https://developer.ibm.com/tutorials/l-vim-script-2/
it gives you more an IDE feel. but at the end of the day you get a bit more control.

Considering Autocompletion. How to tell Geany in which directories to look for header files?

I am coming from KDE to XFCE and hence arrive from KDevelop at Geany. I have no plans of using Geany for any compilation stuff, prefering to write my own CMakeLists.txt files. However, what I loved about KDevelop and dearly would have again:
In KDevelop I could "attach" an include-directory to a c++ file meaning that code autocompletion would look for #included headers within that directory and use its content.
For example it was possible to "attach" something like /usr/share/myIncludes/
containing "my_foo.h". Then in the source code of my program I would
#include "my_foo.h"
and henceforth auto-completion would kick in using the contents of "my_foo.h".
I find this surprisingly hard to google. Is there even a feature like that in Geany, and if so, how would one use it?
My Geany is a simple install (geany and geany-common) on a clean (no non-free stuff) Debian System.
Geany is not supporting dynamic parsing of header files due to the performances and resources impact it would have.
However, you can generate a tag file from your headers as described inside manual or maybe use a plugin like geanyctags or projectorganizer (recently rename from gproject) which could help you there
Also you can find tag-files inside the wiki which you can import via the Tools-menu.

How to configure Vim for C++ development?

I'm learning C++ using Vim as an editor on Windows XP, however I found a issue that I have listed below.
I have downloaded and installed c.vim and it is a essential file, however when I start vim it shows the message C/C++ template file 'C:\Program Files\Vim\vimfiles\c-support/templates/Templates' does not exist or is not readable, How do i fix this problem?
How would i make vim compile a C++ STL file?
For your first problem: I suspect that you didn't extract all the files in the archive (that c.vim came in). The c.vim documentation (README.csupport) says:
The subdirectories in the zip archive
cvim.zip mirror the directory
structure which is needed below the
local installation directory
$HOME/.vim/ for LINUX/UNIX
($VIM/vimfiles/ for Windows)
This means that you need to uncompress the entire archive as it is into your vimfiles directory.
There are some other steps to follow, detailed in the documentation.
As for your second issue: you need a Makefile to do that. If you have never done this before, I suggest using cmake to generate a Makefile. You will also need GNU tools for Windows; Cygwin or MinGW are the most popular choices. I haven't use them, it is easier to do all this on some *nix OS :).
When done, use :cd (if you are not in your working directory), and :make. Use :cl to list the compiler output, :cn to jump to the next error. There are some other useful commands for compiling. You might find these resources useful:
StackOverflow: Recommended plugins for C coding
Compiling from Vim
C editing with Vim
Also, I found the Nerd Commenter a very useful companion.
I found that Vim acts somewhat like alien on Windows; it is designed for an *nix-like operating system. I think it is possible to craft a similar environment for it, and use it mostly successfully, but it is so much easier to do on some linux, as it is "instantly home" there.
Anyway, if you wish to stick with Windows, I think you can find a way to accomplish what you want. Good luck.
Download Vim and install to your computer.
Download c.vim and extract to $Vim\vimfiles\
Download MinGW and install to your computer, make sure that you check C++ Compiler at Select Components.
Add C:\MinGW to system Path variable
Edit _vimrc file, add set makeprg=mingw32-make after line behave mswin
Test your Vim with hello world, use !g++ c:\full_path\filename.cpp -o c:\full_path\output.exe.

Educational IDE to start programming in C++?

I'am aware there has been a generic question about a "best IDE in C++" but I would like to stress I'm a new to C++ and programming in general. This means I have the needs of a student:
relatively easy and unbloated working environment
things just work, focus on the code
color coding to show the different language features (comments, etc)
not too unfriendly (not a simple editor, something to handle projects from start to finish)
cross-platform so not to be bound with specific system practices
I think the above are relatively reasonable demands for an educational IDE, perhaps excluding the last as such universal tool might not exist. Any ideas?
It depends on which world are you coming from to learn C++.
Do you have previous Java experience? - Use Eclipse CDT.
Have used .NET previously? - Go with Visual Studio C++ Express Edition (and then throw it away if you really need multiplatform IDE, not just code).
Are you an Unix guy? Use just a syntax-highlighting editor + Makefile. When you want to learn basics of the C++, the project should not be complicated and it is well invested time to learn how the C++ compiler is called with preprocessor options, etc.
Code::Blocks is free and really easy to install and use. I always recommend it to my students.
I've heard good things about Code::Blocks. Might be a bit complex, but you can close any unneeded panes, and it's cross-platform.
I would recommend Komodo Edit.
It functions as a great text editor that I've used on Ubuntu, Windows(XP/7) and OSX. It's big brother is a full blown IDE but KE still allows for projects and some great extensions. It's also free and open source. I found it easy to get started quickly with it and as your skills grow, it has the ability to keep up.
Edit to add a link to ActiveState's community site for Komodo Extensions. If you decide to try out KE, I'd suggest the RemoteDrive Tree (ssh,ftp,scp remote editing) and Source Tree as a start.
If you are using both windows and linux (as your comment indicates), I'd recommend Qt Creator. Qt is cross platform so your apps will work on linux, windows, and mac. Qt has excellent documentation, too, so it's very newbie friendly. Signals and Slots take a bit of getting used to, but IMO it's worth it.
Until the last point I would have said Microsoft Visual C++ Express Edition, which is free and fits your first 4 criteria. Cross platform you'd be looking at something like emacs or vim, neither of which are particularly friendly. On Windows I actually use Notepad++ for small C++ programs as it has good syntax highlighting and a (limited) intellisense.
I'd recommend Eclipse CDT as it does good code completion and it builds code on the fly, so you can see your errors immediately which is very good for a language studying.
Assuming Linux/Unix like system ...
I've found out that it's much easier and beneficial to go the other way round. Try using 'simple' editor like vim and for C++ just Makefiles to compile using gcc and linker.
I've started using that at uni and 5> years and couple of companies later it's still the easiest and most flexible option because you have quick access to all settings in one simple file.
Even when you switch to IDE later on you will know what to look for if things don't work because you will know the basics for example what are the steps to go from source file to object file and link to binary executable, how to handle libraries and so on. These things change between IDE's and are often complicated to trace and modify.
You can start with simple makefile and keep improving it over years. It's easy to copy it to your project directory and update file names - for C++ the compilation process will be fairly standard between projects.
I highly encourage you to consider this option. I've learned a lot doing it that way and you have a backup plan when you IDE just wouldn't work.
I keep one generic Makefile that compiles main.cpp into executable. To compile something quickly I just copy it into directory and make.
My current workflow is to open all files in project directory (flat file system) with vim (vim *.cpp *.hpp), edit, compile with :mak (or :mak -C .. debug) from within vim to invoke the Makefile stored in relevant directory, after compiling it'll jump to first warning/error, use :cn to go over errors, fix what's needed, open errors in separate window with :cope (close with :clo or unload file with :bd, jump between split windows with ctrl-w ctrl-w or ctrl-ww - hold ctrl and press w twice) ...
Vim has syntax highlighting millions of other features, I'm using tags (or ctags) to navigate code from within vim and so on.
Personally, it's my opinion that all C++ IDEs suck. When I write C or C++, I tend to use some sort of powerful programmer's text editor along with command line compilation. If I'm just messing around and have a couple source files, I'll just invoke gcc -g -o myprog *.c on the command line myself. If I have a more involved project, I'll just write a simple makefile. You could also look into gmakemake if you don't want to bother learning makefile syntax just to compile your programs.
On the Mac side, I have always been a fan of both BBEdit and TextMate, but much more so of the latter, especially given its lesser price tag and more modern feel. Both have project organization features.
On Windows, I'd stick with either e (which is basically a port of TextMate to Windows) or Notepad++. The downside of Notepad++ is that it doesn't have any project organization features, whereas e does. You could also look at SciTE, but like Notepad++, it has no project org features.
As for Linux, I'm personally unsure. I'd stick with other people's answers covering that platform for recommendations there.

C++ development on linux - where do I start?

I decided to leave my windows install behind and am now running Debian as my default OS. I have always coded in Windows and specifically with Visual Studio. I am currently trying to get used to compiling my code under linux.
Although I still have a lot of documentation to read, and don't expect you guys to make it too easy for me, it'd still be nice to get some pointers on where to start. I have some specific questions, but feel free to suggest/recommend anything else regarding the subject.
What are recommended guides on creating a make file, how do I compile from this makefile (do I call g++ myself, do I use 'make'?)
Looking at other linux software, they almost always seem to have a 'configure' file. What exactly does it do? Does it only check if the required libraries are installed or does it more than just checking requirements?
How do I link libraries, and how does this relate to my makefile or g++ parameters? In windows I would compile the library, include some header files, tell my linker what additional lib file to link, and copy a dll file. How exactly does this process work in linux?
Recommendations for code editors? I am currently using nano and I've heard of vim and emacs, but don't know what the benefits of them are over eachother. Are there any others, and why would I consider them over any of the previous three? Note: I am not looking for an IDE.
Any help, links to guides & documentation (preferably those that are aimed at beginners) are very much appreciated!
What are recommended guides on creating a make file, how do I compile from this makefile (do I call g++ myself, do I use 'make'?)
You build from the makefile by invoking "make". And inside your makefile, you compile and link using g++ and ld.
Looking at other linux software, they almost always seem to have a 'configure' file. What exactly does it do? Does it only check if the required libraries are installed or does it more than just checking requirements?
It's a script usually used to set up various things based on the environment being used for building. Sometimes it's just a basic shell script, other times it invokes tools like Autoconf to discover what is available when building. The "configure" script is usually also a place for the user to specify various optional things to be built or excluded, like support for experimental features.
How do I link libraries, and how does this relate to my makefile or g++ parameters? In windows I would compile the library, include some header files, tell my linker what additional lib file to link, and copy a dll file. How exactly does this process work in linux?
ld is the GNU linker. You can invoke it separately (which is what most makefiles will end up doing), or you can have g++ delegate to it. The options you pass to g++ and ld determine where to look for included headers, libraries to link, and how to output the result.
Recommendations for code editors? I am currently using nano and I've heard of vim and emacs, but don't know what the benefits of them are over eachother. Are there any others, and why would I consider them over any of the previous three? Note: I am not looking for an IDE.
Vim and Emacs are very flexible editors that support a whole bunch of different usages. Use whatever feels best to you, though I'd suggest you might want a few minimal things like syntax highlighting.
Just a note to go with MandyK's answers.
Creating make files by hand is usually a very unportable way of building across linux distro's/unix variants. There are many build systems for auto generating make files, building without make files. GNU Autotools, Cmake, Scons, jam, etc.
Also to go more in depth about configure.
Checks available compilers, libraries, system architecture.
Makes sure your system matches the appropriate compatible package list.
Lets you specify command line arguments to specialize your build, install path, option packages etc.
Configure then generates an appropriate Makefile specific to your system.
What are recommended guides on
creating a make file, how do I compile
from this makefile (do I call g++
myself, do I use 'make'?)
I learned how to write makefiles by reading the GNU Make manual.
Looking at other linux software, they
almost always seem to have a
'configure' file. What exactly does it
do? Does it only check if the required
libraries are installed or does it
more than just checking requirements?
The configure file is usually associated with autotools. As the name of the script suggests, it allows you to configure the software. From the perspective of the developer this mostly means setting macros, which determine variables, which libraries are available, and such. It also tests for the availability of libraries. In the end the script generates a GNU Makefile, which you can then use to actually build and install the software.
The GNU build system is only one of many. I don't particularly like the GNU build system as it tends to be slower than others, and generates an ugly Makefile. Some of the more popular ones are CMake, Jam (Boost Jam might be of interest for C++) and waf. Some build systems simply generate Makefiles, while others provide a completely new build system. For simple projects writing a Makefile by hand would be easy, but "dependency checking" (for libraries, etc) would also have to be done manually.
Edit: Brian Gianforcaro also pointed this out.
Your question is a bit too general, but here is what I would recomment:
Editor: vim and emacs are popular. What matters the most, as with most tools, is to master one. I like using vim because vi (its descendant) is available everywhere, but that may not be very relevant, specially if you stay on Linux. Any programming editor is fine.
configure: unless you do big projects, don't bother with it. It is a nightmare to use and debug. It only makes sense if you intend to distribute your project - in that case, read the autobook: http://sources.redhat.com/autobook/. As other said, there are alternatives (cmake, scons, etc...). I am quite familiar with both scons and autotools, but I still use make for small (couple of files) projects.
Concerning shared library: it is almost as windows, except that you link against the shared library directly - there is no .lib vs .dll distinction in Linux. For example. for one library foo with a function foo:
int foo(void)
{
return 1;
}
You would build it as follows:
gcc -fPIC -c foo.c -o foo.o
gcc -shared foo.o -o libfoo.so
A main (of course in real life you put the API in a header file):
int foo(void);
int main(void)
{
foo();
return 0;
}
And then, you link it as:
gcc -c main.c -o main.o
gcc main.o -o main -L. -lfoo
The -L. is here to say that you want the linker to look in the current directory (contrary to windows, this is never done by default in Linux), the -lfoo says to link against the library foo.
So to get you started I will first point you to this guide for makefiles, it also covers some linking stuff too.
It's just a little something my university Computer Science prof gave us I found it to be very clear and concise, very helpful.
And as for an IDE, I use eclipse usually because it handles the makefile as well. Not to mention compile and standard output are right at your fingertips in the program.
It was mainly intended for Java developing, but there is a C/C++ plugin too!
Recommendations for code editors? I am
currently using nano and I've heard of
vim and emacs, but don't know what the
benefits of them are over eachother.
Are there any others, and why would I
consider them over any of the previous
three? Note: I am not looking for an
IDE.
Vi and Emacs are the two quintessential Unix editors; if you are set on using a text editor rather than an IDE, one of them or their derivatives (vim, xemacs, etc) is the way to go. Both support syntax highlighting and all sorts of features, either by default or via extensions. The best part about these editors is the extensibility they offer; emacs via a variety of lisp, and vim via its own scripting language.
I personally use Emacs, so I can't say much about Vim, but you should be able to find lots of information about both online. Emacs has several good tutorials and references, including this one.
EDIT [Dec 2014]: There seems to be a trend of cross-platform and highly extendable editors recently. This could be a good choice if you'd like something less than an IDE, but more graphical than vi/emacs and native-feeling across multiple platforms. I recommend looking at Sublime or Atom; both of these work across Windows/Linux/Mac and have great communities of plugins and themes.
I recommend the book The Art of Unix Programming by ESR. It covers choice of editor, programming language, etc. It also gives a good sense for the mindset behind programming on Unix or Linux.
For editors, you probably want either Vim or Emacs. They are both different and which one is better is more about personal taste than anything else. I use Vim. It is great for quickly moving around the code and making changes. I didn't like Emacs as much but many people do. Emacs is extremely extensible and can be used for everything from a news reader to an ide. Try both and see what you like.
Recommendations for code editors? I am currently using nano and I've heard of vim and emacs, but don't know what the benefits of them are over eachother. Are there any others, and why would I consider them over any of the previous three? Note: I am not looking for an IDE.
If you're using Linux with a window manager (KDE, Gnome, etc.) you could also consider using the standard text editor for your window manager. The main benefit it would have over vim/emacs/nano is that it would seem more familiar to one coming from a Windows environment - an editor written to run on the window manager has a menu bar, file open/save dialogs, undo/redo, and plenty of other neat features that console editors probably can't match. (Though emacs and vim are pretty sophisticated these days, so who knows ;-P)
On KDE (which is what I use) I can recommend KWrite, which is a well-featured but fairly basic text editor with syntax highlighting; or Kate, which is a fancier text editor with some extra features: session management, a builtin terminal panel, automatic invocation of make, and several plugins including a C/C++ symbol viewer. I usually use Kate for my C++ work when I don't want to bother with setting up a full IDE project. (FYI the IDE for KDE is KDevelop)
the space between invoking g++ directly and using an autotools build chain is pretty narrow. Get good at autotools, which is really the closest thing to a 'project' available in the Linux/Open Source world.
For someone coming from Visual Studio, all this commandline stuff might seem arcane and messy.
Before you turn into a bash shell/vim/emacs junkie, try a few GUI based tools first so you have some transition time...
QT 4.5 with its QT Creator mini-IDE. This is the best framework, lightyears ahead of the competition.
Eclipse (C++) - From my experience with this on Windows, I find it's astounding ( This is probably the best Java application ever written )
KDevelop
Anjuta
If you use Delphi, Lazarus/FreePascal is a good alternative.
I'm sure the longhairs will scoff and claim that vim or emacs gives them the best and fastest development environment, but different strokes for different folks. Someone accustomed to an IDE will take some time to switch or may not wish to switch at all.
For all their editing prowess, creating GUI apps is certainly not a job for 80x25 tools.
It takes years to become an expert with the command line side of things, its more of a transformation of worldview than anything else.
As a side note amongst the proper answers here.. In case you wanna hit the ground running as a Windows guy, I'd suggest the fresh new Qt SDK. It will feel like home :-)
I advise using SCons in place of Make, it does the same job but it's easier to use and handle out of the box how to make dynamic libraries, dependencies, etc. Here it is a real life example for a simple prog
env = Environment()
env.Append(CCFLAGS='-Wall')
env.Append(CPPPATH = ['./include/'])
env.MergeFlags('-ljpeg')
env.ParseConfig("sdl-config --cflags --libs")
env.ParseConfig("curl-config --cflags --libs")
env.ParseConfig("pkg-config cairo --cflags --libs")
env.Program('rovio-pilot', Glob('./src/*.cpp'))
As a text editor, I'm happy with JEdit for coding, but it's a matter of taste.