I am using dev c++. also i am getting assistant from a library, developed by somebody else for my own c++ projects. i have added two new classes called TriangleList and TriangleLists to the library as i wanted new class types for the project. once, i added the hpp and cpp files to the library, i got the massage like creating ../lib/libExtraction.a .
however, when i called back the functions, that i written in the new classes, from my own project, i got the error massages like
`[Linker error] undefined reference to `TriangleLists::Erase()`
(the above one for the Erase function)
then, when i removed the .cpp file and wrote everything in .hpp it works.
But, i would like to maintain two files for the hpp and cpp further. so, please tell me how to solve this link error problem as i want to learn this. thank you in advance.
First thing you need to check is if you’re exporting the class:
When you include the header in a different project however, you need to specify that the class is imported, so:
This is usually achieved with preprocessor directives:
and only define
BUILDING_FIRST_PROJECTin your first project. That way, when building the first project, you’ll be exporting the class, and when you include the header in another project, you’re importing it.Second thing is that the other project must link to the
.libfile generated by the first project.The reason it works when you move the implementation to the header is that the method becomes
inline, so there’s no lookup for it in thelibfiles, as it’s definition is already known.