Qualified's blog

By Qualified, history, 5 months ago, In English

What is your compiler flags when using Vim for C++? Where do you put them?

 
 
 
 
  • Vote: I like it
  • +26
  • Vote: I do not like it

»
5 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

In .vimrc file. Here is mine....

autocmd vimEnter *.cpp map <F8> :w <CR> :!clear ; g++ --std=c++17 %; if [ -f a.out ]; then time ./a.out; rm a.out; fi <CR>

  • »
    »
    5 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I put it in my .vimrc but when I press F8, it doesn't run. Where does the output show? How to input numbers? Do you have multiple input and output files?

    • »
      »
      »
      5 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      It works fine for me. Where did you put .vimrc file ? are you sure it is on your home directory. you can just make it like, vim ~/.vimrc or vim /home/USERNAME/.vimrc

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    is it for ubuntu ?

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I don't use Vim but if you are compiling from the command line you can make an alias so that you can type "bd" instead of "g++ -Wall ...". On Linux you can modify the .bash_aliases. On Windows you can modify $PROFILE by adding a function to it like:

function cl($arg) {
    g++ -std=c++17 -Wshadow -Wall -Wextra -g -D_GLIBCXX_DEBUG $arg
}

and then you can simply type "cl xxx.cpp" to compile.

  • »
    »
    5 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I want to compile and run within Vim but thanks!

    • »
      »
      »
      5 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      BTW, how do you modifly $PROFILE? Just a question, was wondering...

    • »
      »
      »
      3 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      can you please help me .. i want to compile by such that it opens a new terminal screen so that i can compair my code and its output

      • »
        »
        »
        »
        3 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Did you set up .vimrc?

        • »
          »
          »
          »
          »
          3 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          yes but its compiling and running my code in same terminal i.e by first closing my vim and then running my code and then after that again opening my vim.. i want that the output must be shown by opening new terminal window ..like in codingblocks ide... is it possible?? help:)

          • »
            »
            »
            »
            »
            »
            3 months ago, # ^ |
            Rev. 2   Vote: I like it 0 Vote: I do not like it

            You can use :term CMD to open a terminal in a new window and execute a command in that window. E.g. try :term ls. Replace that with your command you want to run, e.g. the command to compile or run your program. Obviously you can also make a keybinding for that command.

»
5 months ago, # |
  Vote: I like it +3 Vote: I do not like it
Here's my Compilation and Execution part of Vim Config


F9 for Compile and Run
F5 for Compile ( never use this :p )
F2 for saving file

I save and run inside Vim. Also, when creating a new file it asks for a template that I should load , which is saved in .vim/template and it fetches .cpp types which you can easily change

And just in Case :

My Vim Configuration

