无法使用命令行链接库(错误 LNK2019)

Posted

技术标签:

【中文标题】无法使用命令行链接库(错误 LNK2019)【英文标题】:Can't link libraries using command line (error LNK2019) 【发布时间】:2020-10-15 14:13:59 【问题描述】:

我正在尝试使用 cl.exe 编译器手动编译包含 GLFW 和 GLEW 的项目。 我在链接 .lib 文件时遇到问题。链接器似乎忽略了 /LIBPATH。我添加了 /VERBOSE:LIB 以查看链接器正在搜索什么。

命令

cl src/main.cpp /I include /link /LIBPATH:C:\Users\user\Desktop\C++\VSCode1\lib /VERBOSE:LIB

输出

/out:main.exe
/LIBPATH:C:\Users\yuryi\Desktop\C++\VSCode1\lib
/VERBOSE:LIB
main.obj

Searching libraries
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\libcpmt.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\LIBCMT.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\OLDNAMES.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\uuid.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\kernel32.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\libvcruntime.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\ucrt\x86\libucrt.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\libcpmt.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\LIBCMT.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\OLDNAMES.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\uuid.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\kernel32.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\libvcruntime.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\ucrt\x86\libucrt.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\libcpmt.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\LIBCMT.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\OLDNAMES.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\uuid.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\kernel32.lib:

Finished searching libraries

Searching libraries
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\libcpmt.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\LIBCMT.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\OLDNAMES.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\uuid.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\kernel32.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\libvcruntime.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\ucrt\x86\libucrt.lib:

Finished searching libraries

Searching libraries
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\libcpmt.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\LIBCMT.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\OLDNAMES.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\uuid.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\kernel32.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\libvcruntime.lib:
    Searching C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\ucrt\x86\libucrt.lib:
    Searching C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\lib\x86\libcpmt.lib:

Finished searching libraries

... and a lot of error LNK2019 unresolved external symbol

【问题讨论】:

您没有链接到任何特定的库。仅指定您拥有某些库的路径并不会指示链接器尝试该文件夹中的所有库。 您实际上在哪里指定要链接的库?链接器不会神奇地知道查看 libglew.lib 或类似的东西,你必须告诉它。 是的,我不知道您必须在 cl 参数中指定 lib,而不是链接器选项。 【参考方案1】:

通过将 glfw3.lib 和 glew32.lib 添加到命令中来修复它

cl main.cpp glfw3.lib glew32.lib /I include /link /LIBPATH:C:\Users\user\Desktop\C++\VSCode

【讨论】:

以上是关于无法使用命令行链接库(错误 LNK2019)的主要内容,如果未能解决你的问题,请参考以下文章

错误 LNK2019:无法解析的外部符号“extern”C”这是啥错误?

Error:LNK2019无法解析的外部符号_sscanf, _vsprintf

关于我遇到的“LNK 2019无法解析的外部符号”的链接错误

visual c++,链接:致命错误 LNK1104:无法打开文件

error LNK2019: 无法解析的外部符号;fatal error LNK1120: 1 个无法解析的外部命令; 可能存在的问题

error LNK2019: 无法解析的外部符号