1

I am getting the following link error when building a project in VS2015:

LNK2001 unresolved external symbol __imp__PathCombineW@12

The function PathCombineW is from shlwapi.h which I have included in my headers. The part that I find so confusing is that Intellisense resolves this function just fine, I can "peek definition" on the function and it takes me right to the declaration within shlwapi.h.

I have the same exact problem if I attempt to use any functions from pathcch.h. I had originally used this API because it seemed like that is the newer, preferred path manipulation API. However, I get the same exact LNK2001 with either API.

I can't figure out if it has to do with other headers I have included. They are listed below:

// daqServiceTray.cpp 
#include "stdafx.h"
#include "daqServiceTray.h"    
#include <Shlwapi.h>
#include <Shellapi.h>
#include <Strsafe.h>


//stdafx.h
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>


//targetver.h
#pragma once
#define WINVER 0x0601
#define _WIN32_WINNT 0x0601
#include <SDKDDKVer.h>


//dayServiceTray.h
#pragma once
#include "resource.h"

Resource.h is the header for my UI resource file. I am building these into a win32 GUI application. I am developing on Windows 7.

Reading through the error help I get the feeling like I am causing some conflict by using code from two different platform versions but I am having a lot of trouble figuring out what that conflict might be. The application was building and running OK before I attempted to do some path manipulation and added either pathcch.h or shlwapi.h.

Why can intellisense find the function by the linker yells at me for an unresolved external? Can it not find the library for the function definition? Is the order of my includes an issue?

My core question is twofold:

How can intellisense find the function but the linker cannot?

If the issue is that the definition of the function cannot be found, how can I learn which libraries need to be included for writing applications for my target platform (windows 7)? I have been having much trouble getting at this information on MSDN. Is there some kind of high level tutorial or page that explains the nuances of the build specifications for different platforms?

Kin3TiX
  • 708
  • 1
  • 5
  • 18
  • Declaring a function in a C header file is not the same as linking it at link time. They're two separate steps of the build process, and both are necessary for proper compilation. But IntelliSense is most likely only using the header files (if not its own catalog of function signatures), since that's where things like type information are stored. I don't know exactly how it wrks, but I do know it doesn't need the linker information to do its job. – andlabs Oct 13 '15 at 15:53
  • Intellisense only requires a declaration. The linker needs a definition. I don't see anything confusing here, this is how the tools work. To fix the issue, link against Shlwapi.lib, as documented under [PathCombine](https://msdn.microsoft.com/en-us/library/windows/desktop/bb773571.aspx). – IInspectable Oct 13 '15 at 15:53
  • As for looking on MSDN for linking information, that information is at the bottom of every page that covers a function. There's a neat table; the first row of that table reads "Minimum supported client". The "Library" row says what library you need to link against. This is always in the form "somename.lib". I don't use Visual Studio so i don't know how to actually add the library to the list of libraries to link against, but check your project properties. – andlabs Oct 13 '15 at 15:56
  • I could find where I need to add the library definition, though I have not yet figured out how to find where VS2015 stores the libraries themselves. I am also not sure why shlwapi.h wouldn't get pulled in with the default build spec. – Kin3TiX Oct 13 '15 at 15:58
  • You shouldn't need to worry about where the .lib files themselves are; merely using the filename should be sufficient... Do you get an error if you try to just say `shlwapi.lib` in that project setting? As for shlwapi.h, what error are you getting? That should be stored alongside windows.h and there should be no problems finding it... – andlabs Oct 13 '15 at 15:59

1 Answers1

5

How can IntelliSense find the function but the linker cannot?

The linker is looking for the .lib file, IntelliSense is just using the header. To generate an executable (compile + link) you need both.

If the issue is that the definition of the function cannot be found, how can I learn which libraries need to be included for writing applications for my target platform (windows 7)?

The documentation for PathCombine has a nice table at the bottom that says which header the function is defined in, as well as which library the linker needs to link the function.

For PathCombine the table looks like this:

+----------------------------------------------------------------------------------------+
| Minimum supported client  |  Windows 2000 Professional, Windows XP [desktop apps only] |
+----------------------------------------------------------------------------------------+
| Minimum supported server  |  Windows 2000 Server [desktop apps only]                   |
+----------------------------------------------------------------------------------------+
| Header                    |  Shlwapi.h                                                 |
+----------------------------------------------------------------------------------------+
| Library                   |  Shlwapi.lib                                               |
+----------------------------------------------------------------------------------------+
| DLL                       |  Shlwapi.dll (version 4.71 or later)                       |
+----------------------------------------------------------------------------------------+
| Unicode and ANSI names    |  PathCombineW (Unicode) and PathCombineA (ANSI)            |
+----------------------------------------------------------------------------------------+

Reading this chart tells us that to get this function we'd want to use

#include <Shlwapi.h>

and add Shlwapi.lib to the linker's list of libraries. It also tells us that if we want to dynamically call the function, we can call LoadLibrary and pass Shlwapi.dll to it, then use GetProcAddress to find the PathCombineA or PathCombineW functions.

theB
  • 6,450
  • 1
  • 28
  • 38
  • Nitpicking: To compile, declarations (header files) are sufficient. The linker requires definitions or references, to fill in the gaps. – IInspectable Oct 13 '15 at 16:05
  • @IInspectable - Worthwhile point. Updated to be more specific. (I appreciate nitpicking, it makes the answer better.) – theB Oct 13 '15 at 16:08