Skip to content

Specify Compilation Flags for Each Individual Source File

Hong Xu edited this page Oct 27, 2013 · 2 revisions

Sometimes, a single source file may depend on some specific set of libraries. In order to make SingleCompile depend on these libraries when doing the compilation, we can take advantage of let-modeline. This plugin extends Vim modeline to customize variables, aka "let" in vimscript.

First, follow the instructions to install let-modeline. Then add something similar as the following lines to your .vimrc file:

autocmd BufEnter * if exists('b:sc_additional_flags') |
            \ exec 'nmap <buffer> <f9> :SCCompileAF ' . b:sc_additional_flags . '<cr>' |
            \ exec 'nmap <buffer> <f10> :SCCompileRunAF ' . b:sc_additional_flags . '<cr>' |
            \ end

Then, for any file you need to compile with specific flags, simply add a "let modeline" to your file, either at the beginning or the end, e.g. for a C++ source file, you can add the following line to the end or the beginning to specify the specific flags you need:

// VIM: let b:sc_additional_flags='-lz'

Now, if you press , your source file will be compiled with an additional -lz flag; if you press , your source file will be compiled with an additional -lz flag and run.

This question was first raised by @johntyree in #12.