Discussion:
Getting LNK2005 error when adding a file from another location (static MFC)
(too old to reply)
JiiPee
2015-09-11 08:36:55 UTC
Permalink
I have two projects and the other one is MFC. I want to include a cpp
and h files from the first project to the second. Two projects are in a
totally different folders. I though (and have done it before) that I can
just easily add files to the second project from the first project
location, but I do get this link error:

1>uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator
new(unsigned __int64)" (??***@YAPEAX_K@Z) already defined in
LIBCMT.lib(new_scalar.obj)
1>uafxcw.lib(afxmem.obj) : error LNK2005: "void __cdecl operator
delete(void *)" (??***@YAXPEAX@Z) already defined in
LIBCMT.lib(delete_scalar.obj)
1>fatal error LNK1169: one or more multiply defined symbols found

The funny thing is that it only happens if the files are in a folder
which is on a same level as the second solution folder, or further. But
if I move the folder inside the second solution folder and then add the
files everything works. Also it does not matter what is the content of
the files! Even if the cpp/h files are totally empty I still get this
error, which is strange. Does including files only work if they are
inside the solution folder ... I cannot include files from another
folder on my computer?

One more thing: if I change the project to "use shared MFC library" ,
then it compiles. I also tried to play around with "runtime library "
setting (\MD, \MT) but could not get working.

With static MFC linking I cannot take files from another location on my
computer?

thank you
JiiPee
2015-09-11 09:17:32 UTC
Permalink
Post by JiiPee
I have two projects and the other one is MFC. I want to include a cpp
and h files from the first project to the second. Two projects are in
a totally different folders. I though (and have done it before) that I
can just easily add files to the second project from the first project
1>uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator
LIBCMT.lib(new_scalar.obj)
1>uafxcw.lib(afxmem.obj) : error LNK2005: "void __cdecl operator
LIBCMT.lib(delete_scalar.obj)
1>fatal error LNK1169: one or more multiply defined symbols found
Well, yes indeed I got all the files compiling /linking after I copied
them under the *project* folder (even under solution folder was not
enough, got the same error there... had to move under project). I am
confused why this happens...
Woody
2015-09-12 05:39:34 UTC
Permalink
Usually this happens because you are linking the libraries in the wrong order. This comes up often with MFC. MS has a page explaining this; Google "LNK2005 already defined".

It's a pain to do it right, and not at all obvious why it happens.
JiiPee
2015-09-12 16:08:09 UTC
Permalink
Post by Woody
Usually this happens because you are linking the libraries in the wrong order. This comes up often with MFC. MS has a page explaining this; Google "LNK2005 already defined".
It's a pain to do it right, and not at all obvious why it happens.
thanks, good to know that it is a known problem (spent couple of hours
scraching my head :) ). Yes I found out it was not obvious...

I wish the compiler could do this order-thing automatically somehow...
less pain for us :).

Loading...