c++visual-studio-2010compilationlinker

Linker errors when attempting to convert const char * to LPTSTR


I have the following code snippet that converts a const char * to a LPTSTR, however I get linker errors when I compile.

char * pCopy3 = NULL;
    if (sizeof(TCHAR) == sizeof(char))
    {
        size_t size = strlen(words[2].c_str());
        pCopy3 = new char[size + 1];
        strcpy(pCopy3, words[2].c_str());
    }

The linker errors are as follows

Error   19  error LNK1169: one or more multiply defined symbols found   STablUpd.exe    1   1   STablUpd
Error   17  error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * args" (?args@@3PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in maindlg.obj  tabledlg.obj    STablUpd
Error   18  error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * words" (?words@@3PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in maindlg.obj    tabledlg.obj    STablUpd
Error   16  error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > cmdArgs" (?cmdArgs@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in maindlg.obj    tabledlg.obj    STablUpd
Error   13  error LNK2005: "int __cdecl Main_OnInitDialog(struct HWND__ *,struct HWND__ *,long)" (?Main_OnInitDialog@@YAHPAUHWND__@@0J@Z) already defined in maindlg.obj    tabledlg.obj    STablUpd
Error   8   error LNK2005: "int __stdcall Main_DlgProc(struct HWND__ *,unsigned int,unsigned int,long)" (?Main_DlgProc@@YGHPAUHWND__@@IIJ@Z) already defined in maindlg.obj tabledlg.obj    STablUpd
Error   15  error LNK2005: "struct HINSTANCE__ * g_hInstApp" (?g_hInstApp@@3PAUHINSTANCE__@@A) already defined in maindlg.obj   tabledlg.obj    STablUpd
Error   9   error LNK2005: "void __cdecl Main_OnBrowse(struct HWND__ *)" (?Main_OnBrowse@@YAXPAUHWND__@@@Z) already defined in maindlg.obj  tabledlg.obj    STablUpd
Error   10  error LNK2005: "void __cdecl Main_OnClose(struct HWND__ *,int)" (?Main_OnClose@@YAXPAUHWND__@@H@Z) already defined in maindlg.obj   tabledlg.obj    STablUpd
Error   11  error LNK2005: "void __cdecl Main_OnCommand(struct HWND__ *,int,struct HWND__ *,unsigned int)" (?Main_OnCommand@@YAXPAUHWND__@@H0I@Z) already defined in maindlg.obj    tabledlg.obj    STablUpd
Error   12  error LNK2005: "void __cdecl Main_OnDisplay(struct HWND__ *)" (?Main_OnDisplay@@YAXPAUHWND__@@@Z) already defined in maindlg.obj    tabledlg.obj    STablUpd
Error   14  error LNK2005: _WinMain@16 already defined in maindlg.obj   tabledlg.obj    STablUpd

Solution

  • I presume the code snippet you have shown converts std::string to char*. In that case the code is correct and there should be no errors when compiling that code (except strcpy warning).

    You originally asked for char* to LPTSTR conversion. You could do it like this (for UNICODE character set):

    char *test;
    wchar_t *bla=new wchar_t[size];
    swprintf(bla, size, L"%hs", test);
    LPTSTR bla2=bla;
    

    Alternatively if you use multi-byte character set:

    char *test;
    LPTSTR bla2=test;