Sorry, I've misread your request. However, if editing a source file means, that only a single binary has to be updated my answer below is helpful. If your project has grown to consist of several binaries which depend on shared source code, this definitely sounds like you need a build tool and I will give you a quick start in my new answer:
Build Tool: CMake
If you do not like GNU Make, or the Makefiles in particular, consider CMake as tool to generate Makefiles for you. To make it more clear: vim is not a build tool!
A simple setup with CMake would be to create the file CMakeLists.txt
in your project folder
cmake_minimum_required (VERSION 3.0)
project (Tutorial)
add_executable(binaryname1 main1.cpp)
add_executable(binaryname2 main2.cpp)
...
add_executable(binarynameN mainN.cpp)
or if your binaries have the same name as your cpp files and they are located in the main folder, you can use
file( GLOB APP_SOURCES *.cpp )
foreach( testsourcefile ${APP_SOURCES} )
string( REPLACE ".cpp" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
endforeach( testsourcefile ${APP_SOURCES} )
Some build tools do not support wildcards to detect source files: http://mesonbuild.com/FAQ.html#why-cant-i-specify-target-files-with-a-wildcard
Create the Makefile with $ cmake .
. The created Makefile compiles all of your binaries with one invocation of $ make
or within vim with :make
.
And as a beginner you should be aware of the following: you do not need to re-run cmake as long as you do not change the file CMakeLists.txt
. For example, editing a shared source file in vim and simply calling :make
is sufficient to update all of your binaries. CMake has automatically detected the dependencies and creates a sensible Makefile, so that the Makefile targets will be considered as out-of-date by your file change.
Alternative build tools:
Old answer:
Plugin SingleCompile
If you do not mind using plugins, there are also several ones for the situation where the compilation is straightforward (no configuration of linking, includepaths, ...). One to consider is called SingleCompile and is available from http://singlecompile.topbug.net/ and on github. The suggested mappings are
nmap <F9> :SCCompile<cr>
nmap <F10> :SCCompileRun<cr>
Alternatives
If you like to run and interact with your compiled program see this discussion on reddit https://www.reddit.com/r/vim/comments/6vfrrz/plugin_to_compile_and_run_file/