Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I using gcc wrong?

Tags:

c++

gcc

I've been having no end of trouble just getting started with C++. Seems like no matter what I do, I've got dozens of errors. I've taken to compiling example code from tutorials, but even the example code doesn't compile. For example, the code at: http://www.cppgameprogramming.com/cgi/nav.cgi?page=arrayclasses

gives me lots of flack when I try to:

$ g++ main.cpp -o dog

It says:

> main.cpp: In function 'int main()': main.cpp:18:15: warning: deleting
> array 'Dog myDogs [5]'
> C:\Users\bob-pc\Appdata\Local\Temp\ccTXXCOB.o:main.cpp:(.text+0x21):
> undefined rference to 'Dog::Dog()' C:\Users\bob-pc\Appdata\Local\Temp\ccTXXCOB.o:main.cpp:(.text+0x59):
> undefined rference to 'Dog::setAge(int)' C:\Users\bob-pc\Appdata\Local\Temp\ccTXXCOB.o:main.cpp:(.text+0x88):
> undefined rference to 'Dog::getAge()' collect2: Id returned 1 exit status

This isn't the first time I've tried straight copying example code & compiling it. I'm having lots of trouble even after I go over syntax, but I just can't seem to get headers & libraries working right. Any help much appreciated.

like image 297
Jack Avatar asked Apr 23 '26 19:04

Jack


1 Answers

You have to include dog.cpp in the compilation:

g++ main.cpp dog.cpp -o dog

Alternatively, you can compile dog.cpp by itself to an object file, and include that in the final compilation:

g++ dog.cpp -c -o dog.o

g++ main.cpp dog.o -o dog
like image 71
meagar Avatar answered Apr 25 '26 10:04

meagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!