2

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,

user3593978
  • 37
  • 1
  • 5
  • 1
    [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) almost always applies as a duplicate for such questions, sorry. – πάντα ῥεῖ Jul 07 '14 at 20:32
  • 1
    Did you add Shell32.lib to your libraries? – drescherjm Jul 07 '14 at 20:32

1 Answers1

7

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.

user3427419
  • 1,769
  • 11
  • 15