简体   繁体   中英

C++ Delayed Load DLL Linker Error

I have a program that calls functions from foo.dll in one configuration, and has a second configuration that does not use foo.dll. I want to require foo.dll to be present only when needed. I currently switch between the two versions using

#define FLAGVAR 0

or

#define FLAGVAR 1

where FLAGVAR==1 means foo.dll is used. I then surround my import statements and function declarations like this:

#if FLAGVAR == 1
#import "foo_file.h"
#endif
...

#if FLAGVAR == 1
int foobar() {...}
#endif

If I include foo.dll in Linker->Input->Additional Dependencies, then my program builds regardless of FLAGVAR's value.

Here's my problem:

I don't want to require foo.dll to be present in versions where FLAGVAR == 0. Thus, I moved foo.dll to Linker->Input->Delay Loaded DLLs. Now if FLAGVAR==0 then it doesn't require foo.dll and builds fine. However, if FLAGVAR==1, then I get a bunch of LNK2019 errors complaining that the functions from foo.dll couldn't be linked.

How can I accomplish my goal of only requiring foo.dll when FLAGVAR==1? Am I using delayed dll loading incorrectly? Thanks in advance for your help!

Using Visual Studio 2017, version 15.4.5

Solved based on this link from the comments: C++ Visual Studio: linking using pragma comment

I didn't put any of the optional libraries in the properties dialog box in visual studio. Instead, in a header file, I put the following few lines:

#if FLAGVAR == 1
#pragma comment(lib, "foo.lib")
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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