方法1:使用完整的开发者命令提示符

  1. 找到正确的开发者命令提示符

    • 在开始菜单搜索 "x64 Native Tools Command Prompt for VS 2022"

    • 或者 "Developer Command Prompt for VS 2022"

  2. 在该命令窗口中编译

    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找不到的问题了。

Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