当前位置: 技术问答>linux和unix
用gcc怎么进行c++编程?
来源: 互联网 发布时间:2015-07-22
本文导语: 1. bbb.h #include class myclass { public : myclass(); ~myclass(); void mmm(); }; 2. bbb.cxx #include "bbb.h" myclass::myclass() { } myclass::~myclass() { } void myclass::mmm() { printf("aaaaa"); } 3. main.cxx #include ...
1. bbb.h
#include
class myclass
{
public :
myclass();
~myclass();
void mmm();
};
2. bbb.cxx
#include "bbb.h"
myclass::myclass()
{
}
myclass::~myclass()
{
}
void myclass::mmm()
{
printf("aaaaa");
}
3. main.cxx
#include
#include "bbb.h"
int main()
{
myclass * aaa;
aaa = new myclass();
aaa->mmm();
delete[] aaa;
return 0;
}
4. makefile
test: main.o bbb.o
gcc -o test main.o bbb.o
main.o: main.cxx
gcc -c main.cxx
bbb.o: bbb.cxx bbb.h
gcc -c bbb.cxx
5.make
结果:
gcc -c main.cxx (通过)
gcc -c bbb.cxx (通过)
gcc -o test main.o bbb.o
main.o(.text+0x16): In function `main':
: undefined reference to `operator new(unsigned)'
main.o(.text+0x54): In function `main':
: undefined reference to `operator delete(void*)'
main.o(.text+0x79): In function `main':
: undefined reference to `operator delete(void*)'
main.o(.text+0xcc): In function `main':
: undefined reference to `operator delete[](void*)'
main.o(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [test] Error 1
why?
#include
class myclass
{
public :
myclass();
~myclass();
void mmm();
};
2. bbb.cxx
#include "bbb.h"
myclass::myclass()
{
}
myclass::~myclass()
{
}
void myclass::mmm()
{
printf("aaaaa");
}
3. main.cxx
#include
#include "bbb.h"
int main()
{
myclass * aaa;
aaa = new myclass();
aaa->mmm();
delete[] aaa;
return 0;
}
4. makefile
test: main.o bbb.o
gcc -o test main.o bbb.o
main.o: main.cxx
gcc -c main.cxx
bbb.o: bbb.cxx bbb.h
gcc -c bbb.cxx
5.make
结果:
gcc -c main.cxx (通过)
gcc -c bbb.cxx (通过)
gcc -o test main.o bbb.o
main.o(.text+0x16): In function `main':
: undefined reference to `operator new(unsigned)'
main.o(.text+0x54): In function `main':
: undefined reference to `operator delete(void*)'
main.o(.text+0x79): In function `main':
: undefined reference to `operator delete(void*)'
main.o(.text+0xcc): In function `main':
: undefined reference to `operator delete[](void*)'
main.o(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [test] Error 1
why?
|
把gcc改成g++编译