当前位置: 技术问答>linux和unix
gcc如何编译带有源文件的c++程序
来源: 互联网 发布时间:2015-06-08
本文导语: /*-----------hello.h--------------*/ #include 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) { ...
/*-----------hello.h--------------*/
#include
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("%fn",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
请各位帮忙,多谢!
#include
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("%fn",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
请各位帮忙,多谢!
|
用g++
|
C++源代码要用g++编译。
|
gcc gnu c compiler
g++ gnu c++ compiler
g++ gnu c++ compiler
|
自己写一个makefile文件,里面加上:
.SUFFIXES: .cc
就可以了!
.SUFFIXES: .cc
就可以了!
|
g++ -o main main.o hello.o
gcc -c main.cc -o main.o
gcc -c hello.cc -o hello.o
gcc -c main.cc -o main.o
gcc -c hello.cc -o hello.o