免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2215 | 回复: 7
打印 上一主题 下一主题

gcc如何编译带有源文件的c++程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-03-12 14:26 |只看该作者 |倒序浏览
我在redhat 9.0下用gcc编译c++文件,有3个文件:hello.h hello.cc main.cc
其中我在main.cc中#include "hello.h",然后用gcc main.cc怎么也编译通不过是什么原因,可能是头文件和源文件没有和到一块,我想问一下各位谁能教我怎么才能编译这样的文件。

注:hello.h中定义了一个简单的类
    hello.cc中有#include "hello.h",然后对hello.h中的成员函数进行定义
如果我将hello.cc中的代码考到hello.h中就没有问题了

论坛徽章:
0
2 [报告]
发表于 2004-03-12 14:32 |只看该作者

gcc如何编译带有源文件的c++程序

能不能把你的命令行和出错信息贴出来

论坛徽章:
0
3 [报告]
发表于 2004-03-12 14:56 |只看该作者

gcc如何编译带有源文件的c++程序

先编译成目标文件,然后合并编译
gcc -c -o hello.o hello.cc
gcc -c -o main.o main.cc
gcc -o hello main.o hello.o

论坛徽章:
0
4 [报告]
发表于 2004-03-12 15:14 |只看该作者

gcc如何编译带有源文件的c++程序

/*-----------hello.h--------------*/
#include <stdio.h>;

class Testmath
{
public:
float pingjun(int aa,int bb,int cc);
int a;
int b;
int c;
};


/*-----------hello.cc--------------*/

#include "hello.h"

float Testmath::pingjun(int aa,int bb,int cc)
{
a=aa;
b=bb;
c=cc;
float avg;
avg=(float)(a+b+c)/3;
return avg;
}

/*-----------main.cc--------------*/

#include "hello.h"

main()
{
float num;
Testmath testmath;
num=testmath.pingjun(23,34,56);
printf("%f\n",num);
}

假如运行#gcc -o main main.cc
出现以下:/tmp/ccGe3L6y.o(.text+0x1b): In function `main':
: undefined reference to `Testmath::pingjun(int, int, int)'
/tmp/ccGe3L6y.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

假如运行#gcc -c hello.cc
            #gcc -c main.cc
            #gcc -o main hello.o main.o
回出现以下:
main.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

请各位帮忙,多谢!

论坛徽章:
0
5 [报告]
发表于 2004-03-12 15:15 |只看该作者

gcc如何编译带有源文件的c++程序

/*-----------hello.h--------------*/
#include <stdio.h>;

class Testmath
{
public:
float pingjun(int aa,int bb,int cc);
int a;
int b;
int c;
};


/*-----------hello.cc--------------*/

#include "hello.h"

float Testmath::pingjun(int aa,int bb,int cc)
{
a=aa;
b=bb;
c=cc;
float avg;
avg=(float)(a+b+c)/3;
return avg;
}

/*-----------main.cc--------------*/

#include "hello.h"

main()
{
float num;
Testmath testmath;
num=testmath.pingjun(23,34,56);
printf("%f\n",num);
}

假如运行#gcc -o main main.cc
出现以下:/tmp/ccGe3L6y.o(.text+0x1b): In function `main':
: undefined reference to `Testmath::pingjun(int, int, int)'
/tmp/ccGe3L6y.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

假如运行#gcc -c hello.cc
            #gcc -c main.cc
            #gcc -o main hello.o main.o
回出现以下:
main.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

请各位帮忙,多谢!

论坛徽章:
0
6 [报告]
发表于 2004-03-12 15:16 |只看该作者

gcc如何编译带有源文件的c++程序

/*-----------hello.h--------------*/
#include <stdio.h>;

class Testmath
{
public:
float pingjun(int aa,int bb,int cc);
int a;
int b;
int c;
};


/*-----------hello.cc--------------*/

#include "hello.h"

float Testmath::pingjun(int aa,int bb,int cc)
{
a=aa;
b=bb;
c=cc;
float avg;
avg=(float)(a+b+c)/3;
return avg;
}

/*-----------main.cc--------------*/

#include "hello.h"

main()
{
float num;
Testmath testmath;
num=testmath.pingjun(23,34,56);
printf("%f\n",num);
}

假如运行#gcc -o main main.cc
出现以下:/tmp/ccGe3L6y.o(.text+0x1b): In function `main':
: undefined reference to `Testmath::pingjun(int, int, int)'
/tmp/ccGe3L6y.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

假如运行#gcc -c hello.cc
            #gcc -c main.cc
            #gcc -o main hello.o main.o
回出现以下:
main.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

请各位帮忙,多谢!

论坛徽章:
0
7 [报告]
发表于 2006-05-13 13:11 |只看该作者

把个gcc换成g++

把个gcc换成g++

论坛徽章:
0
8 [报告]
发表于 2006-05-15 23:01 |只看该作者

回复 7楼 fzy8888cn 的帖子

好像只用g++不行,
我通过如下方式:
gcc -c hello.cc
gcc -c main.cc
产生hello.o和main.o文件
再用g++ -o main hello.o main.o生成main
即可得到结果37.667
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP