🔒 Locked
Linking errors
Advertisement
Original Post
I have never written large programs in C++ before, so I''m having trouble getting used to keeping programs in several files. I''m having a weird linking error. I defined a class in one file. I am including this file in the main file. I am getting the following linking error:
MyFileName.obj : error LNK2005: "public: __thiscall MyClassName::MyClassName(MyArgumentList)" (??0MyClassName@@QAE@PAUHINSTANCE__@@PAUHWND__@@@Z) already defined in Input.obj
This is obviously for my constructor (MyClassName::MyClassName). It comes up with similar messages for my other member functions. Then, it comes up with:
Debug/MyFileName.exe : fatal error LNK1169: one or more multiply defined symbols found
It seems that I am defining my classes functions more than once, but I''m not.
For some reason, I got it to work the first couple of times using strange methods. I commented out all of the code (determined to find out the problem) of the class, and since it wasn''t called anywhere, it compiled and ran fine. Then, when I uncommented it, it still worked. However, when I did a major change such as add a function or something, it came up with the same errors and I had to do it again. Not only did this become a hassle (having to comment out all calls the the class, too), it also seems rediculous and odd.
I feel like this is some dumb problem, but I guess I''m dumb and please help me.
Thank you.
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
Without seeing the code, I can only give you a few pointers (haha get it? pointers! ok nevermind
) -
1. Always use multiple inclusion protection in your header files.
2. Don''t put implementation into header files (unless it''s a small function within the definition itself; then it''s ok).
I can elaborate if you need, but I have a broken wrist and am stuck typing with one hand
1. Always use multiple inclusion protection in your header files.
2. Don''t put implementation into header files (unless it''s a small function within the definition itself; then it''s ok).
I can elaborate if you need, but I have a broken wrist and am stuck typing with one hand
I haven''t protected against double declaration, but its a small enough program to tell that no headers are doubly included. What''s the problem with having function definitions in headers? Is one supposed to put all implementation into one file?
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
Aiiight. Here''s the gist.
In your header file...
In your cpp file....
In your main cpp file...
That wasn''t too clear. I think that should explain it, but if you need more, just ask.
Later,
ZE.
In your header file...
|
In your cpp file....
|
In your main cpp file...
|
That wasn''t too clear. I think that should explain it, but if you need more, just ask.
Later,
ZE.
ZealousElixir''s example is absolutely correct, doing it any other way is just asking for trouble.
The .cpp gets included as long as it shares the same name as the .h file - it''s how the compiler works (through the makefile).
As for multiple inclusion protection of header is concerned, it''s good idea to put that in even if it''s for a tiny program because
1) You''ll never know if someday down the road you''ll want to expand that header file into a bigger project
2) It a good habit to have. It prevents some silly errors that may take any programmers days to find if s/he is not thinking about it.
The .cpp gets included as long as it shares the same name as the .h file - it''s how the compiler works (through the makefile).
As for multiple inclusion protection of header is concerned, it''s good idea to put that in even if it''s for a tiny program because
1) You''ll never know if someday down the road you''ll want to expand that header file into a bigger project
2) It a good habit to have. It prevents some silly errors that may take any programmers days to find if s/he is not thinking about it.
If you use a relatively new compiler, such as VC++, or anything that has an windows interface, you can usually add the .cpp file to the project. Then it will automatically compile it and link it in.
If you have an dos based, commandline compiler, then you should get an makefile system. Or, you can do this (let''s assume you went and downloaded the free borland compiler):
bcc32 mainfile.cpp classimp.cpp
The whole point of makefiles is to automate stuff like that... it gets tedious once you have around 10 files...
but that''s getting off topic.
If you have an dos based, commandline compiler, then you should get an makefile system. Or, you can do this (let''s assume you went and downloaded the free borland compiler):
bcc32 mainfile.cpp classimp.cpp
The whole point of makefiles is to automate stuff like that... it gets tedious once you have around 10 files...
but that''s getting off topic.
As a touch of clarification, whenever a cpp file is included in your project (in MSVC, as you are), it will be compiled and linked, unless you use the file settings to exclude it from the build.
Hope that helps,
ZE.
Hope that helps,
ZE.
Thanks, I finally got around to trying it and it worked
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
Oh no! Now I''m having new problems. That worked for all my classes, but now I''m having trouble with global functions! I put a function in a .cpp file, which included a same-named .h file, which was also included in the main .cpp file. It came up with a whole bunch of compile errors, stuff about not recognizing the types of its arguments (which are classes defined elsewhere in the project), when these classes are clearly included in the project before the function!
What''s wrong?
Thanks again,
Shalom
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
What''s wrong?
Thanks again,
Shalom
In my thunking device, what happens to the inherited pointer to the original base class when I override the function & how do I access it in my inline assembly code, assuming that we are referencing the higher byte of the 16-bit variable?
Actually, I''ve had similar problems with global variables. Except that I always guard against multiple inclusion
Yet I''d still get multiple definition linker errors. The worst part was, it wasn''t really consistent. I''d build, get the error, then recompile a file and build again, and no error. The linker would run, and the executable was fine. Build again (no code changes), and I get the linker error again. Very strange, and I never really figured out what my problem was. I just sortof cheated by declaring it in my main source file and then adding an extern reference to it in any other source files that needed to use it. Hackish, but it worked.
-Lutrosis
-Lutrosis
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement