Auto Linking
#pragma comment (lib, "...")
and #pragma link "..." both enable Auto-Linking.
They differ in the default extension when none is specified. It is important to note that the logic only supports short extensions: i.e. extensions of 3 letters or less.
When no extension is specified #pragma link "..." defaults to .obj and .o for WIN32 and WIN64 respectively. #pragma comment]](lib, "...")
defaults to .lib for WIN32 and WIN64X (Modern), and defaults to .a for WIN64.
The table below illustrates.
Pragma | File Auto-Linked WIN32 | File Auto-Linked WIN64 | File Auto-Linked WIN64X | Notes |
---|---|---|---|---|
#pragma link "Network.util" | "Network.util.obj" | "Network.util.o" | "Network.util.o" | |
#pragma link "Network.util.os" | "Network.util.os" | "Network.util.os" | "Network.util.os" | The .os is treated as the extension |
#pragma comment (lib, "Network.util")
|
"Network.util.lib" | "Network.util.a" | See section below for library auto linking with win64x | |
#pragma comment (lib, "Network.util.os")
|
"Network.util.os" | "Network.util.os" | See section below for library auto linking with win64x | The .os is treated as the extension |
Contents
Library Auto Linking with WIN64X
The auto-linking of libraries with the bcc64x.exe
compiler and ld.lld.exe
linker involves a search process in the new toolchain. This process looks for multiple name formats, including extensions, and returns an error only if none are found. The search stops as soon as a matching format is found. The files searched also depend on whether we default to static vs. dynamic. The table below shows the files searched for in static and dynamic modes.
Static | Dynamic |
---|---|
<name> + ".lib"
|
<name> + ".bpi"
|
"lib" + <name> + ".dll.a"
|
"lib" + <name> + ".dll.a"
|
<name> + ".dll.a"
|
<name> + ".dll.a"
|
"lib" + <name> + ".a"
|
"lib" + <name> + ".a"
|
<name> + ".lib"
|
The linker performs the search shown above. Therefore, the same search is applied for auto-linking via code (#pragma comment(lib, <name>)
, via the compiler with -lname
, or via the linker with -lname
.
Default libraries and library link searching
When searching for library X
, the toolchain looks for, in order:
- libX.a
- X.lib
Your libraries, both static libraries and package libraries, will use .lib
as an extension. However, the import libraries for many default Windows DLLs follow the libX.a
convention, meaning the library used to link against kernel32.dll is libkernel32.a.
This means the correct way to link to, say, uxtheme is:
#pragma comment(lib, "uxtheme")
This will link to libuxtheme.a, but due to auto-linking searching for the library, you only need to specify the name of what you want to link against (uxtheme), and there is no need to worry about the naming conventions.
Static vs. Dynamic
Static is the default setting. But you can switch to dynamic by specifying the linker option -Bdynamic or the compiler option -tR along with some other -txxx target option.