Hope it helps!

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    When I press F9, it says that '.' is not recognized as an internal or external command, operable program or batch file. shell returned 1. How to solve this problem?

    • »
      »
      »
      5 months ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Here is my .vimrc

      .vimrc
      • »
        »
        »
        »
        5 months ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        By any chance, are you on Windows?

        Edit : Sorry Never came across such error , Did you try compiling first , then running by terminal command ./file_name

        • »
          »
          »
          »
          »
          5 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          How to compile first and how to run it in terminal?

          • »
            »
            »
            »
            »
            »
            5 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            As mentioned above,

            • By pressing F2 to save your code,
            • F5 you can compile your code,( if there's no error, there will be a file created in your current directory with name of your current cpp file name )
            • Exit vim and run ./filename in the directory file is saved
            • »
              »
              »
              »
              »
              »
              »
              5 months ago, # ^ |
                Vote: I like it +1 Vote: I do not like it

              Thanks!

              • »
                »
                »
                »
                »
                »
                »
                »
                3 weeks ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                hi bro , did you could to run files , i faced same problem as you

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    thank you! But is this the same for VIM in windows too?! Where the .vimrc file is named as _vimrc.

    • »
      »
      »
      5 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I would (in normal mode) type ":version" and then a list of files will appear. Towards the bottom, it will say "user vimrc file: " then that file name. If you don't see that, first create one.

  • »
    »
    2 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    How to set up a C++ template for a GVIM on Windows for competitive programming? Please I have tried so hard, but I haven't found any solution.

    • »
      »
      »
      2 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      What do you mean by template? Is it like sublime in which you type in the "template", the template appears in the file?

  • »
    »
    2 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    How to set up a C++ template for a GVIM on Windows for competitive programming? Please I have tried so hard, but I haven't found any solution.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I only use F9 with your configure. However, when I press F9, it only show command g++ -g % -o %:r && ./%:r and never stop. How can I fix it

    • »
      »
      »
      6 weeks ago, # ^ |
      Rev. 3   Vote: I like it 0 Vote: I do not like it

      use this

      autocmd filetype cpp nnoremap <F10> :w !g++ -std=c++14 -O2 -Wall % -o %:r && %:r.exe
      

      just pess F10 to compile & run at the same time and you'll not get any error.

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I would like to have a vim plugin to include code snippets.

On some keypress a listing of the files of a configured directory should be displayed. I can choose one. If choosen, that file should be copied into my current edited file, maybe at cursor position.

Any suggestions? Or how to write such one?

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Or does somebody know how to use/extend NERDTree for that purpose? File listing and choosing is perfect, but I don't know how to read a file on keypress. Instead I have to open it, copy content, close it, then paste content.

    Surly there is a better way.

    • »
      »
      »
      5 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I don't exactly know how to do this in Vim, but whenever I need to copy a whole file I just do xsel -b < FILE.

  • »
    »
    4 months ago, # ^ |
    Rev. 2   Vote: I like it +8 Vote: I do not like it

    Hey. I've done exactly the thing you are describing. With a key binding I can choose between all my prewritten algorithms, choose one, and it will be copied into the file at the current position.

    Take a look here: https://github.com/jakobkogler/Algorithm-DataStructures (The Readme also contains a GIF with a short demonstration)

    The path to the directory is hard-coded to my algorithms, but you can also change it to something different in the autoload/alg_ds.vim file.

  • »
    »
    3 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    You might wanna try UltiSnips. It works just like snippets in VS Code and Sublime. You can even add a tab trigger with a few lines of vim script.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    bruh! can u share your vimrc file ?

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Not sure if it really helps, but anyway:

      .vimrc
»
5 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Use terminal and Makefile.

Makefile

P.S. I don't know, why one dollar automatically replace to three dollars, and have no idea, how to write exactly one dollar, sorry(

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I am new here. Is there any reason why using vim is better than sublime? There are many good settings and automations that you can do with sublime.

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    What editor you use is up to your personal preference. An editor might be better for me, but worse for you. Find one that you like, learn how to use it and stick to it.

    The reason I use Vim, is because it is small, fast, directly available in the terminal, available on every server, modal editing is awesome, huge possibilities of configuration and extension, one editor for everything, ...

    Also I doubt that Sublime has a feature that you can't have in Vim. Well, maybe one feature: being beginner friendly :-)

    • »
      »
      »
      2 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Seems appropriate. But why not just use the custom invocation provided by CodeForces itself?

      • »
        »
        »
        »
        2 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Advantages that you have by developing locally:

        • much faster compile and run times
        • not dependent on internet connection
        • you can work on multiple files at once
        • auto-completion, error warnings, jump to declaration, and many other IDE niceties
        • debugging capabilities
        • code snippets support
        • you don't get called "noob" as often
        • and probably many, many more...
        • »
          »
          »
          »
          »
          2 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Alright. Seems like the number of reasons is much more than I had expected. Anyways, thanks.

  • »
    »
    6 weeks ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    .

»
5 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

simplest form of compiler flag

map<C-b> :!g++ -std=c++17 -O2 % <CR>  
map<C-n> :!./a.out < in <CR>
  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I use

    autocmd filetype cpp nnoremap <F9> :w <bar> !g++ -std=c++17 % -O2 -Wall -Wextra -DLOCAL -o %:r -Wl,--stack,268435456<CR>
    autocmd filetype cpp nnoremap <F10> :!%:r<CR>
    

    just press for compiling and for executing. :P

    • »
      »
      »
      4 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      it's working, but how can i run, compile and save from a single command??

      • »
        »
        »
        »
        4 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        All you have to do to compile is press . Then, you can press for executing where you can input your data. For saving a file go to normal mode by pressing then type

        :w
        

        If you want to close and save that file in normal mode, you can press

        :wq
        

        Hope that this helped!

        • »
          »
          »
          »
          »
          4 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          thanks, it really helped!!

        • »
          »
          »
          »
          »
          6 weeks ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          do u know what it does in Vim =>

          autocmd filetype cpp nnoremap <C-C> :s/^\(\s*\)/\1\/\/<CR> :s/^\(\s*\)\/\/\/\//\1<CR> $

    • »
      »
      »
      2 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      How can i compile & run pressing a single function ?? i have a source but it only compile & runs C++11 .But i want it for C++17 .

      • »
        »
        »
        »
        2 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        If you want to press a single button and have it compile and execute and have C++17 with -O2 and -Wall put

        "autocmd filetype cpp nnoremap <F5> :w <bar> !g++ -std=c++17 -O2 -Wall % -o %:r && %:r.exe <CR>
        

        in your .vimrc.

        • »
          »
          »
          »
          »
          2 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          But, how can i add this --stack,268435456 in vimrc for compiling & running at the same time !?

          • »
            »
            »
            »
            »
            »
            2 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Then use

            autocmd filetype cpp nnoremap <F5> :w <bar> !g++ -std=c++17 -O2 -Wall % -o %:r -Wl,--stack,268435456 && %:r.exe <CR>
            
            • »
              »
              »
              »
              »
              »
              »
              2 months ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              But when i try to add bits/stdc++.h , it doesn't work and shows error.

              • »
                »
                »
                »
                »
                »
                »
                »
                2 months ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                It works for me though

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  2 months ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  which compiler are u using for C++ ?

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  2 months ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  I am using MinGW version 9.2.0 on Windows.

        • »
          »
          »
          »
          »
          6 weeks ago, # ^ |
          Rev. 3   Vote: I like it 0 Vote: I do not like it

          bruh! i am also using tmw's vimrc but when i first compile (with bits/stdc++.h header) with F9, and then execute my cpp file with F10, the compilation is done quickly, but the problem is=> if i use a F5 to compile & run (that u provided me in this post

          this line

          ), the performance is laggy and why does it takes more than 4 seconds to compile first and then run. Any fix to reduce the time with appropriate compilation flag?

          • »
            »
            »
            »
            »
            »
            6 weeks ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Precompiled headers.

            • »
              »
              »
              »
              »
              »
              »
              6 weeks ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              i did that also, but didn't work! u can check on your windows also. thanks!

              • »
                »
                »
                »
                »
                »
                »
                »
                6 weeks ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                For the F5 command try using this

                autocmd filetype cpp nnoremap :w !g++ -std=c++14 % -o %:r -Wl,--stack,268435456 && %:r.exe
                
                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  6 weeks ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  tried but it now runs in 3.5 seconds!

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  6 weeks ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  any solution ?

            • »
              »
              »
              »
              »
              »
              »
              6 weeks ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              btw, can u share your current vimrc file with me now!

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    when i tried your code, it says the system cannot find the file specified. shell returned 1?? what should i do??

    • »
      »
      »
      4 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I think his config is for linux. You may change ./a.out to ./a.exe. I'm not using windows so not 100% sure.

      • »
        »
        »
        »
        6 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        can u share a valid vimrc file for ubuntu ?

    • »
      »
      »
      4 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      you should have a "in" file first.

      • »
        »
        »
        »
        4 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        can you please explain what is this in file and how to create it??

        • »
          »
          »
          »
          »
          4 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          "in" is the name of the input file of your program. My setup is use ctrl-n to run the file with input in the "in" file, you can change it to another file name. Just a file to paste input of your program then to run.

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    do i have to add something else than mingw to path??

