The new design of the thermal elongation compensation mechanism eliminates the transient thermal error of the ram caused by the thermal deformation before the ram reached thermal equilibrium. Through test, measured machine tools after reaching heat balance of the temperature error of the spindle and the corresponding temperature field, and by using the least square method (LSM) fitting out the mathematical model between the error and temperature, the mathematical model was input in the numerical control (NC) system of machine tool for spindle steadystate thermal compensation, namely the temperature error compensation. The combination of these two kinds of thermal compensation way further enhances the machining precision of the machine tool, so as to ensure the flexible production line of computer numerical control (CNC) gantry of machining accuracy requirements of various parts.