A formulation of the virial theorem in general relativity