2014-02-09 2 views
0

나는 하나의 기능을 단계별로 실행할 수 있도록이 프로젝트를 CodeBlocks로 빌드하려고하는데, 빌드하는데 문제가있다. 이것은 내 오류가C 프로그램을 빌드 할 때 CodeBlocks에서 'Multiple Definitions'오류를 어떻게 해결합니까?

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

답변

2

당신은 th 대신 GMCORD.c의 C 소스 파일 GM_SubLibrary.c 포함 e 헤더 파일 GMHeader.h.

참조 : Including one C source file in another?

+0

감사합니다. 그게 내 문제를 해결해 줬어. .c 파일의 인클루드를 삭제 해, 헤더 파일의 인클루드 및 에러없이 구축 된 모든 것을 포함하도록했습니다. – erotavlas

1

당신은해야하지 #include C 파일 머리글 만. 당신의 GMCORD.c은 본질적으로 이미 GM_SubLibrary.c에서 모든 코드가 포함되어 있기 때문에이 오류가 발생, 그래서 당신은 동일한 프로젝트에서 두 파일을 컴파일하는 경우 해당 코드는 #include 지시어는 현재에 모든 텍스트에 포함 된 파일에서두고

1

두 번 정의 얻을 파일. GM_SubLibrary.c을 독립 실행 형 모듈로 컴파일하면 (컴파일러가 .o 파일에이 이름을 보았 기 때문에) 동일한 코드가 두 번 컴파일되도록합니다.

링커가 호출 할 함수를 알아 내려고 시도 할 때 두 정의를 구분할 수 없습니다. 정의가 동일하다는 것을 알기에는 충분히 똑똑하지 않습니다. 단지 "함수 X를 호출 할 때 Y"라고하는 두 가지 명령어 세트를 제공하는 것처럼 보입니다.

아마 .h 파일을 포함해야합니다.

관련 문제