I am trying to include the World Magnetic Model code, written in C, into an iOS app. It includes a 8.5M header file that defines a float array with lots of elements. If I include it as is, I get linker errors about duplicate objects, probably because the header is included more than once by different compilation units and the float array is defined in the header file. This is somewhat expected.
I’ve tried splitting that up into a h file and a c file and declaring the float array as extern, but that didn’t work.
Any ideas on how to solve this with as few modifications to the WMM code as possible?
Thanks
EDIT: This is how I tried to split the files up:
// EGM9615.h file
extern float GeoidHeightBuffer[];
// EGM9615.c file
#include "EGM9615.h"
float GeoidHeightBuffer[] =
{1.2, 1.2, // lots more
};
It results in linker errors still:
Undefined symbols for architecture armv7:
“__Z29WMM_TimelyModifyMagneticModel12WMMtype_DateP21WMMtype_MagneticModelS1_”,
referenced from:
-[Waypoint magneticVariation] in Waypoint.o “__Z23WMM_GeodeticToSpherical17WMMtype_Ellipsoid21WMMtype_CoordGeodeticP22WMMtype_CoordSpherical”,
referenced from:
-[Waypoint magneticVariation] in Waypoint.o “__Z26WMM_CalculateGridVariation21WMMtype_CoordGeodeticP27WMMtype_GeoMagneticElements”,
referenced from:
-[Waypoint magneticVariation] in Waypoint.o “__Z10WMM_Geomag17WMMtype_Ellipsoid22WMMtype_CoordSpherical21WMMtype_CoordGeodeticP21WMMtype_MagneticModelP27WMMtype_GeoMagneticElements”, referenced from:
-[Waypoint magneticVariation] in Waypoint.o “__Z23WMM_AllocateModelMemoryi”, referenced from:
-[Waypoint magneticVariation] in Waypoint.o “__Z22WMM_GeomagIntroductionP21WMMtype_MagneticModelPc”, referenced
from:
-[Waypoint magneticVariation] in Waypoint.o “__Z23WMM_robustReadMagModelsPcPP21WMMtype_MagneticModeli”, referenced
from:
-[Waypoint magneticVariation] in Waypoint.o “__Z9WMM_Errori”, referenced from:
-[Waypoint magneticVariation] in Waypoint.o “__Z15WMM_SetDefaultsP17WMMtype_EllipsoidP21WMMtype_MagneticModelP13WMMtype_Geoid”,
referenced from:
-[Waypoint magneticVariation] in Waypoint.o ld: symbol(s) not found for architecture armv7 clang: error: linker command failed with
exit code 1 (use -v to see invocation)
Then you did this incorrectly. This is the correct way to solve your problem. Take another look at what errors you saw when you split it up this way.
I am assuming here that when you say “duplicate objects” you mean duplicate identifiers at link time, not “objects.”
EDIT: Your failed symbols have nothing to do with
GeoidHeightBuffer. It looks like you’re not linking whatever code providesWMM_TimelyModifyMagneticModel()and its friends. These appear to be C++ name-mangled. Are you compiling C code with a C++ compiler without usingextern "C" {}?