简体   繁体   中英

link.exe, choosing 64-bit or 32-bit version of Kernel32.lib

I am trying to compile and link 32 bit program in 64 bit Windows. I compiled my program with nasm

nasm -f win32 test.asm 

It is compiled successfully. But when i try to link with link.exe

link /entry:start /subsystem:console test.obj Kernel32.lib 

or

link /machine:x86 /entry:start /subsystem:console test.obj Kernel32.lib

it gives me this error:

C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x64\Kernel32.lib : warnin
g LNK4272: library machine type 'x64' conflicts with target machine type 'X86'
test.exe : fatal error LNK1120: 3 unresolved externals

but when i write the absolute path of 32bit version of Kernel32.lib it links successfully. I think i should add the location of 32bit Kernel32.lib as path variable to somewhere or i should use a switch but i could not find it.

Is there any way to link without writing absolute path ?

You should be running the linker (and other SDK tools) from a command prompt that has been properly set up with the correct environment variables. Then you will not have this problem.

The simplest way to do this is to run the provided batch files. vcvarsall.bat is the master one, use it with a single argument specifying the type of build environment you want to use. Either x86 for 32-bit, amd64 for 64-bit builds using the 64-bit toolchain, or x86_amd64 for 64-bit builds using the 32-bit toolchain (the 64-bit on 32-bit cross-compiler). Alternatively, you can use the individual batch files, vcvars32.bat , vcvars64.bat , and vcbarsx86_amd64.bat .

You can go through these files yourself and see what environment variables they set up, but it is honestly not worth the time reverse engineering the setup that they do. The relevant portion is setting an environment variable %WindowsSdkDir% to the root directory of the Windows SDK installation, and then using that to set:

  1. PATH to include the appropriate bin subdirectory, containing the tools, including link.exe. For x86 and x86_amd64, that is just bin . For amd64, it is the bin\\x64 subdirectory.

  2. INCLUDE to the appropriate include subdirectory containing the header files (this is the same for all build types).

  3. LIB to the appropriate lib subdirectory containing the linker dynamic stubs. For x86, this is just lib . For amd64 and x86_amd64, it is the lib\\x64 subdirectory.

The one you are apparently missing is the last one. The linker is not looking in the correct directory for the LIB files. It doesn't do it automatically based on the machine switch. It has no idea how you have configured your build system or where you have installed the SDK files. Use the batch files to get it all set up correctly with minimal fuss.

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