Search code examples
c++qtlnk2019

LNK2019 Unresolved External Symbol SHGetFolderPathW


So,

I've come across a strange error and I can't seem to find a solution for it. I'm writing this code on Qt Creator 3.1.1. The code is:

#include <ShlObj.h>
#include <string>
#include <vector>

static std::wstring GetUserDirectory()
{
    std::wstring returnPath;
    TCHAR path[MAX_PATH];
    HRESULT hr = SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL,
                                 SHGFP_TYPE_CURRENT, path);
    if (hr == S_OK)
    {
        returnPath = path;
        return returnPath;
    }
    else
    {
        return NULL;
    }
}

When I try to build the program, I get the following linking errors:

error: LNK2019: unresolved external symbol __imp_SHGetFolderPathW referenced in function "public: static class std::basic_string,class std::allocator > __cdecl FileUtils::GetUserDirectory(void)" (?GetUserDirectory@FileUtils@@SA?AV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@XZ)

error: LNK1120: 1 unresolved externals

I'm assuming that these errors are related to the above code because from what I understand, SHGetFolderPath is just a wrapper for SHGetFolderPathW. However, I'm not sure why I'm getting this error. Any help would be appreciated.

Thanks,


Solution

  • This is a Windows linker error caused by not including proper libraries during link. Read the documentation for the function at,

    http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181%28v=vs.85%29.aspx

    then you see at the bottom that you have to include

    Library: Shell32.lib

    If you are using Qt, that would mean you have to add LIBS += lshell32 to the Qt project file.