2014-02-09 105 views
0

我正在尝试在CodeBlocks中构建此项目,以便可以逐步执行其中一个功能,但我在构建它时遇到了问题。这是我的错误如何在构建C程序时解决CodeBlocks中的'Multiple Definitions of'错误?

||=== 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| 

//头文件

#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; 
} 

回答

2

你是包括GMCORD.c中的C源文件GM_SubLibrary.c而不是th e头文件GMHeader.h。

参见:Including one C source file in another?

+0

谢谢!这解决了我的问题。我删除了包含.c文件,并将其替换为包含头文件以及没有错误构建的所有内容。 – erotavlas

1

你不应该#include C文件中,只有头。你得到这个错误,因为你GMCORD.c主要包括从GM_SubLibrary.c已经全部代码,所以如果你在同一个项目中编译两个文件你得到定义代码两次

1

#include指令将自包含的文件中的所有文本到本文件。如果你编译GM_SubLibrary.c作为独立模块(你这样做,因为编译器注意到这个名字的.o文件),那么你最终会编译两次相同的代码。

当链接器试图找出要调用的函数时,它无法区分这两个定义。看到这个定义是完全相同的,只是你看起来给出了两组指令,“当我调用函数X,做Y”时,它是不够聪明的。

您可能打算包含一个.h文件。

相关问题