There is a well known result of Borel that will be useful here.
Borel's theorem on power series: Let $x_0\in \mathbb R.$ Given $a_0,a_1, \dots \in \mathbb R,$ there exists $f\in C^\infty(\mathbb R)$ such that
$$\tag 1 D^nf(x_0) = a_n, n=0,1,\dots.$$
This is stated here, with a sketch of the proof, at the very end: http://math.caltech.edu/~nets/lecture12.pdf. The statement there is not exactly $(1),$ but the two are clearly equivalent. (I'd refer you to the wikipedia page on this, but the author there gets all caught up in stating a very general version, which we don't need.)
Corollary: If $f\in C^\infty[a,b],$ then there exists $F\in C^\infty(\mathbb R)$ such that $F= f$ on $[a,b].$
Proof: By Borel, there exists $f_1 \in C^\infty(\mathbb R)$ such that
$$D^nf_1(a) = D^nf(a), n=0,1,\dots.$$
There also exists $f_2 \in C^\infty(\mathbb R)$ such that
$$D^nf_2(b) = D^nf(b), n=0,1,\dots.$$
Now define
$$F(x) = \begin{cases} f_1(x) ,\qquad x \le a \\ f(x) , \qquad x\in [a,b] \\ f_2(x) ,\qquad x \ge b \\
\end{cases}$$
This $F$ does the job: It equals $f$ on $[a,b],$ it's clearly in $C^\infty(\mathbb R\setminus \{a,b\}),$ and Borel gives us exactly the match-up at $a,b$ we need to see $F\in C^\infty(\mathbb R).$
So now to your set up with $f\in C^\infty([a,b]),g\in C^\infty([c,d]): $ We choose $F$ relative to $f,$ and $G$ relative to $g,$ as in the corollary. We also choose "blending" functions $\alpha, \beta \in C^\infty(\mathbb R)$ that do the following:
$$\alpha (x) = \begin{cases} 1, \qquad x\in (-\infty,b] \\ 0, \qquad x\in [c,\infty)\\
\end{cases}$$
$$\beta (x) = \begin{cases} 1, \qquad x\in [c,\infty) \\ 0, \qquad x\in (-\infty,b]\\
\end{cases}$$
Then $h=\alpha F + \beta G$ solves your problem with room to spare: It belongs to $C^\infty(\mathbb R),$ it equals $f$ on $[a,b],$ and equals $g$ on $[c,d].$