在 MSYS 环境中使用 MSVC 工具链(例如:CL.EXE)时,如何避免 "unresolved external symbol _mainCRTStartup" 错误?
How do I avoid the "unresolved external symbol _mainCRTStartup" error when using the MSVC toolchain (ex: CL.EXE) from within an MSYS environment?
在 MSYS 环境中使用 MSVC 工具链(例如:CL.EXE)时如何避免 unresolved external symbol _mainCRTStartup
错误?
详情:
我启动了 "VS2013 x64 Native Tools Command Prompt" 并从那里启动了 C:\msys64\msys2.exe
。
在我的 MSYS 会话中,我得到如下结果:
user@host MSYS /c/libpostal/libpostal
$ "/c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/amd64/vcvars64.bat"
user@host MSYS /c/libpostal/libpostal
$ cl "-nologo" "conftest.c" "-link" "-SUBSYSTEM:CONSOLE"
conftest.c
LINK : error LNK2001: unresolved external symbol _mainCRTStartup
conftest.exe : fatal error LNK1120: 1 unresolved externals
user@host MSYS /c/libpostal/libpostal
$ cat conftest.c
/* confdefs.h */
#define PACKAGE_NAME "libpostal"
#define PACKAGE_TARNAME "libpostal"
#define PACKAGE_VERSION "1.0.0"
#define PACKAGE_STRING "libpostal 1.0.0"
#define PACKAGE_BUGREPORT ""
#define PACKAGE_URL ""
#define PACKAGE "libpostal"
#define VERSION "1.0.0"
/* end confdefs.h. */
int
main ()
{
;
return 0;
}
user@host MSYS /c/libpostal/libpostal
$ echo $PATH
/c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
如果我尝试将相同的 "cl" 命令放入 "VS2013 x64 Native Tools Command Prompt",我会得到预期的结果:
C:\libpostal\libpostal>cl "-nologo" "conftest.c" "-link" "-SUBSYSTEM:CONSOLE"
conftest.c
C:\libpostal\libpostal>cl "/nologo" "conftest.c" "/link" "/SUBSYSTEM:CONSOLE"
conftest.c
C:\libpostal\libpostal>conftest.exe
C:\libpostal\libpostal>echo %PATH%
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\MSBuild.0\bin\amd64;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\amd64;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools;C:\Program Files (x86)\HTML Help Workshop;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools;C:\Program Files (x86)\Windows Kits.1\bin\x64;C:\Program Files (x86)\Windows Kits.1\bin\x86;C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\MSBuild.0\bin\amd64;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\amd64;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools;C:\Program Files (x86)\HTML Help Workshop;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools;C:\Program Files (x86)\Windows Kits.1\bin\x64;C:\Program Files (x86)\WindowsKits.1\bin\x86;C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft Dynamics AX\Client\Bin\;C:\Program Files\Microsoft Dynamics AX\BusinessConnector\Bin\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC0\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\ManagementStudio\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files(x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files (x86)\Windows Kits.1\Windows Performance Toolkit\;C:\Program Files (x86)\Microsoft SDKs\TypeScript.0\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Git\cmd;C:\D\dmd2\windows\bin;C:\msys64;C:\msys64\usr\local\bin;C:\msys64\usr\bin;C:\msys64\bin;C:\msys64\opt\bin
这发生在我尝试使用 cccl to build a posix project (libpostal) 和 MSVC 工具链时,因此我可以 link 它与其他 MSVC 代码一起使用。因此,任何涉及 Visual Studio GUI 的答案可能都不会很有帮助:我需要弄清楚如何让它在 MSYS2/MSYS64 环境中工作。
在 MSYS 示例中,我故意将 /c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin
移动到我的 $PATH 的开头以避免选择错误的可执行文件。当它位于 $PATH 的末尾时,我最初收到了相同的结果(完全相同的错误消息)。
仔细检查了一切,我发现了问题所在:
当从 "VS2013 x64 Native Tools Command Prompt" 执行 C:\msys64\msys2.exe
时,$PATH 变量将最终包含 /c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin
,但这是 64 位工作的错误目录。相反,路径应包含 /c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/amd64
。一旦我做了那个替换,cl.exe
工作正常。
运行 vcvars64.bat 来自 MSYS 环境的 不会 修复 $PATH。上述替换必须手动完成或从 ~/.bashrc
.
等自定义启动脚本完成
在 MSYS 环境中使用 MSVC 工具链(例如:CL.EXE)时如何避免 unresolved external symbol _mainCRTStartup
错误?
详情:
我启动了 "VS2013 x64 Native Tools Command Prompt" 并从那里启动了 C:\msys64\msys2.exe
。
在我的 MSYS 会话中,我得到如下结果:
user@host MSYS /c/libpostal/libpostal
$ "/c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/amd64/vcvars64.bat"
user@host MSYS /c/libpostal/libpostal
$ cl "-nologo" "conftest.c" "-link" "-SUBSYSTEM:CONSOLE"
conftest.c
LINK : error LNK2001: unresolved external symbol _mainCRTStartup
conftest.exe : fatal error LNK1120: 1 unresolved externals
user@host MSYS /c/libpostal/libpostal
$ cat conftest.c
/* confdefs.h */
#define PACKAGE_NAME "libpostal"
#define PACKAGE_TARNAME "libpostal"
#define PACKAGE_VERSION "1.0.0"
#define PACKAGE_STRING "libpostal 1.0.0"
#define PACKAGE_BUGREPORT ""
#define PACKAGE_URL ""
#define PACKAGE "libpostal"
#define VERSION "1.0.0"
/* end confdefs.h. */
int
main ()
{
;
return 0;
}
user@host MSYS /c/libpostal/libpostal
$ echo $PATH
/c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
如果我尝试将相同的 "cl" 命令放入 "VS2013 x64 Native Tools Command Prompt",我会得到预期的结果:
C:\libpostal\libpostal>cl "-nologo" "conftest.c" "-link" "-SUBSYSTEM:CONSOLE"
conftest.c
C:\libpostal\libpostal>cl "/nologo" "conftest.c" "/link" "/SUBSYSTEM:CONSOLE"
conftest.c
C:\libpostal\libpostal>conftest.exe
C:\libpostal\libpostal>echo %PATH%
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\MSBuild.0\bin\amd64;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\amd64;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools;C:\Program Files (x86)\HTML Help Workshop;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools;C:\Program Files (x86)\Windows Kits.1\bin\x64;C:\Program Files (x86)\Windows Kits.1\bin\x86;C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\MSBuild.0\bin\amd64;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\amd64;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools;C:\Program Files (x86)\HTML Help Workshop;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Performance Tools;C:\Program Files (x86)\Windows Kits.1\bin\x64;C:\Program Files (x86)\WindowsKits.1\bin\x86;C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft Dynamics AX\Client\Bin\;C:\Program Files\Microsoft Dynamics AX\BusinessConnector\Bin\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC0\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\ManagementStudio\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files(x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files (x86)\Windows Kits.1\Windows Performance Toolkit\;C:\Program Files (x86)\Microsoft SDKs\TypeScript.0\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Git\cmd;C:\D\dmd2\windows\bin;C:\msys64;C:\msys64\usr\local\bin;C:\msys64\usr\bin;C:\msys64\bin;C:\msys64\opt\bin
这发生在我尝试使用 cccl to build a posix project (libpostal) 和 MSVC 工具链时,因此我可以 link 它与其他 MSVC 代码一起使用。因此,任何涉及 Visual Studio GUI 的答案可能都不会很有帮助:我需要弄清楚如何让它在 MSYS2/MSYS64 环境中工作。
在 MSYS 示例中,我故意将 /c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin
移动到我的 $PATH 的开头以避免选择错误的可执行文件。当它位于 $PATH 的末尾时,我最初收到了相同的结果(完全相同的错误消息)。
仔细检查了一切,我发现了问题所在:
当从 "VS2013 x64 Native Tools Command Prompt" 执行 C:\msys64\msys2.exe
时,$PATH 变量将最终包含 /c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin
,但这是 64 位工作的错误目录。相反,路径应包含 /c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/amd64
。一旦我做了那个替换,cl.exe
工作正常。
运行 vcvars64.bat 来自 MSYS 环境的 不会 修复 $PATH。上述替换必须手动完成或从 ~/.bashrc
.