Qualified's blog

By Qualified, history, 5 years 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

| Write comment?
»
5 years 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 years 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 years 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

»
5 years 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 years 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 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      4 years 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

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

        Did you set up .vimrc?

        • »
          »
          »
          »
          »
          4 years 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:)

          • »
            »
            »
            »
            »
            »
            4 years 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 years 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 years 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 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Here is my .vimrc

      .vimrc
      • »
        »
        »
        »
        5 years 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 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

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

          • »
            »
            »
            »
            »
            »
            5 years 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
        • »
          »
          »
          »
          »
          18 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Is there any solution for windows, it's working in linux but showing error in powershell

  • »
    »
    5 years 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 years 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.

  • »
    »
    4 years 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.

»
5 years 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 years 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 years 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 years 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.

  • »
    »
    4 years 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.

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

    bruh! can u share your vimrc file ?

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

      Not sure if it really helps, but anyway:

      .vimrc
»
5 years 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(

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

    can u share ur github link for the makefile and can u share how does it work on windows

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

      Ok, link.

      Idk, how does it work on windows. I advise you to install any Unix system, it's more useful for programming.

»
5 years 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 years 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 :-)

    • »
      »
      »
      4 years 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?

      • »
        »
        »
        »
        4 years 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...
»
5 years 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 years 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 years 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 years 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 years 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 .

      • »
        »
        »
        »
        4 years 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.

        • »
          »
          »
          »
          »
          4 years 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 !?

          • »
            »
            »
            »
            »
            »
            4 years 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>
            
            • »
              »
              »
              »
              »
              »
              »
              4 years 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.

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

                It works for me though

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

                  which compiler are u using for C++ ?

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

                  I am using MinGW version 9.2.0 on Windows.

        • »
          »
          »
          »
          »
          4 years 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?

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

            Precompiled headers.

            • »
              »
              »
              »
              »
              »
              »
              4 years 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!

              • »
                »
                »
                »
                »
                »
                »
                »
                4 years 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
                
  • »
    »
    4 years 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 years 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.

»
4 years 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.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Did you consider using a text editor with vim keybindings?

»
4 years 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.