»
4 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

In ~/.vimrc , here is mine ...

nnoremap <C-a> :!g++ -std=c++17 -Wshadow -Wall -o %:r.out % -O2 -Wno-unused-result | :update<CR> nnoremap <C-x> :!./%:r.out

»
2 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

This is my Gvim commands to compile and Execute the c++ code :w :!g++ % :!filename.exe ``

»
2 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

save :w compile :!g++ % Execute or run :!filename.exe

I think it will work because I am compiling and running this command on Gvim or vim your welcome if this work on your pc or computer

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

:w :!g++ % :!filename.exe ``

»
2 months ago, # |
Rev. 2   Vote: I like it -23 Vote: I do not like it
  1. This my _vimrc
  2. " Vim with all enhancements
  3. source $VIMRUNTIME/vimrc_example.vim
  4. ``
  5. " Use the internal diff if available.
  6. " Otherwise use the special 'diffexpr' for Windows.
  7. if &diffopt !~# 'internal'
  8. set diffexpr=MyDiff()
  9. endif
  10. function MyDiff()
  11. let opt = '-a --binary '
  12. if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  13. if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  14. let arg1 = v:fname_in
  15. if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
  16. let arg1 = substitute(arg1, '!', '\!', 'g')
  17. let arg2 = v:fname_new
  18. if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  19. let arg2 = substitute(arg2, '!', '\!', 'g')
  20. let arg3 = v:fname_out
  21. if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  22. let arg3 = substitute(arg3, '!', '\!', 'g')
  23. if $VIMRUNTIME =~ ' '
  24. if &sh =~ '\<cmd'
  25. if empty(&shellxquote)
  26. let l:shxq_sav = ''
  27. set shellxquote&
  28. endif
  29. let cmd = '"' . $VIMRUNTIME . '\diff"'
  30. else
  31. let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
  32. endif
  33. else
  34. let cmd = $VIMRUNTIME . '\diff'
  35. endif
  36. let cmd = substitute(cmd, '!', '\!', 'g')
  37. silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
  38. if exists('l:shxq_sav')
  39. let &shellxquote=l:shxq_sav
  40. endif
  41. endfunction
  42. ``
  43. ``
»
2 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

How to include timeout in the command so that it automatically terminates in case of infinite loop..?
autocmd filetype cpp nnoremap :w !timeout 3s g++ -ulimit -Wall -Wno-unused-result -std=c++17 -O2 % -o %:r && ./%:r
Above command works fine for running code but timeout does'nt work.

»
2 months ago, # |
  Vote: I like it -19 Vote: I do not like it

You can use "Dev-C++"!It's easy for everyone

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Did you consider using a text editor with vim keybindings?

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

This my vim commands to compile and execute save:w compile :!g++ -Wall -o "a" "a.cpp" Execute :!a.exe

so it will open exe file and you paste input in it and you will get output in Vim but use Gvim

Thank you

»
7 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

https://github.com/xuhdev/SingleCompile is a Vim plugin made to compile and run single files and it's really good because it support a lot of languages. iirc it runs synchronously (that means it stops vim while your program is running)

In Neovim and Vim >= 8 asynchronous tasks were introduced, and i use this plugin ( https://github.com/skywind3000/asyncrun.vim ) because is simpler to configure. But with it you have to define a compile command for every programming language... To simplify everything i made an external bash script that is called by AsyncRun when I press F9. It automatically detects the language and calls the right compiler.