Compasses used in vehicles need to be calibrated to null the magnetic mass of the vehicle. This is true for airplanes and ships but also cars, although cheap car compasses often settle for poor accuracy to avoid this hassle. This whole issue is why the compass in airplanes is usually mounted in an odd place like above the glare shield or below the roof, so that it's farther from the other instruments to make the magnetic field situation simpler.
One of the earliest methods to do this has the amusing nickname of "Nelson's Balls," the binnacle (compass holder) on ships often has two large metal spheres mounted on its sides that serve to compensate for the fore-aft magnetic mass of the all the hardware in the ship.
The magnetic compass in an airplane is subject to a certain amount of variance due to installation (essentially calibration error), but that error should be quite small. More problematically, airplanes move fast enough that the magnetic compass shows errors due to the math of magnetic flux when you're moving relative to a magnetic field. The theory is somewhat complicated but pilots often remember it with acronyms like "ANDS" for accelerate north/decelerate south - basically, if there's any appreciable east-west component of your flight path, when you accelerate your compass will turn towards the north, and when you decelerate it will turn towards the south.
This is why airplanes are equipped with a heading indicator, also called a directional gyro, which uses a gyroscope to simulate a compass. The heading indicator isn't subject to the weird moving errors that the magnetic compass shows so it's more useful for maneuvering. On the other hand, the gyro does slowly drift over time, so every once in a while it needs to be reset against the magnetic compass - during straight and level flight to avoid moving errors.
One of the earliest methods to do this has the amusing nickname of "Nelson's Balls," the binnacle (compass holder) on ships often has two large metal spheres mounted on its sides that serve to compensate for the fore-aft magnetic mass of the all the hardware in the ship.
The magnetic compass in an airplane is subject to a certain amount of variance due to installation (essentially calibration error), but that error should be quite small. More problematically, airplanes move fast enough that the magnetic compass shows errors due to the math of magnetic flux when you're moving relative to a magnetic field. The theory is somewhat complicated but pilots often remember it with acronyms like "ANDS" for accelerate north/decelerate south - basically, if there's any appreciable east-west component of your flight path, when you accelerate your compass will turn towards the north, and when you decelerate it will turn towards the south.
This is why airplanes are equipped with a heading indicator, also called a directional gyro, which uses a gyroscope to simulate a compass. The heading indicator isn't subject to the weird moving errors that the magnetic compass shows so it's more useful for maneuvering. On the other hand, the gyro does slowly drift over time, so every once in a while it needs to be reset against the magnetic compass - during straight and level flight to avoid moving errors.