ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
" 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).
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.