Click to See Complete Forum and Search --> : c++ linking?


sans-hubris
03-28-2001, 11:33 PM
I feel kinda stupid for asking this (OTOH, I still consider myself a novice programmer), but how do I get g++ to link C++ objects? I have my class definitions in a .h (e.g. class.h) file, its member function definitions in a .cpp (class.cpp), and the main program file in another .cpp (main.cpp) file. I'll compile (but not link) class.cpp, then the same for main.cpp, but when I try to compile the executeble I'll get nasty linker messages saying the main program can't find the member functions defined in class.cpp. The only way to get it to work was to #include "class.cpp" in main.cpp, but that's not ideal. I'm not really sure why it doesn't work.

Carnel
03-29-2001, 12:46 AM
Basicly you'd do something like this when compiling. Depending on how many steps you want to take. But this is how I'd do it:

>g++ main.cpp class.cpp -o main

Or something very close to that. Play around with it until you get what you're looking for. Often what is done as well is either: compiler classes to object files (.o) and then add those files to the main.cpp when you're compiling it to an executable. Or compiler everything to an oject file and use the linker itself (ld)

sans-hubris
03-29-2001, 01:27 AM
That's what I'm doing.

Does it matter that the class I defined is a template class and that in main.cpp I typedefed it? Someone told me that it does, but I don't know why it would matter. I don't get errors except for in this situation.