Romonov
Romonov

Reputation: 8605

inheritance makefile C++ undefined symbol first referenced

I am having a compilation problem. The following are the files and the inheritance.

dateType.h, dateTypeImp.cpp: define and implement dateType class
addressType.h, addressTypeImp.cpp: define and implement addressType class
personType.h, personTypeImp.cpp: define and implement personType class
extPersonType.h, extPersonTypeImp.cpp: define and implement extPersonType class
addressBookType.h, addressBookTypeImp.cpp: define and implement addressBookType class

The following is the inheritance and has-the relationships.
extPersonType "extends"(inheritance) personType class and it also "has" one object of each addressType and dateType.
addressBookType class "has" 500 objects of type extPersonType.
(I can copy paste the .h files in case thats needed)
Hence extPersonType.h has #include statements for "dateType.h", "addressType.h", "personType.h" in it. addressBookType.h has #include "extPersonType.h" in it.

I have the following Makefile:

all: main



personTypeImp.o: personTypeImp.cpp
    g++ -c -Wall personTypeImp.cpp

dateTypeImp.o: dateTypeImp.cpp
    g++ -c -Wall dateTypeImp.cpp

addressTypeImp.o: addressTypeImp.cpp
    g++ -c -Wall addressTypeImp.cpp

addressBookTypeImp.o: addressBookTypeImp.cpp
    g++ -c -Wall addressBookTypeImp.cpp

main.o: main.cpp
    g++ -c -Wall main.cpp

main: main.o addressBookTypeImp.o addressTypeImp.o dateTypeImp.o personTypeImp.o
    g++ -Wall main.o addressBookTypeImp.o addressTypeImp.o dateTypeImp.o personTypeImp.o -o main


clean:
    rm -f *.o *~ main

All the .o lines are compiled without errors. The last "main" compilation gives this error:

make
g++ -c -Wall main.cpp
g++ -c -Wall addressBookTypeImp.cpp
g++ -c -Wall addressTypeImp.cpp
g++ -c -Wall dateTypeImp.cpp
g++ -c -Wall personTypeImp.cpp
g++ -Wall main.o addressBookTypeImp.o addressTypeImp.o dateTypeImp.o personTypeImp.o -o main
Undefined                       first referenced
 symbol                             in file
extPersonType::getStatus() const    addressBookTypeImp.o
extPersonType::isLastName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >) constaddressBookTypeImp.o

More functions from extPersonType are listed.

Wondering what could be the error.

Upvotes: 0

Views: 1785

Answers (2)

Andr&#233; Caron
Andr&#233; Caron

Reputation: 45239

The key part of the error message is:

Undefined symbol extPersonType::....

Basically, the symbols for class extPersonType are not part your program. If you look at the makefile, you'll see that

extPersonTypeImp.o: extPersonTypeImp.cpp
    g++ -c -Wall extPersonTypeImp.cpp

is missing and that you need to add extPersonType.o to the linker command as such

g++ -Wall main.o addressBookTypeImp.o addressTypeImp.o dateTypeImp.o personTypeImp.o extPersonTypeImp.o -o main

Upvotes: 3

Ben Voigt
Ben Voigt

Reputation: 283614

Well, have you actually written those functions somewhere, or simply declared them? If they do have bodies, in what file, and are they inline?

Upvotes: 2

Related Questions