0

I'm trying to Build this project in CodeBlocks so that I can step through one of the functions, but I'm having trouble building it. This is my error

||=== Build: Debug in MAGLAT (compiler: GNU GCC Compiler) ===|
obj\Debug\GMCORD.o||In function `GM_CartesianToSpherical':|
C:\Users\Guest\SkyDrive\temp\MAGLAT\MAGLAT\GM_SubLibrary.c|11|multiple definition of `GM_CartesianToSpherical'|
obj\Debug\GM_SubLibrary.o:C:\Users\Guest\SkyDrive\temp\MAGLAT\MAGLAT\GM_SubLibrary.c|11|first defined here|

//HEADER FILE

#ifndef GMHEADER_H
#define GMHEADER_H
#endif

#ifndef M_PI
#define M_PI    ((2)*(acos(0.0)))
#endif

#define GM_STARTYEAR    1900
#define RAD2DEG(rad)    ((rad)*(180.0L/M_PI))
#define DEG2RAD(deg)    ((deg)*(M_PI/180.0L))
#define ATanH(x)    (0.5 * log((1 + x) / (1 - x)))
#define MU_0        4*M_PI / 10000000
#define R_e     6.371 * 1000000

#define TRUE        ((int)1)
#define FALSE       ((int)0)


typedef struct {
            int Day;
            int Month;
            int Year;
            double DecimalYear;
            int DayNumber;
            } GMtype_Date;

typedef struct {
            double lambda;// longitude
            double phi; // geodetic latitude
            double HeightAboveEllipsoid; // height above the ellipsoid (HaE)
            } GMtype_CoordGeodetic;

typedef struct {
...
...
}...;

//GM Cord functions
void GM_CartesianToSpherical(GMtype_CoordCartesian CoordCartesian, GMtype_CoordSpherical *CoordSpherical);

///GM_SubLibrary.c

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#include "GMHeader.h"



void GM_CartesianToSpherical(GMtype_CoordCartesian CoordCartesian, GMtype_CoordSpherical *CoordSpherical)
{
    /*This function converts a point from Cartesian coordinates into spherical coordinates*/
    double X, Y, Z;

    X = CoordCartesian.x;
    Y = CoordCartesian.y;
    Z = CoordCartesian.z;

    CoordSpherical->r = sqrt(X * X + Y * Y + Z * Z);
    CoordSpherical->phig = RAD2DEG(asin(Z / (CoordSpherical->r)));
    CoordSpherical->lambda = RAD2DEG(atan2(Y, X));
} /*GM_CartesianToSpherical*/

///GMCORD.c

//----------------------------------------------------------------------------------------

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#include "GM_SubLibrary.c"


//----------------------------------------------------------------------------------------


int main()
{
    int Flag = 1;
    char ans[20];
    GMtype_Date date;
    GMtype_Data G0, G1, H1;
    GMtype_CoordGeodetic location;
    GMtype_CoordDipole GMlocation;
    GMtype_Ellipsoid Ellip;


    GM_ScanIGRF(&G0, &G1, &H1);
    GM_SetEllipsoid(&Ellip);
    while(Flag == 1) {
        GM_GetUserInput(&location, &date);
        GM_CORD(location, &date, Ellip, G0, G1, H1, &GMlocation);
        GM_PrintUserData(location, date, GMlocation);
    ...
    }
    return 1;
}
Floris
  • 45,857
  • 6
  • 70
  • 122
erotavlas
  • 4,274
  • 4
  • 45
  • 104

3 Answers3

2

You are including the C source file GM_SubLibrary.c in GMCORD.c instead of the header file GMHeader.h.

See: Including one C source file in another?

Community
  • 1
  • 1
diegog
  • 1,731
  • 9
  • 8
  • Thank you!! That solved my problem. I removed the include of .c file and replaced it with include of the header file and everything built with no errors. – erotavlas Feb 10 '14 at 00:33
1

You shouldn't #include c files, only headers. You get this error because your GMCORD.c essentially includes all the code from GM_SubLibrary.c already, so if you compile both files in the same project you get that code defined twice

Leeor
  • 19,260
  • 5
  • 56
  • 87
1

the #include directive puts all the text from the included file into the present file. If you compile GM_SubLibrary.c as a standalone module (which you do, since the compiler noticed the .o file of that name), then you end up with the same code being compiled twice.

When the linker tries to figure out what function to call, it can't tell the two definitions apart. It is not clever enough to see that the definition is identical, just that you appear to give two sets of instructions "when I call function X, do Y".

You probably meant to include a .h file instead.

Floris
  • 45,857
  • 6
  • 70
  • 122