+ -
当前位置:首页 → 问答吧 → 编译c++出了点问题,帮看下怎么回事

编译c++出了点问题,帮看下怎么回事

时间:2010-08-22

来源:互联网

我都把文件全部都编译成.o的啦,然后用下面的编译成最终的可执行文件
  1. g++ -o rzfy  ./server.o ./lib/function.o ./lib/instruction_test.o ./lib/policy.o ./lib/stptok.o ./class/app/application.o ./class/app/people_chat.o ./class/control/control.o ./class/data_deal/data_module.o ./class/data_deal/role_move.o ./class/data_deal/war.o -lpthread -lmemcached -lcurl -ljson -D__STRICT_ANSI__ -D_SVID_SOURCE -fno-stack-protector
复制代码
出现变量被重复定义
  1. g++ -o rzfy  ./server.o  -lpthread -lmemcached -lcurl -ljson -D__STRICT_ANSI__ -D_SVID_SOURCE -fno-stack-protector
复制代码
这样又发现有好多函数库和类没有被定义,大侠 要如何弄啊

作者: jd808   发布时间: 2010-08-22

看的人还挺多,怎么就没一个支声的呢

作者: jd808   发布时间: 2010-08-23

既然你的都了命令出来,为什么比把命令的执行结果也贴出来,只说“重复定义”或者“没定义”,也就只能呮一声



再呮一声



作者: drangon   发布时间: 2010-08-23

应该是把变量定义放到头文件了

作者: hellioncu   发布时间: 2010-08-23

回复 hellioncu

应该是声明放头文件吧

作者: cstdio   发布时间: 2010-08-23

  1. [root@test src]# make
  2. g++  -g -O2  -lpthread -lmemcached -lcurl -ljson -D__STRICT_ANSI__ -D_SVID_SOURCE -fno-stack-protector -o rzfy  server.o function.o instruction_test.o policy.o stptok.o application.o people_chat.o control.o data_module.o monster.o role_move.o war.o  
  3. function.o: In function `patoca(char*, char*)':
  4. /www/game/server++/src/lib/function.cpp:1020: multiple definition of `gag'
  5. server.o:/www/game/server++/src/class/control/../../class/data_deal/../../class/app/application.h:26: first defined here
  6. function.o: In function `patoca(char*, char*)':
  7. /www/game/server++/src/lib/function.cpp:1020: multiple definition of `ics'
  8. server.o:/www/game/server++/src/class/control/../../class/data_deal/../../class/app/application.h:26: first defined here
  9. function.o: In function `user_on(int, ARG*)':
  10. /www/game/server++/src/lib/function.cpp:499: multiple definition of `EXECUTION_PATH'
  11. server.o:/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iostream:76: first defined here
  12. function.o: In function `user_on(int, ARG*)':
  13. /www/game/server++/src/lib/function.cpp:493: multiple definition of `ev'
  14. server.o:/www/game/server++/src/server.cpp:489: first defined here
  15. function.o: In function `user_on(int, ARG*)':
  16. /www/game/server++/src/lib/function.cpp:493: multiple definition of `epfd'
复制代码
错误比较多 特出来一部分
  1. /**
  2. * 指针字符串数组转字符串数组
  3. *
  4. * @param char* arr        指针字符串数组
  5. * @param char *returnarr 返回数组 char returnarr[50];
  6. */
  7. void patoca(char *arr,char *returnarr)  //这里是functions的1020行
  8. {
  9.     int i;
  10.     for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  11.     {
  12.         if(arr[i])
  13.         {
  14.             returnarr[i]=arr[i];
  15.         }
  16.     }
  17. }
复制代码
  1. int user_on(int user_id,struct ARG *arg)//这里是function文件的493行
  2. {
  3.     if(!user_id){return 0;}
  4.     int i;
  5.     for (i=0;i<arg->config.ONLINE;i++)
  6.     {
  7.         if(arg->connfd[i][6]==user_id)//这里是function文件的499行
  8.         {
  9.             return 1;
  10.         }
  11.     }
  12.     return 0;
  13. }
复制代码

作者: jd808   发布时间: 2010-08-23

本帖最后由 jd808 于 2010-08-23 16:40 编辑

我的变量和声明全部在头文件就是.H文件里的
比如policy.h文件如下
  1. #ifndef POLICY_H_
  2. #define POLICY_H_

  3. #ifndef FUNCTION_H_
  4. #include "lib/function.h"
  5. #endif

  6. void policy(void);
  7. void sendpolicy(int connectfd);
  8. #endif
复制代码
比如stptok.h文件如下
  1. #ifndef STPTOK_H_
  2. #define STPTOK_H_

  3. #ifndef FUNCTION_H_
  4. #include "lib/function.h"
  5. #endif

  6. int iscc(unsigned char ch);
  7. static int cc1(unsigned char *bp,unsigned char *bufp);
  8. int firstcc(unsigned char *bp,unsigned char *bufp);
  9. int secondcc(unsigned char *bp,unsigned char *bufp);
  10. char *stptok(char *src,char *trg,int len,char *tok);
  11. #endif
复制代码
程序全部在.CPP文件里 cpp文件值引入他本身的.H文件

还有一点 我的所有变量和常量以及要引入的外部库的头文件均在 function.h里头定义的,所以很多文件都引入了这个文件

作者: jd808   发布时间: 2010-08-23

貌似几个变量/常量重定义了。是不是#include了.c文件,或者在.h文件中定义了数据,而不是声明

作者: davelv   发布时间: 2010-08-23

嗯 是从定义了但找不到原因,按里说我引入文件的时候都用宏判断了下这个文件是否被引入,难道内编译成.O文件之后,在用.o文件变成可执行文件这个过程中,那个判断是否被引入的宏不起作用了?

作者: jd808   发布时间: 2010-08-23

你的function.h文件里面定义了什么东西。

作者: davelv   发布时间: 2010-08-23

gag ics 等是什么咚咚?

/www/game/server++/src/class/control/../../class/data_deal/../../class/app/application.h 文件里面内容如何?

初步猜测,application.h 文件中定义了变量 int gag = 1; 然后多次应用后就重复定义了。
头文件中改成声明 extern int gag; 然后找某个cpp文件放定义 int gag = 1;

作者: drangon   发布时间: 2010-08-23



QUOTE:
gag ics 等是什么咚咚?

/www/game/server++/src/class/control/../../class/data_deal/../../class/app ...
drangon 发表于 2010-08-23 16:57


赞同。。。

作者: davelv   发布时间: 2010-08-23