我如何解决这个问题';对'的多种定义;构建C程序时代码块出错?
我试图在代码块中构建这个项目,这样我就可以逐步完成其中一个函数,但是我在构建它时遇到了困难。这是我的错误我如何解决这个问题';对'的多种定义;构建C程序时代码块出错?,c,codeblocks,C,Codeblocks,我试图在代码块中构建这个项目,这样我就可以逐步完成其中一个函数,但是我在构建它时遇到了困难。这是我的错误 ||=== 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 def
||=== 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_子图书馆.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*/
#包括
#包括
#包括
#包括
#包括“GMHeader.h”
void GM_笛卡尔球面(GMtype_坐标笛卡尔坐标笛卡尔,GMtype_坐标球面*坐标球面)
{
/*此函数用于将点从笛卡尔坐标转换为球坐标*/
双X,Y,Z;
X=坐标笛卡尔坐标X;
Y=坐标笛卡尔坐标系Y;
Z=坐标笛卡尔坐标;
坐标球面->r=sqrt(X*X+Y*Y+Z*Z);
坐标球面->phig=RAD2DEG(asin(Z/(坐标球面->r));
坐标球面->λ=RAD2DEG(atan2(Y,X));
}/*通用汽车公司*/
///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;
}
//----------------------------------------------------------------------------------------
#包括
#包括
#包括
#包括
#包括“GM_SubLibrary.c”
//----------------------------------------------------------------------------------------
int main()
{
int标志=1;
char-ans[20];
GMU日期类型;
GMU类型数据G0、G1、H1;
GMU型坐标大地定位;
GMU型坐标定位;
GMU型椭球椭圆;
GM_扫描射频(G0、G1和H1);
GM_set椭球体(&Ellip);
while(标志==1){
GM_GetUserInput(位置和日期);
GM_线(位置、日期、椭圆、G0、G1、H1和GMlocation);
GM_PrintUserData(位置、日期、GMlocation);
...
}
返回1;
}
您将在GMCORD.C中包含C源文件gmu SubLibrary.C,而不是头文件GMHeader.h
请参阅:您将在GMCORD.C中包含C源文件gmu SubLibrary.C,而不是头文件GMHeader.h 请参阅:您不应该包含
#c文件,只包含标题。出现此错误是因为GMCORD.c
基本上已经包含了gmu SubLibrary.c
中的所有代码,因此如果在同一项目中编译这两个文件,则该代码将被定义两次您不应该只包含头文件。出现此错误是因为GMCORD.c
基本上已经包含了gmu SubLibrary.c
中的所有代码,因此如果在同一项目中编译这两个文件,则会得到两次定义的代码\include
指令将包含文件中的所有文本放入当前文件。如果您将GM_SubLibrary.c
编译为一个独立的模块(因为编译器注意到了该名称的.o
文件,所以您会这样做),那么最终相同的代码会被编译两次
当链接器试图找出要调用的函数时,它无法区分这两个定义。它不足以聪明地看到定义是相同的,只是你似乎给出了两组指令“当我调用函数X时,做Y”
您可能打算包含一个.h
文件。指令将包含文件中的所有文本放入当前文件。如果您将GM_SubLibrary.c
编译为一个独立的模块(因为编译器注意到了该名称的.o
文件,所以您会这样做),那么最终相同的代码会被编译两次
当链接器试图找出要调用的函数时,它无法区分这两个定义。它不足以聪明地看到定义是相同的,只是你似乎给出了两组指令“当我调用函数X时,做Y”
您可能想包含一个.h
文件。谢谢!!这解决了我的问题。我删除了include of.c文件,并将其替换为include of头文件和所有生成的文件,没有错误。谢谢!!这解决了我的问题。我删除了include of.c文件,并将其替换为头文件的include和所有生成的内容,没有任何错误。