简体   繁体   English

如何在Visual Studio中使用C ++ shlwapi库?

[英]How can I use the C++ shlwapi library in Visual Studio?

I wanted to make a simple program for classifying files by their extensions, but when tried to compile got the error 我想制作一个简单的程序来按扩展名对文件进行分类,但是在尝试编译时出现了错误

Severity Code Description Project File Line Error LNK2019 unresolved external symbol __imp__PathFileExistsW@4 referenced in function "void __cdecl classifyFiles(void)" (?classifyFiles@@YAXXZ) 严重性代码说明项目文件行错误LNK2019无法解析的外部符号__imp__PathFileExistsW @ 4在函数“ void __cdecl classifyFiles(void)”中引用(?classifyFiles @@ YAXXZ)

My code is where PathFileExists is called is this: 我的代码是调用PathFileExists的地方:

void classifyFiles() {
    for (int i = 0; i < files.max_size(); i++) {
        //if each folder doesn't exist, create folder
        LPCWSTR folderName = (L"\\" + extensions.at(i)).c_str();
        if (!PathFileExistsW(folderName)) {
            CreateDirectory(folderName, NULL);
        };
        LPCWSTR destination = (extensions.at(i) + files.at(i)).c_str();
        //move file
        MoveFile(files.at(i).c_str(), destination);
    }
}

I googled a little and it seems it is not enough with including the Shlwapi.h header and also need to link the library. 我在Google上搜索了一下,似乎仅包含Shlwapi.h标头是不够的,还需要链接库。 But I can't find any answer about how to do it in Visual Studio. 但是我找不到有关如何在Visual Studio中执行此操作的任何答案。

Note: I am also having problem finding were are each menu and everything (this is my first time with Visual Studio), so please make it clear where to find what and if possible include screenshots. 注意:我也遇到了问题,即每个菜单和所有内容(这是我第一次使用Visual Studio),因此请弄清楚在哪里可以找到内容,并在可能的情况下包括屏幕截图。

References: https://msdn.microsoft.com/en-us/library/windows/desktop/bb773584(v=vs.85).aspx 参考: https : //msdn.microsoft.com/zh-cn/library/windows/desktop/bb773584(v=vs.85).aspx

Add shlwapi.lib to your dependency list: 将shlwapi.lib添加到您的依赖项列表中:

Configuration Properties -> Linker -> Input -> Additional Dependencies 配置属性->链接器->输入->其他依赖项

There are two things I did for the build to succeed: Add 为了使构建成功,我做了两件事:添加
#pragma comment(lib, "Shlwapi.lib")
after the includes. 包含之后。 In my specific case changing the following line of code made a difference, for some reason, even if they are the same. 在我的特定情况下,由于某些原因,即使它们相同,更改以下代码行也有所不同。 Changing: 变更:

if (!PathFileExistsW(folderName))

To: 至:

if (PathFileExistsW(folderName) == FALSE)

For some reason using the value of PathFileExists like the first outputted a linking error even if everything else was in order. 由于某种原因,即使其他条件都正常,使用像第一个一样的PathFileExists的值也会输出链接错误。 The next day I tested it again and it worked both ways. 第二天,我再次对其进行了测试,并且它可以双向工作。 Maybe it was my imagination and was making some mistake. 也许这是我的想象,并且犯了一些错误。

首先,您需要添加VC ++目录,然后需要设置链接器输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM