it's a very basic problem. I've got those files:
And as you can see, I get error about undefined reference. Ok I know that in main.cpp I can include MyClass.cpp instead of MyClass.h and it will work, but I am pretty shure that it is not proper :/ I would like it to work exactly as string class. So after including iostream, we can use std::string without any errors regarding undefined reference. We also don't need to write: class string; before main().
So could you tell me how do you do it?
Those are on the default library path and are linked automatically.
For your information, you can compile each source file separately. You'll need to use the -c flag for that, and the extension is usually .o. For instance:
g++ -c MyClass.cpp -o MyClass.o
g++ -c main.cpp -o main.o
g++ MyClass.o main.o -o main The first two lines compile the source files into object files. The third line then links these two object files (and any libraries on the library path) into the executable.
Joined: Jan 03, 2011
Thanks again for your explanation! That really helped