This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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