It's not possible with gcc but you might find tcc (an embeddable C compiler) good enough for what you have in mind. The distribution comes with a libtcc library that allows to compile, link and run C code "on the fly".

Note that this only for C, your question is also tagged C++ but I've not seen any equivalent of tcc for C++.