undefined reference to imp PathIsDirectory


I have included <shlwapi.h>, and in building options -> linker settings, I have added "libshlwapi.a". This does not work for "PathIsDirectory". I get undefined reference to imp__PathIsDirectory @ 4.

Previously linking to "libws2_32.a" has worked well.

On the microsoft document, it says header: "shlwapi.h" and lib: "shlwapi.lib" (I suppose "libshlwapi.a" is the equivalent to "shlwapi.lib" if I use codeblocks?)

But it doesn't work. What can I do? :)
Last edited on
Can you be more specific in how you're linking?

CodeBlocks is an IDE. You can think of it as being a wrapper around a compiler. The default compiler (and linker) that can come bundled with CodeBlocks is g++ (part of GCC).

For linker errors like this, it's important to be very precise in communicating what you've done.
In my mingw directory tree, I have the two files:
 Directory of C:\MinGW\x86_64-w64-mingw32\lib 
    libws2_32.a (174,452 bytes)
    libshlwapi.a (274,140 bytes)


With the following code,

1
2
3
4
5
6
7
8
9
10
#include <windows.h>
#include <shlwapi.h>

#include <iostream>

int main()
{
    bool is_dir = PathIsDirectory("C:\\foo");
    std::cout << "is " << ((is_dir) ? "dir" : "not dir");
}


If I compile with g++ main.cpp -o main.exe

I get the following error:
C:\code\cplusplus241958>g++ main.cpp -o main.exe
C:\Users\Foo\AppData\Local\Temp\ccNDV47z.o:main.cpp:(.text+0x17): undefined reference to `__imp_PathIsDirectoryA'
collect2.exe: error: ld returned 1 exit status


To alleviate this, I need to explicitly link with the libshlwapi.a, like you said.
I did this through:
g++ main.cpp -lshlwapi -o main.exe
or
g++ main.cpp -o main.exe -lshlwapi

And no longer get linker errors.

I can't use CB right now, but if you go to your project settings somewhere, you should be able to specify -lshlwapi as an additional linker command or command-line argument.
(CodeBlocks might add the dash or the lowercase-L for you, I'm not sure. Try different combos.)

If that doesn't work, post the XML of your .cbp file.
Last edited on
Hello Ganado. Thanks for your answer. It does not work in CB for me. I added -lshlwapi and its included in the buildlog. I tried adding it everywhere
And I tried the following with sublime. I get the same result...

{
"cmd": ["g++", "$file_name", "-o", "${file_base_name}.exe", "-lshlwapi", "&&", "start", "cmd", "/k" , "$file_base_name"],
"selector": "source.cpp",
"working_dir": "${file_path}",
"shell": true
}

undefined reference to `_imp__PathIsDirectoryA@4'
Is your .a file for shlwapi in the same folder as libws2_32.a (which you said did work)?

If it is in the same folder, I'm out of ideas.
Yes. They're all in C:\Program Files\CodeBlocks\MinGW\lib
It works flawlessly in the updated version of mingw which is mingw-w64 (which is what Ganado used).

All I can rule out now is that maybe something is really really janky with your computer as a whole, because people usually don't link libws2_32.a from the mingw folder.

I recommend you to try a different computer because your computer seems cursed.

Also as a hunch after 1 google, for sublime, don't you need a "path" like this:
"path": "c:/Program Files/mingw-w64/mingw64/bin",

If there is a mingw is on your global PATH (like from cygwin or msys), maybe that could cause some weirdness. Can you make sure your mingw is the mingw that you think you are using?

You are absolutely right. I now tested on my other computer (windows 7), it works without any problem.

But on my laptop (windows vista) it does not work. It's strange because I have the same installation of CodeBlocks on both computers. And when I've previously linked to other libs, it has, as I mentioned, worked without problems
The joke's on me. I found this now: "Minimum supported server Windows 2000 Server [desktop apps only]"
Last edited on
Trust me, laptops count as desktop apps, since they are fundamentally running the same code from the same install disk (and I am writing this on a laptop).

I don't know if this is a problem with vista, (I doubt it), but this link:

http://wiki.codeblocks.org/index.php/Installing_MinGW_with_Vista

Is super outdated and probably not relevant (all the way from 2007), but perhaps you should try putting in the search directories in the global compiler options as a last attempt (even though it doesn't make sense to me).
Topic archived. No new replies allowed.