We present a novel penalty function named SLEP for optimization problems over the generalized Stiefel manifold (OCP). The proposed penalty function SLEP has the same order of differentiability as the objective function of OCP, hence the gradients and hessians of SLEP can be easily computed from the gradients and hessians of the objective function. Moreover, we prove that SLEP is an exact penalty function in the sense that SLEP and OCP have the same first-order and second-order stationary points in a neighborhood of any point on the generalized Stiefel manifold, for a sufficiently large but finite penalty parameter. Based on the smoothness and exactness of SLEP, we can directly employ various existing unconstrained optimization algorithms to efficiently solve OCP through SLEP. Compared with existing Riemannian optimization algorithms, we show that employing unconstrained optimization algorithms to SLEP can enjoy lower per-iteration computational costs. Extensive numerical experiments are performed to show that our proposed penalty function SLEP enables efficient implementation of gradient method with Barzilai-Borwein (BB) stepsizes, which exhibits superior performance when compared with existing state-of-the-art Riemannian gradient methods with BB stepsizes. These results demonstrate the great potential of our proposed penalty function.