0

I am a newish programmer trying to figure out how to compile two separate .cpp files and .h file. From my google search so far, I have found that I need to enter something like:

gcc -o myprogram file1.cpp file2.cpp

Can someone expand on this?

3
  • 2
    Vim is an editor, not a compiler. Search for compiling multiple source files gcc. You probably want g++ instead. Commented Oct 23, 2018 at 13:37
  • So it would just be the same command with g++? I have been struggling with this for quite a while sadly. Commented Oct 23, 2018 at 13:39
  • 1
    You should rather use something to help you with it - GNU Make, CMake, Meson... It's rather inconvenient and restrictive to use a single g++ invocation for this. Commented Oct 23, 2018 at 13:43

1 Answer 1

3

For a temporary session (or in a possibly local .vimrc file), you can specify the builtin makeprg variable (see also :help makeprg)

set makeprg=g++\ -o\ myprogram\ file1.cpp\ file2.cpp

and invoke the compilation via :make. This will open up the quickfix window if you had any compilation errors or warnings, which can be handy.

Note, however, that this is nothing but a temporary hack for quick trial-and-error cycles. When these two files are meant to be something more serious, choose a build system and configure it accordingly. Then, adjust the makeprg variable again to invoke e.g. make (which is the default anyway), ninja, scons (in case you hate yourself) or whatever tool you chose.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.