Mixing native C++ and C++/CLI - c++

I have a solution which consists of a native C++ DLL and a C++/CLI wrapper. My aim is to create a wrapper in C++/CLI of the native C++ DLL.
Whenever I try to create an instance of a native C++ class within the wrapper, I am met with many linker errors (see below).
2> .NETFramework,Version=v4.0.AssemblyAttributes.cpp
2>NFileOperation.obj : error LNK2028: unresolved token (0A000208) "public: static bool __cdecl CFileOperation::FileExists(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?FileExists#CFileOperation##$$FSA_NABV?$CStringT#_WV?$StrTraitATL#_WV?$ChTraitsCRT#_W#ATL###ATL###ATL###Z) referenced in function "[T2M] void __clrcall `dynamic initializer for 'public: static float * tagVARIANT::* ATL::CVarTypeInfo<float *>::pmField''(void)" (__t2m#???__E?pmField#?$CVarTypeInfo#PAM#ATL##2QQtagVARIANT##PAMQ3###YMXXZ#?A0x22b777aa##YMXXZ)
2>DeskUpdateManaged.obj : error LNK2028: unresolved token (0A00021C) "public: static bool __cdecl CFileOperation::FileExists(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?FileExists#CFileOperation##$$FSA_NABV?$CStringT#_WV?$StrTraitATL#_WV?$ChTraitsCRT#_W#ATL###ATL###ATL###Z) referenced in function "public: bool __clrcall DeskUpdateManaged::Conversion::FileExist(class System::String ^)" (?FileExist#Conversion#DeskUpdateManaged##$$FQ$AAM_NP$AAVString#System###Z)
2>DeskUpdateManaged.obj : error LNK2019: unresolved external symbol "public: static bool __cdecl CFileOperation::FileExists(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?FileExists#CFileOperation##$$FSA_NABV?$CStringT#_WV?$StrTraitATL#_WV?$ChTraitsCRT#_W#ATL###ATL###ATL###Z) referenced in function "public: bool __clrcall DeskUpdateManaged::Conversion::FileExist(class System::String ^)" (?FileExist#Conversion#DeskUpdateManaged##$$FQ$AAM_NP$AAVString#System###Z)
2>NFileOperation.obj : error LNK2001: unresolved external symbol "public: static bool __cdecl CFileOperation::FileExists(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?FileExists#CFileOperation##$$FSA_NABV?$CStringT#_WV?$StrTraitATL#_WV?$ChTraitsCRT#_W#ATL###ATL###ATL###Z)
2>C:\Users\ABGZAMANK\Music\DeskUpdate\Dev\Source\Solution\Debug\DeskUpdateManaged.dll : fatal error LNK1120: 3 unresolved externals
Function call within the C++/CLI to the native C++ DLL:
bool NFileOperation::FileExists(CAtlString sPathName)
{
return CFileOperation::FileExists(sPathName);
}
Is there a more adequate approach to what I am trying to achieve?
Any advice relating to the issue is much appreciated.

It seems that your native C++ code is using ATL/MFC. Assuming you are compiling under VS, go to the C++/CLI project's project properties->Configuration properties->General and select "Use of MFC" and "Use of ATL" to be either static library or shared dll (depending on your project type). This flag will add the necessary include paths, linking paths and libs to your project to make use of ATL/MFC types from within your project. Also, make sure that you are linking your C++/CLI project against your native C++ project to avoid linking errors from your project (easily found in the Linker segment of the project properties in General and Input).

Related

Error building c++ project with ppconsul(Consul client for c++)

I'm a beginner to cpp and trying to use a cpp library called ppconsul(https://github.com/oliora/ppconsul ). This library has dependency to some of other libraries like (boost and curl). I have build ppconsul with all its dependencies and trying to use ppconsul executable in a new project. When I try to build the new project, I'm getting some linker issue like below.
error LNK2001: unresolved external symbol "public: virtual char const * thiscall ppconsul::BadStatus::what(void)const " (?what#BadStatus#ppconsul##UBEPBDXZ)
1>ConsulApp.obj : error LNK2019: unresolved external symbol "class std::basic_string,class std::allocator > __cdecl ppconsul::helpers::encodeUrl(class std::basic_string,class std::allocator > const &)" (?encodeUrl#helpers#ppconsul##YA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##ABV34##Z) referenced in function "void __cdecl ppconsul::parameters::detail::printParameter(class std::basic_ostream > &,class std::basic_string,class std::allocator > const &,struct ppconsul::kw::dc_keyword)" (??$printParameter#Udc_keyword__#kw#ppconsul###detail#parameters#ppconsul##YAXAAV?$basic_ostream#DU?$char_traits#D#std###std##ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##4#Udc_keyword__#kw#2##Z)
I tried to link the project and change the visual studio but nothing worked. Anybody has any idea about this linker issue?
TIA,
Prassi
Issue resolved- Refer this thread github.com/oliora/ppconsul/issues/24
build ppconsul as a static library by passing -DBUILD_STATIC_LIB=ON to CMake.

Boost-log linker error

i've downloaded and builded Boost 1.55 from source. I'm trying to use Boost-log. In basic case (http://boost-log.sourceforge.net/libs/log/doc/html)/log/tutorial.html#log.tutorial.trivial it's working fine without any problems, but when i try to use some more advanced features(like this - http://boost-log.sourceforge.net/libs/log/example/doc/tutorial_fmt_stream.cpp ) i'm getting this error:
main.obj : error LNK2019: unresolved external symbol "void __cdecl
boost::filesystem::path_traits::convert(char const *,char const
*,class std::basic_string,class std::allocator>
&,class std::codecvt const &)" (?convert#path_traits#filesystem#boost##YAXPBD0AAV?$basic_string#GU?$char_traits#G#std##V?$allocator#G#2##std##ABV?$codecvt#GDH#5##Z)
referenced in function "void __cdecl
boost::filesystem::path_traits::dispatch,class std::allocator > >(class
std::basic_string,class
std::allocator > const &,class std::basic_string,class
std::allocator > &,class std::codecvt const &)"
(??$dispatch#V?$basic_string#GU?$char_traits#G#std##V?$allocator#G#2##std###path_traits#filesystem#boost##YAXABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##AAV?$basic_string#GU?$char_traits#G#std##V?$allocator#G#2##4#ABV?$codecvt#GDH#4##Z)
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl
boost::log::v2s_mt_nt5::aux::code_convert(unsigned short const
*,unsigned int,class std::basic_string,class std::allocator > &,class
std::locale const &)"
(?code_convert#aux#v2s_mt_nt5#log#boost##YAXPBGIAAV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##ABVlocale#6##Z)
referenced in function "private: void __thiscall
boost::log::v2s_mt_nt5::basic_formatting_ostream,class std::allocator>
::aligned_write(unsigned short const *,__int64)" (??$aligned_write#G#?$basic_formatting_ostream#DU?$char_traits#D#std##V?$allocator#D#2##v2s_mt_nt5#log#boost##AAEXPBG_J#Z)
1>main.obj : error LNK2019: unresolved external symbol "public: static
class std::codecvt const & __cdecl
boost::filesystem::path::codecvt(void)"
(?codecvt#path#filesystem#boost##SAABV?$codecvt#GDH#std##XZ)
referenced in function "public: __thiscall
boost::filesystem::path::path(char const (&)[14],void
*)" (??$?0$$BY0O#$$CBD#path#filesystem#boost##QAE#AAY0O#$$CBDPAX#Z)
So basically it can't find functions convert(...), code_convert(...) and codecvt(...) in .lib files.
I've already tried to add all release libraries (.lib files made while building boost) to "additional libraries" and generally i think that visual studio find all libraries, but can't find those functions in libraries. Any ideas how can i fix this?
I've build Boost 1.55 from source using visual studio 2010 (msvc10.0), 32 bit, release and debug. I'm working on windows 7 x64.
I had the same problem and I could fix it by changing one of the Project properties on VisualStudio 2010.
Go to:
Property Pages -> ConfigurationProperties -> C/C++ -> Language -> Treat WChar_t As Built in Type:
And set the property to:
Yes (/Zc:wchar_t)
I hope it will be helpful for you too :)
Take care,

LNK2001 and LNK2019 error during VS2008 (or VS2010) upgrade from VS2005

I am attempting to upgrade an old VC6 solution to VS2010. Everything compiles fine (after a few code changes to handle some VC++ changes), but it refuses to link. I have 6 static libraries -- all set to "Use MFC in a Shared DLL" and "Not Using ATL" -- Unicode character set, and one Application (.EXE) set to "Use Standard Windows Libraries" and "Not Using ATL" (also Unicode, of course). This solution compiles/links fine using Visual Studio 2005 (SP1), but linking fails on both VS2008 and VS2010. The error messages are predominantly related to "CStringT". From everything I've researched, I should have seen these CStringT link errors in VS2005 also (that is when CString changed to template class), but as I said, it links fine in VS2005. I've tried every tip & trick that has been recommended here and elsewhere (i.e. explicitly importing the template class for CStringT in my stdafx.h header), but no luck. Anyone have any NEW ideas? I'll paste a sampling of the errors below for reference. Thanks!
error LNK2001: unresolved external symbol "public: class ATL::CStringT<wchar_t,class StrTraitMFC<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const & __thiscall SomeExceptionClass::GetErrorMessage(void)const " (?GetErrorMessage#SomeExceptionClass##QBEABV?$CStringT#_WV?$StrTraitMFC#_WV?$ChTraitsCRT#_W#ATL#####ATL##XZ)
error LNK2001: unresolved external symbol "public: class ATL::CStringT<wchar_t,class StrTraitMFC<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const & __thiscall SomeExceptionClass::GetErrorMessage(void)const " (?GetErrorMessage#SomeExceptionClass##QBEABV?$CStringT#_WV?$StrTraitMFC#_WV?$ChTraitsCRT#_W#ATL#####ATL##XZ)
error LNK2019: unresolved external symbol "public: class ATL::CStringT<wchar_t,class StrTraitMFC<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const & __thiscall SomeExceptionClass::GetErrorMessage(void)const " (?GetErrorMessage#SomeExceptionClass##QBEABV?$CStringT#_WV?$StrTraitMFC#_WV?$ChTraitsCRT#_W#ATL#####ATL##XZ) referenced in function __catch$?Init#SomeOtherClass##QAEHPA_W00000#Z$0
error LNK2001: unresolved external symbol "public: class ATL::CStringT<wchar_t,class StrTraitMFC<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const & __thiscall SomeExceptionClass::GetErrorMessage(void)const " (?GetErrorMessage#SomeExceptionClass##QBEABV?$CStringT#_WV?$StrTraitMFC#_WV?$ChTraitsCRT#_W#ATL#####ATL##XZ)
error LNK2001: unresolved external symbol "public: class ATL::CStringT<wchar_t,class StrTraitMFC<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const & __thiscall SomeExceptionClass::GetErrorMessage(void)const " (?GetErrorMessage#SomeExceptionClass##QBEABV?$CStringT#_WV?$StrTraitMFC#_WV?$ChTraitsCRT#_W#ATL#####ATL##XZ)
error LNK2001: unresolved external symbol "public: class ATL::CStringT<wchar_t,class StrTraitMFC<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const & __thiscall SomeExceptionClass::GetErrorMessage(void)const " (?GetErrorMessage#SomeExceptionClass##QBEABV?$CStringT#_WV?$StrTraitMFC#_WV?$ChTraitsCRT#_W#ATL#####ATL##XZ)
error LNK2001: unresolved external symbol "public: void __thiscall SomeDatabaseClass::ExecDirect(class ATL::CStringT<wchar_t,class StrTraitMFC<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?ExecDirect#SomeDatabaseClass##QAEXABV?$CStringT#_WV?$StrTraitMFC#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z)
error LNK2001: unresolved external symbol "public: void __thiscall SomeDatabaseClass::ExecDirect(class ATL::CStringT<wchar_t,class StrTraitMFC<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?ExecDirect#SomeDatabaseClass##QAEXABV?$CStringT#_WV?$StrTraitMFC#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z)
DUH! That about sums up this solution. For some strange reason, VS2008 and VS2010 do a incredibly poor job of handling "additional link dependencies" when upgrading an old VC6 project (while VS2005 seems to handle it just fine). All I was missing was going into Link options in VS2010 (after loading & upgrading my old VC6 workspace/projects) and adding my five static libraries (lib1.lib, lib2.lib, etc.) to the "Additional Dependencies" field (Linker->Input), and then adding my output folder[s] (where those .LIB files are created) to the "Additional Library Directories" field (Linker->General) -- all under Properties for my actual .EXE project, of course. Boom, now it links like a charm. I got so sidetracked with the "red herring" of these CString/template upgrade issues (from MSDN articles, etc.), that I completely missed the obvious. Hope this helps someone down the road. :)

Linking error while using Visual Studio 2005(VC8)

I am getting a bunch of linking errors while trying to link the release version of an executable(debug version does not have the same issue). Comparing the command line for the link does not reveal any issues.
there are broadly 2 types of errors neither of which I can get a handle on.
The first kind complains about a unresolved external symbol _declspec(dllimport)
As an example:
error LNK2019: unresolved external symbol
"_declspec(dllimport)
public: __thiscall
stlpd_std::basic_string,class stlpd_std::allocator >::basic_string,class stlpd_std::allocator >(class stlpd_std::basic_string,class stlpd_std::allocator > const &)" (_imp??0?$basic_string#DV?$char_traits#D#stlpd_std##V?$allocator#D#2##stlpd_std##QAE#ABV01##Z) referenced in function "public: __thiscall Springfield::generic::runtime_error::runtime_error(class stlpd_std::basic_string,class stlpd_std::allocator > const &)" (??0runtime_error#generic#Springfield##QAE#ABV?$basic_string#DV?$char_traits#D#stlpd_std##V?$allocator#D#2##stlpd_std###Z)
for a more human readable version(replacing all the strings):
error LNK2019: unresolved external symbol
"__declspec(dllimport)
public: __thiscall
string::basic_string,class stlpd_std::allocator >(class string const &)" (_imp??0?$basic_string#DV?$char_traits#D#stlpd_std##V?$allocator#D#2##stlpd_std##QAE#ABV01##Z) referenced in function "public: __thiscall Springfield::generic::runtime_error::runtime_error(class string const &)" (??0runtime_error#generic#Springfield##QAE#ABV?$basic_string#DV?$char_traits#D#stlpd_std##V?$allocator#D#2##stlpd_std###Z
The sceond class of errors complains about
unresolved external symbol __CrtDbgReportW
I hope I can get some kind of insight in dealing with this.
From the errors it looks like you are not including the CRT as one of your linked libraries. Here is a link to the different CRT lib's offered in Visual Studio 2005. Choose the one which is most appropriate and make sure it's in the list of lib's to link against
http://msdn.microsoft.com/en-us/library/abx4dbyh(VS.80).aspx
It looks like you're either including a file that's been built using the debug settings or you're mixing runtime libraries (DLL and static).

VC++ linker errors on std::exception::_Raise and std::exception::exception

I am using Visual C++ 2005 Express Edition and get the following linker errors:
19>mylib1.lib(mylibsource1.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall std::exception::_Raise(void)const " (__imp_?_Raise#exception#std##QBEXXZ) referenced in function "protected: static void __cdecl std::vector<class mytype,class std::allocator<class mytype> >::_Xlen(void)" (?_Xlen#?$vector#Vmytype##V?$allocator#Vmytype###std###std##KAXXZ)
19>mylib2.lib(mylibsource2.obj) : error LNK2001: unresolved external symbol "__declspec(dllimport) public: void __thiscall std::exception::_Raise(void)const " (__imp_?_Raise#exception#std##QBEXXZ)
19>mylib1.lib(mylibsource1.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall std::exception::exception(char const *,int)" (__imp_??0exception#std##QAE#PBDH#Z) referenced in function "public: __thiscall std::logic_error::logic_error(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0logic_error#std##QAE#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##1##Z)
19>mylib2.lib(mylibsource2.obj) : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::exception::exception(char const *,int)" (__imp_??0exception#std##QAE#PBDH#Z)
I turned off exceptions in generated code, and I am using before including the vector header file:
#define _HAS_EXCEPTIONS 0
A few Google results turned up some stuff, but no "aha!" solutions that worked for me.
EDIT:
As noted "_HAS_EXCEPTIONS 0" doesn't turn off exceptions, per se. What it does is, at least in the vector header file, is call _Raise on an exception object instead of calling the C++ "throw". In my case, it can't link to the exception object's _Raise function since I am not including the correct library. What that library is, though, is not obvious.
Adding this line:
#define _STATIC_CPPLIB
before including the vector header seems to do the trick.
The third error makes it clear that #define the _HAS_EXCEPTIONS 0 does not affect . Now, might include (makes sense, sharing code might reduce the size of your executable). That would explain why you still have errors if you define it before your inclusion of . This kind of defines should be done in your project settings.
Note that _HAS_EXCEPTIONS is an unsupported feature in Visual Studio. It does not turn off exceptions as such.