compile directly from vim - c++

I'd like to compile cpp file w/o turning off vi.
I know the :!g++ file.cpp but I prefer :make so I added this line in .vimrc file
au FileType C set makeprg=gcc\ %
au FileType Cpp set makeprg=g++\ %
but I keep getting
"make: ***** No targets specified and no makefile found. Stop.** "message.
can anyone tell me what is wrong with my setting?
I use to compile successfully with the option above.

You need the substitution there, try something like:
set makeprg=gmake\ %:r.o
Oh, this assumes that you've got:
a (M|m)akefile in the directory, or
default SUFFIX rules are available for your environment (which it looks like there aren't)
Check for the default by entering:
make -n <my_file>.o
and see if that gives you something sensible.
If there is a makefile in another location you can add the -f option to point at the makefile, for example:
set makeprg=gmake\ -f\ ../some_other_dir/makefile\ %:r.o
BTW For learning about make, and especially gmake, I'd suggest having a look at the excellent book "Managing Projects with GNU Make" (sanitised Amazon link).
HTH.
cheers

I should change C,Cpp into c,cpp, then it works fine.
thank you all, especially Rob Wells, your answer helped me a lot. thank you.

I think it's much easier if you write a Makefile and put it where vi can find it. I'm not sure if you actually use vi (I've only used Vim), but when there is a Makefile compiling should be as easy as writing :make (no set makeprg needed).

It can be easily achieved by the use of key maps.
First open up your vimrc file and these lines to the file,
autocmd filetype cpp nnoremap <F4> :!g++ % -ggdb -o %:r <CR>
autocmd filetype cpp nnoremap<F5> :!g++ % -ggdb -o %:r && ./%:r <CR>
The first line maps the key F4 to compiling the file. The second line maps the key F5 to compile and run.
If you use gdb frequently then this may also come handy.
autocmd filetype cpp nnoremap<F10> :!g++ % -ggdb -o %:r && gdb -tui %:r <CR>
This line maps the key F10 to compile and start gdb
Hope this helps.

I recommend a vim plugin called SingleCompile instead of what you have done:
http://www.vim.org/scripts/script.php?script_id=3115

First of all, just make the bloody make file. Every tool out there is expecting to work with make and if your compilations are that simple it takes about 30 seconds to write a make file that compiles all c and cpp files into an executable.
Second, if you refuse to use a make file then try
:help system
That should give you enough info to come up with your own command similar to this
:com Mymake call system("g++ ".expand("%"))

Related

Compiling a C++ program in Vim

I am using Ubuntu 16.04, with Vim
Can't find a way to compile and run my C++ program in Vim.
I have appended this to my vimrc file
nnoremap <silent> <F8> :!clear;gcc % -o %:r && ./%:r<CR>
from this question: How do I run a C program from VIM?
But it isn't working - my program doesnt seem to compile when I press F8.
Any help would be appreciated.
You should have gone for the vim way of doing things. In particular in C++.
Calling g++ from :! instead of :make is really counter-productive (:h quickfix).
Follow this path instead: https://stackoverflow.com/a/35702919/15934 (1), unless you are under windows with the poorly configured make from MingW, in which case, follow this path: https://stackoverflow.com/a/22452184/15934
(1) If you want a mapping, it would be something like:
nnoremap <silent> <F8> :update<cr>:make %<<cr>
You can simply create a makefile and then add
nmap <F8> :make!<CR>:cw<CR> to your vimrc file

How can I give the filename of current buffer automatically when I execute 'compile' command in Emacs?

As you already know, we can see 'make -k' command when we execute 'M-x compile' in emacs c++ mode.
But, now I'm studying some boost libraries, so I must frequently compile just single file after writing a example code.
So, for convenience, I've been doing this like below.
(1) make an alias in shell init file for g++ compile command.
alias c="g++ -g -Wall -O0 -std=c++11"
(2) map 'M-x compile' command to Function Key F9 in .emacs
(global-set-key [f9] 'compile)
(3) write c++ code
(4) press F9 and modify filename to current open file
For example, you can see 'Compile command: c vvv.cpp' in the below screen shot.
(This screen shot is just a simple code that I made for this question.
'c' is alias for compile command and options)
But, this open filename is actually 'vector1.cpp', so 'vvv.cpp' should be automatically replaced by 'vector1.cpp'.
I think this is not bad.
But, I'd like to find a way that I don't have to modify the current filename.
"Just press F9 and RET, then the current file will be compiled." This is exactly what I want.
Is there any better idea or way I can do like this?
Or plz let me know your nice compiling way for a minimal key pressing.
Thanks.
The EmacsWiki has a nice section explaining how you can customize the compile command: Compile Command
In your case, you could add a hook to the c++ mode to define the compile-command variable as you need. Following the example on the wiki:
(add-hook 'c++-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(format "g++ -g -Wall -O0 -std=c++11 %s" (buffer-name)))))
Adding this snippet to your .emacs should do the trick.

Makefile for Linux from Xcode-written C++ program

I've written a simple c++ program on Xcode, all contained within ONE FILE called huffmanGenerator.cpp. The program reads input from a file on the user's computer, and writes output to a file saved to their computer.
The instructor has asked us to create a makefile so that our programs compile and run with g++ OR gcc in Linux; however she never showed us how to do so, and when the class asked for help, her answer was we could figure it out.
I found many links online, but they're all very confusing as this is all new to me, and most of them fail to answer even the most basic questions like what kind of file should the makefile be? Is it a .txt? Should I just save one in word?
Please help do what the instructor won't, enlighten me. Thanks!
what kind of file should the makefile be?
It should be a plaintext file called Makefile or makefile. The reason the name matters is because when you run the make command, it looks for a file with this name by default for directions on how to compile your code. You can also name it whatever you want as long as you specify the name when you run it (make -f filename).
Is it a .txt?
No, it has no extension. Extensions don't mean that much in *nix.
Should I just save one in word? (Assume you mean Microsoft Word.)
No, definitely not. Whitespace (tabs/spaces/new lines) have meaning in these files, so you should use an editor that won't add formatting to the file. Something like pico/vi/etc.
Here is an example of a makefile, that I think does what you are asking.
# You can change your compiler to gcc / g++ here.
CC=g++
# Add whatever flags you want to use here.
CFLAGS=-c -Wall
all:
$(CC) $(CFLAGS) huffmanGenerator.cpp -o huffmanGenerator
#Use something like this to run `make clean` which deletes your object files, so you can do a fresh compile.
#clean:
# rm -rf *o huffmanGenerator
As a side note, you would be served well not to blame your professor for not spelling out everything for you. When you graduate, you will often be given tasks that have no other directions than a set of requirements and a deadline. You will need to figure it out. You could have easily made this make file by visiting http://mrbook.org/tutorials/make/ (search google for 'makefile tutorial').
The makefile should be called Makefile. It is just a text file.
You need a text editor. There are many to choose from, vim, emacs, nano, pico, ..., etc.
Open a command line and run, say
$ pico Makefile
Then you would enter the contents of the Makefile
all:
g++ -o huffmanGenerator huffmanGenerator.cpp
Save and exit and run make
$ make

Setting up vim as C++ IDE

I wish to setup vim as C++ IDE so I can do all work from it.
I'm using these plugins for vim:
Clang complete - accurate completion
nerdtree - browse files
snipmate - insert snippets
AutoComplPop - omni-completion
buffergator - buffer management
vim-powerline - nice statusbar
vundle - to manage plugins
But I lack things like Jump to definition and compiling multiple files in one executable, project view...
I'm using
nmap <F8> :w % <bar> :!g++ -W -Wall -Wextra -pedantic -std=c++11 % -o %:t:r<CR> <bar> :!./%:t:r<CR>
to compile current file, but it won't work if there are multiple file that create one executable.
I know I could just use eclipse, netbeans, code::blocks and such, but I really like vim... If such thing as vim ide isn't possible do I have to learn GNU build system or some other method?
Any advice is welcome.
You need to create a makefile which handles the build process.
Then from vim just run :make, it will run the build and pop all errors in quickfix window where you can navigate and jump to error locations.
First, to jump to definitions, you might try this:
http://www.santiagolizardo.com/article/vim-jump-to-classes-and-functions-defined-in-different-files/64003
I haven't tested it, so I can't tell you if it works.
Now, to build multiple file projects, it might be better for you to learn how to use makefiles and automake. These links might help you:
http://homepages.gac.edu/~mc38/2001J/documentation/g++.html
http://www.openismus.com/documents/linux/automake/automake
Good luck.
Edit: A similar question was answered on this link: https://stackoverflow.com/a/563992/1820837
"Jump to definition" is already there, it's <C-]> with the cursor on a keyword or :tag foo on the command line.
For these to work, you need a tags file generated by exuberant-ctags and to tell Vim where to find it. See :help tags and :help ctags.
Without a tags file, gd goes to the definition of the keyword under your cursor if it's in the same file. But it's not as generally useful as <C-]>.
For "Jump to definition" I can recommend the YouCompleteMe, plugin which is really easy to setup with vundle.
Otherwise there is also ctags, but I find it less useful.
To use vim as a IDE, I find this post useful.

reference a filename in vi

Sometimes I run make directly from the vim command line. However, sometimes I would just like to build one file currently being edited: !g++ filename.cpp . Is there a shortcut to reference the file without having to type it..?
Guys, I DO NOT want to use make at all. all I want to do is to build it from vi's command line, using g++/gcc
You can use % to reference the current file
so:
:!g++ %
in VIM "%:p" stands for the current file.
try "!g++ %:p"
If your make program is actually GNU make, just execute:
:make %<
If you want to add flags like -Wall or -pedantic then just set $CFLAG (for compiling C files, or $CPPFLAGS for C++ files), or if you want to specify libraries then set $LDFLAGS from vim.
:h :make
:h %<
EDIT:
Unlike plain calls to :!gcc, this solution is compatible with the quickfix mode (:h quickfix), and it does not require to change &makeprg to 'g++ $CPPFLAGS -o $* $*.cpp $LDFLAGS'.
NB:
No need to write any makefile to take advantage of GNU-make.
And even if you have one Makefile, and as long as you don't mess with the default implicit rules, this solution will also work!
You can use this to refer to the filename you are working on with the extension substituted with .o:
%r.o
Your filename.cpp becomes filename.o which is useful if you put something like this in your .vimrc.:
set makeprg=gmake\ %:r.o
That way you can just do this in vim and it will then launch the command declared using makeprg on the current file:
:make
As mentioned the shortcut is %.
You can bind the whole thing to one key by putting the following in your .vimrc file:
map <F9> :!gcc %<CR>