Can cracked ribs cause a lung infection?

Indirectly. The pain from the rib injury keeps you from taking regular deep breaths and this prevents you from clearing the build-up of "stuff" in your lungs that normally accumulates. Bacteria multiply and give you pneumonia.
Not Directly. A cracked or broken rib can result in pain. Pain can result in the patient not taking deep breaths. In turn the small airsacs of the lung (known as alveoli) may hold onto secretions/mucous and pneumonia can occur.