简体   繁体   中英

Qt - check availability of linked .lib on windows with preprocessor directives

I'm trying to use preprocessor directives in C++ for avoiding to compile code that requires a .lib, in case the library cannot be linked.

My .pro file contains:

INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"

and my directives are of the form:

#ifdef _WINSCARD_H_
// do something
#endif

or

#ifndef _WINSCARD_H_
// do something
#endif

This winscard comes with this windows sdk and I can definitely use its functionalities. The problems arise when I try to restrict the compilation based on these conditional directives.


Code compiles fine when using

INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"

in the .pro file.

Code is skipped during the compilation phase, as if the library is missing when using the above .pro configurations and the conditional directive, even though the library is available and linked:

#ifdef _WINSCARD_H_
// code that needs to be compiled only when library is present and linked.
#endif

The only change is the introduction of #ifdef _WINSCARD_H_ .

It is possible to generate conditional build in qmake basing on file existence. You can add in your .pro file something like that

exists( C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86/winscard* ) {
    message( "Configuring for winscard..." )
    INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
    LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"
    DEFINES += _WINSCARD_H_
}

The block after the built-in function exists() is parsed only when the path is found (it is possible to use asterisk to match part of filename). Here _WINSCARD_H_ is defined only if required file is found. So, that macro can be used in source code for conditional compilation. See qmake Test Functions Reference for details.

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