I wrote a .cpp full of functions and made a .h for it. How can I use header in other files without compiling the .cpp?
This is a discussion on Linking within the C++ Programming forums, part of the General Programming Boards category; I wrote a .cpp full of functions and made a .h for it. How can I use header in other ...
OK. I made a .lib
But there is a problem, linker generates an error and says you defined some vars in both files (.lib and the caller .cpp). And it is true, because there are some vars, const, etc defined in the header and the header was included in both .cpp and .lib.
What is the solution?
You DON'T define variables in the header, never ever. If you must have a reference toa variable there, declare it as extern:
X.cpp:Code:extern int Y;
Add "int Y" in one of the modules
Add "extern int Y" in all of the modules
There is only one solution then. Just use that header in only one compilation unit.Originally Posted by siavoshkc