Large ships typically rely on a gyrocompass, using the magnetic compass only as a backup.
Small compasses found in clocks, mobile phones, and other electronic devices are solid-state compasses, usually built out of two or three magnetic field sensors that provide data for a microprocessor.