LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Vim and C++ (https://www.linuxquestions.org/questions/programming-9/vim-and-c-231268/)

Dartelin 09-16-2004 05:25 AM

Vim and C++
 
I have a question about vim: how can i configure it for C++ writing ? I mean i would like to write the C++ code and then test it without getting out of vim. Thx.

Boudewijn 09-16-2004 07:43 AM

Afaik vi(m) does colour the texts if you open a .cpp file. There's AFAIK no integrated compiler in vi(m).

To open a cpp file using vi type:

vi XXX.cpp

druuna 09-16-2004 07:46 AM

A little vim voodoo:

File number one, call it: compile.vim
Code:

" MAPS:
"
"  ,;  Compile current project file.
"  ,,;  Execute the compiled file.
"
" NOTE:
"
"  The project file is the currently loaded file, unless
"  the global variable 'project' is defined:
"
"    let project="test.c"
"
"  in which case that is the file to be compiled.
" -----------------------------------------------------------------------------
let project=""
let compile_file=""

augroup compiletex
  au!
  au bufenter *.c, nm ,; :call CompileC()<cr>
  au bufenter *.cc,*.cpp,*.c++ nm ,; :call CompileCC()<cr>
  au bufenter *.c nm ,,; :!./a.out<cr>
  au bufenter *.cc,*.cpp,*.c++ nm ,,; :!./a.out<cr>
  au bufenter *.vim,*vimrc* nm ,; :so % <cr>
augroup END

fu! CompileFile()
  if g:project==""
    let g:compile_file=expand("%")
  else
    let g:compile_file=g:project
  end
endf

fu! CompileC()
  w!
  call CompileFile()
  set makeprg=gcc
  set errorformat=%f:%l:\ %m
  exe "make ".g:compile_file
endf

fu! CompileCC()
  w!
  call CompileFile()
  set makeprg=g++
  set errorformat=%f:%l:\ %m
  exe "make ".g:compile_file
endf

The above file does all the actual work:
,; will compile your code,
,,; will execute your code.

The above file needs to be loaded in oder for it to work, place the following line in your .vimrc:

so /path/to/compile.vim

You might want to play with the following line (the execute line):

au bufenter *.cc,*.cpp,*.c++ nm ,,; :!./a.out<cr>

The above line will just execute the code, if it needs input oyu are able to give it and at the end you need to press return/enter to go back to your vi session.

You can also redirect it to a file, in this example it is redirected to out

note: if any input is required by your code, you need to give it 'blind':

au bufenter *.cc,*.cpp,*.c++ nm ,,; :!./a.out > out<cr>:e out<cr>

You might have noticed that this will not only compile/execute C++ (*.cpp,*.c++,*.cc) files, but C (*.c)files as well.

Hope this is what you wanted.

edit
PS: Hate those smileys....... Everywhere there's a smiley in the code it should say: : p (without the space in between).

deiussum 09-16-2004 08:23 AM

If you want to know the standard vim commands for compiling and running your code you can also use the following:

To compile your code with make, use:
:make

This works best if you have a Makefile, but if you just have a single .cpp file and no non-standard libraries you need to add to the command line, you can also do the following to compile a file called foo.cpp.

:make foo
(Note, no .cpp)

If there are compile errors it will bring you to the first error. You can then use the following to navigate the errors:

Next error
:cn

Previous error
:cp

List errors
:cl

View the compile log
:cope

To execute a program, just use something like:
:!./foo

Dartelin 09-16-2004 09:30 AM

Ok thank u guys for the answer. Im just a beginner in C++ and would like to learn it. The first contact with C i had in Turbo C so i just wanted a similar program. Thx again.


All times are GMT -5. The time now is 03:48 PM.