They didn’t use a gearbox to drive both shafts. If you wanted to run both props off a single diesel engine, they would run electric current from the operational diesel to both electric motors. The electric motors would then turn both props.
Marius Dornean has a very detailed explanation of uboat engine operations here:
Uboat drivetrain
American boats used a similar system called “diesel-electric drive”: US Boats.
Germany’s Elefant tank used a similar system (diesel engines creating electric current that went to electric motors that turned the drive sprockets). It eliminates the need for a heavy-duty drive shaft and transmission. It also has torque advantages.