Comparative study of gamma-ray emission from molecular clouds and star-forming galaxies