在window命令行下使用MSVC编译c程序
总是使用Visual Studio自带的"x64 Native Tools Command Prompt",这样可以确保所有环境变量正确设置。这样就能解决kernel32.lib找不到的问题了。但这不是推荐做法,可能会影响程序功能。# 编译成功,生成hello.exe。# 如果安装了MinGW-w64。# 根据你的VS版本选择路径。# 如果安装了Clang。
·
方法1:使用完整的开发者命令提示符
-
找到正确的开发者命令提示符
-
在开始菜单搜索 "x64 Native Tools Command Prompt for VS 2022"
-
或者 "Developer Command Prompt for VS 2022"
-
-
在该命令窗口中编译
cl hello.c
方法2:手动运行vcvarsall.bat
# 根据你的VS版本选择路径
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x64 cl hello.c
方法3:检查并设置完整的库路径
如果还想手动设置,需要添加Windows SDK的库路径:
set INCLUDE=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um;C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared set LIB=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x64 set PATH=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\bin\Hostx64\x64;%PATH%
方法4:临时解决方案 - 跳过默认库
如果急需编译,可以暂时跳过kernel32.lib:
cl hello.c /link /NODEFAULTLIB:kernel32.lib
但这不是推荐做法,可能会影响程序功能。
方法5:使用其他编译器(推荐替代方案)
安装 MinGW-w64 或 Clang:
# 如果安装了MinGW-w64 gcc hello.c -o hello.exe # 如果安装了Clang clang hello.c -o hello.exe
最佳实践建议
推荐使用方法1:总是使用Visual Studio自带的"x64 Native Tools Command Prompt",这样可以确保所有环境变量正确设置。
找到正确的命令提示符后,你的编译流程应该是:
cl hello.c # 编译成功,生成hello.exe hello.exe Hello.
这样就能解决kernel32.lib找不到的问题了。
更多推荐
所有评论(0)