Having trouble including my static library (which uses regex) in my MFC app
Hi guys, I have a static library in which I have included the boost regex header. This static library compiles fine with no errors. But then I want to use this static library in an MFC application I made. I keep getting the following linking error: error LNK2005: "public: static unsigned int __cdecl std::char_traits<unsigned short>::length(unsigned short const *)" (?length@?$char_traits@G@std@@SAIPBG@Z) already defined in msvcprtd.lib(MSVCP71D.dll) What is the problem and how can I solve it? Thanks -- This email is from Civica Pty Limited and it, together with any attachments, is confidential to the intended recipient(s) and the contents may be legally privileged or contain proprietary and private information. It is intended solely for the person to whom it is addressed. If you are not an intended recipient, you may not review, copy or distribute this email. If received in error, please notify the sender and delete the message from your system immediately. Any views or opinions expressed in this email and any files transmitted with it are those of the author only and may not necessarily reflect the views of Civica and do not create any legally binding rights or obligations whatsoever. Unless otherwise pre-agreed by exchange of hard copy documents signed by duly authorised representatives, contracts may not be concluded on behalf of Civica by email. Please note that neither Civica nor the sender accepts any responsibility for any viruses and it is your responsibility to scan the email and the attachments (if any). All email received and sent by Civica may be monitored to protect the business interests of Civica.
Martin Zeng wrote:
Hi guys,
I have a static library in which I have included the boost regex header. This static library compiles fine with no errors.
But then I want to use this static library in an MFC application I made.
I keep getting the following linking error:
error LNK2005: "public: static unsigned int __cdecl std::char_traits<unsigned short>::length(unsigned short const *)" (?length@?$char_traits@G@std@@SAIPBG@Z) already defined in msvcprtd.lib(MSVCP71D.dll)
What is the problem and how can I solve it?
Please see https://sourceforge.net/tracker/index.php?func=detail&aid=1470041&group_id=7586&atid=107586 HTH, John.
Thanks John, With regards to your comment: --------------------------------------------------------------- Apologies, but ignore everything in my last message: you can work around this quite easily by building the regex lib without /Zc:wchar_t, or by adding the line: template<> _CRTIMP2 std::size_t __cdecl char_traits<unsigned short>::length(unsigned short const*); to the end of the other specializations in usinstances.cpp (this is the fix that's going into cvs any minute now). ---------------------------------------------------------------- I cannot compile my MFC app in Multibyte, it must be in Unicode, also when I use wchar_t as built in type I get other link errors. So, I'm trying to attempt building the regex lib without the /Zc:wchar_t as you have suggested. But I'm not sure how to do this. Can you please help me? Thanks, Martin -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of John Maddock Sent: Monday, 7 May 2007 6:31 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Having trouble including my static library(whichuses regex) in my MFC app Martin Zeng wrote:
Hi guys,
I have a static library in which I have included the boost regex header. This static library compiles fine with no errors.
But then I want to use this static library in an MFC application I made.
I keep getting the following linking error:
error LNK2005: "public: static unsigned int __cdecl std::char_traits<unsigned short>::length(unsigned short const *)" (?length@?$char_traits@G@std@@SAIPBG@Z) already defined in msvcprtd.lib(MSVCP71D.dll)
What is the problem and how can I solve it?
Please see https://sourceforge.net/tracker/index.php?func=detail&aid=1470041&group_ id=7586&atid=107586 HTH, John. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users -- This email is from Civica Pty Limited and it, together with any attachments, is confidential to the intended recipient(s) and the contents may be legally privileged or contain proprietary and private information. It is intended solely for the person to whom it is addressed. If you are not an intended recipient, you may not review, copy or distribute this email. If received in error, please notify the sender and delete the message from your system immediately. Any views or opinions expressed in this email and any files transmitted with it are those of the author only and may not necessarily reflect the views of Civica and do not create any legally binding rights or obligations whatsoever. Unless otherwise pre-agreed by exchange of hard copy documents signed by duly authorised representatives, contracts may not be concluded on behalf of Civica by email. Please note that neither Civica nor the sender accepts any responsibility for any viruses and it is your responsibility to scan the email and the attachments (if any). All email received and sent by Civica may be monitored to protect the business interests of Civica.
Martin Zeng wrote:
I cannot compile my MFC app in Multibyte, it must be in Unicode, also when I use wchar_t as built in type I get other link errors. So, I'm trying to attempt building the regex lib without the /Zc:wchar_t as you have suggested. But I'm not sure how to do this. Can you please help me?
The regex lib is "just a bunch of source files", so create a new static lib project in your IDE, set the appropriate build options, and add the regex source (in libs/regex/src) to it. Unless you name the resulting lib the same way that our auto-linking code expects then you will need to turn off auto-linking support in your app by defining BOOST_REGEX_NO_LIB when building, then add a dependency to the static regex lib to your apps IDE project. HTH, John.
Unless you name the resulting lib the same way that our auto-linking code expects then you will need to turn off auto- linking support in your app by defining BOOST_REGEX_NO_LIB when building
Not sure I understand what the above means. Can you explain what the auto-linking code expect the name of the resulting lib to be? How do I know if I have it in a form that is expected by the auto-linking code? Thanks John, Martin -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of John Maddock Sent: Tuesday, 8 May 2007 6:47 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Having trouble including mystaticlibrary(whichuses regex) in my MFC app Martin Zeng wrote:
I cannot compile my MFC app in Multibyte, it must be in Unicode, also when I use wchar_t as built in type I get other link errors. So, I'm trying to attempt building the regex lib without the /Zc:wchar_t as you have suggested. But I'm not sure how to do this. Can you please help me?
The regex lib is "just a bunch of source files", so create a new static lib project in your IDE, set the appropriate build options, and add the regex source (in libs/regex/src) to it. Unless you name the resulting lib the same way that our auto-linking code expects then you will need to turn off auto-linking support in your app by defining BOOST_REGEX_NO_LIB when building, then add a dependency to the static regex lib to your apps IDE project. HTH, John. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users -- This email is from Civica Pty Limited and it, together with any attachments, is confidential to the intended recipient(s) and the contents may be legally privileged or contain proprietary and private information. It is intended solely for the person to whom it is addressed. If you are not an intended recipient, you may not review, copy or distribute this email. If received in error, please notify the sender and delete the message from your system immediately. Any views or opinions expressed in this email and any files transmitted with it are those of the author only and may not necessarily reflect the views of Civica and do not create any legally binding rights or obligations whatsoever. Unless otherwise pre-agreed by exchange of hard copy documents signed by duly authorised representatives, contracts may not be concluded on behalf of Civica by email. Please note that neither Civica nor the sender accepts any responsibility for any viruses and it is your responsibility to scan the email and the attachments (if any). All email received and sent by Civica may be monitored to protect the business interests of Civica.
Martin Zeng wrote:
Unless you name the resulting lib the same way that our auto-linking code expects then you will need to turn off auto- linking support in your app by defining BOOST_REGEX_NO_LIB when building
Not sure I understand what the above means. Can you explain what the auto-linking code expect the name of the resulting lib to be? How do I know if I have it in a form that is expected by the auto-linking code?
How our libraries are named is described in the getting started guide, but the easiest way may be to see what error message you get (it will tell you what library auto-linking is looking for), or as I say, just disable it, and add a dependency to the static library build in your applications project. John.
Hi John, Thanks for you help, and now my MFC is compiling without the link error. There are still some things I would still like to know. I noticed that just by including the libs/regex/src/winstances.cpp file is enough to get rid of the the error
LNK2005: "public: static unsigned int __cdecl std::char_traits<unsigned short>::length(unsigned short const *)" (?length@?$char_traits@G@std@@SAIPBG@Z) already defined in msvcprtd.lib(MSVCP71D.dll)
so I was just wondering is it ok just to add only the winstances.cpp to my source directory as opposed to adding the entire libs/regex/src directory? I ask this because my static library was created with using stdafx.h (precompiled header), so I had to copy over the winstances.cpp file into my project directory and put in #include "stdafx.h" at the top as opposed to just referring to the libs/regex/src/winstances.cpp file directly. I prefer not to do this to every single file in the libs/regex/src directory if I don't need to include them anyway. Also, what is the cause of this link error. Does it have anything to do with VC++ compiler and how it ignores inline directives in debug mode as this person seems to suggest: http://aspn.activestate.com/ASPN/Mail/Message/2509074 In my experience, it seems that by having: boost::wregex expression(L"[ab]"); will cause the link error to happen. Is there going to be a fix for this problem? Regards, Martin -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of John Maddock Sent: Tuesday, 8 May 2007 6:47 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Having trouble including mystaticlibrary(whichuses regex) in my MFC app Martin Zeng wrote:
I cannot compile my MFC app in Multibyte, it must be in Unicode, also when I use wchar_t as built in type I get other link errors. So, I'm trying to attempt building the regex lib without the /Zc:wchar_t as you have suggested. But I'm not sure how to do this. Can you please help me?
The regex lib is "just a bunch of source files", so create a new static lib project in your IDE, set the appropriate build options, and add the regex source (in libs/regex/src) to it. Unless you name the resulting lib the same way that our auto-linking code expects then you will need to turn off auto-linking support in your app by defining BOOST_REGEX_NO_LIB when building, then add a dependency to the static regex lib to your apps IDE project. HTH, John. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users -- This email is from Civica Pty Limited and it, together with any attachments, is confidential to the intended recipient(s) and the contents may be legally privileged or contain proprietary and private information. It is intended solely for the person to whom it is addressed. If you are not an intended recipient, you may not review, copy or distribute this email. If received in error, please notify the sender and delete the message from your system immediately. Any views or opinions expressed in this email and any files transmitted with it are those of the author only and may not necessarily reflect the views of Civica and do not create any legally binding rights or obligations whatsoever. Unless otherwise pre-agreed by exchange of hard copy documents signed by duly authorised representatives, contracts may not be concluded on behalf of Civica by email. Please note that neither Civica nor the sender accepts any responsibility for any viruses and it is your responsibility to scan the email and the attachments (if any). All email received and sent by Civica may be monitored to protect the business interests of Civica.
Martin Zeng wrote:
Hi John,
Thanks for you help, and now my MFC is compiling without the link error. There are still some things I would still like to know.
I noticed that just by including the libs/regex/src/winstances.cpp file is enough to get rid of the the error
LNK2005: "public: static unsigned int __cdecl std::char_traits<unsigned short>::length(unsigned short const *)" (?length@?$char_traits@G@std@@SAIPBG@Z) already defined in msvcprtd.lib(MSVCP71D.dll)
so I was just wondering is it ok just to add only the winstances.cpp to my source directory as opposed to adding the entire libs/regex/src directory?
Probably :-)
I ask this because my static library was created with using stdafx.h (precompiled header), so I had to copy over the winstances.cpp file into my project directory and put in #include "stdafx.h" at the top as opposed to just referring to the libs/regex/src/winstances.cpp file directly. I prefer not to do this to every single file in the libs/regex/src directory if I don't need to include them anyway.
Also, what is the cause of this link error. Does it have anything to do with VC++ compiler and how it ignores inline directives in debug mode as this person seems to suggest: http://aspn.activestate.com/ASPN/Mail/Message/2509074
In my experience, it seems that by having: boost::wregex expression(L"[ab]"); will cause the link error to happen. Is there going to be a fix for this problem?
I hope it's fixed for Boost-1.34, but it's hard to test every possible build combination, so it's hard to be 100% sure :-( The problem is caused by certain template instances being declared as extern in the std lib headers (they're instantiated in the C++ runtime). Unfortunately, the std lib headers only declare *either* the unsigned short or wchar_t versions of these as external, even though the std lib actually contains *both*. In order to be link-compatible with as many build options as possible Boost.Regex instantiates *both* unsigned short and wchar_t variants of it's templates, but then when you build with certain specific build options this causes a conflict with the std lib :-( Basically I've made life easier for 90% of users, at the expensense of the 10%. The problem could be solved reduced but not completely solved by not putting any template instances in the lib, but that would be at the expense of some code-bloat and much longer compile times - currently compiling with Boost.Regex placed in a pre-compiled header is more or less instantanious, and I'd like to keep that if at all possible. HTH, John.
participants (2)
-
John Maddock
-
Martin Zeng