c - 从命令行使用 Visual Studio 2013 编译 aritchk.c 时出现未解析的符号

标签 c windows visual-studio-2013 cygwin linker-errors

我需要编译arithchk.c (来自 gdtoa 库)在 Windows 上使用在 cygwin 下运行的 Visual Studio 2013。 VS2008 一切正常,但是当我尝试切换到 VS2013 时,我遇到了这个问题:

ladanyi@WIN64-01$ echo $LIB
;c:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/LIB/amd64;c:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/ATLMFC/LIB/amd64;c:/Program Files (x86)/Windows Kits/8.1/lib/winv6.3/um/x64;

ladanyi@WIN64-01$ echo $INCLUDE
;c:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/INCLUDE;c:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/ATLMFC/INCLUDE;c:/Program Files (x86)/Windows Kits/8.1/include/shared;c:/Program Files (x86)/Windows Kits/8.1/include/um;c:/Program Files (x86)/Windows Kits/8.1/include/winrt;

ladanyi@WIN64-01$ echo $PATH
:/cygdrive/c/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE:/cygdrive/c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/BIN:/cygdrive/c/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/Tools:/cygdrive/c/Program Files (x86)/Microsoft Visual Studio 12.0/VC/VCPackages:/cygdrive/c/Program Files (x86)/Windows Kits/8.1/bin/x64:/cygdrive/c/Program Files (x86)/Windows Kits/8.1/bin/x86:/usr/local/bin:/usr/bin:/bin:/usr/bin:/cygdrive/c/Program Files (x86)/Intel/Composer XE 2011 SP1/redist/intel64/ipp:/cygdrive/c/Program Files (x86)/Intel/Composer XE 2011 SP1/redist/intel64/mkl:/cygdrive/c/Program Files (x86)/Common Files/Intel/Shared Libraries/redist/intel64/compiler:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/c/Program Files/Microsoft SQL Server/110/Tools/Binn:/cygdrive/c/Program Files (x86)/Microsoft SDKs/TypeScript/1.0:/cygdrive/c/Program Files (x86)/Windows Kits/10/Windows Performance Toolkit:/cygdrive/c/Program Files/MATLAB/R2015b/bin

ladanyi@WIN64-01$ LIB="$LIB" INCLUDE="$INCLUDE" cl -DNO_FPINIT arithchk.c -DNO_LONG_LONG -DNO_SSIZE_T  /INCREMENTAL:NO /VERBOSE
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.40629 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line warning D9035 : option 'V' has been deprecated and will be removed in a future release
arithchk.c
Microsoft (R) Incremental Linker Version 12.00.40629.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:arithchk.exe 
arithchk.obj 
arithchk.obj : error LNK2001: unresolved external symbol _asin
arithchk.obj : error LNK2001: unresolved external symbol _exp
arithchk.obj : error LNK2001: unresolved external symbol _log
arithchk.obj : error LNK2001: unresolved external symbol _sqrt
arithchk.obj : error LNK2001: unresolved external symbol _acos
arithchk.obj : error LNK2019: unresolved external symbol ___iob_func referenced in function _main
arithchk.obj : error LNK2019: unresolved external symbol _fprintf referenced in function _main
arithchk.obj : error LNK2019: unresolved external symbol _printf referenced in function _ccheck
arithchk.obj : error LNK2019: unresolved external symbol __errno referenced in function _main
arithchk.obj : error LNK2019: unresolved external symbol @__security_check_cookie@4 referenced in function _Lcheck
arithchk.obj : error LNK2019: unresolved external symbol ___security_cookie referenced in function _Lcheck
arithchk.obj : error LNK2001: unresolved external symbol __fltused
LINK : error LNK2001: unresolved external symbol _mainCRTStartup
arithchk.exe : fatal error LNK1120: 13 unresolved externals

LIB、INCLUDE 和 PATH 设置为我在命令窗口中运行 vcvarsall.bat 时的结果。

我只是不明白为什么这些符号未解析,并且非常感谢任何帮助。

谢谢, ——拉奇

更新

如果我打开 DOS 命令提示符,运行 vcvarsall.bat,然后编译,那么它就可以正常工作。请注意,运行 vcvarsall.bat 并执行 echo %LIB%echo %INCLUDE% 后,我得到与上面相同的值。所以问题出在与 cygwin 交互的某个地方,只是我不知道在哪里,但是:-(...

最佳答案

核实情况

Cygwin 默认将 前缀 /usr/local/bin:/usr/bin 到 PATH,并且错误很可能是由于 Cygwin 的 GNU 链接工具无论您如何设置环境都可以使用。您可以使用 echo $PATH 以及 link --versionwhich link 进行验证。1

快速修复

如果是这种情况,请在 ~/.bash_profile 中定义 PATH 变量,以便首先找到 MSVC 链接器。由于 Cygwin 将其启动时的 Windows 环境的完整路径存储为 ORIGINAL_PATH,因此您可以使用:

# Reorder PATH in .bash_profile
PATH="${ORIGINAL_PATH}/usr/local/bin:/usr/bin"

通过以这种方式定义 PATH,只要与 Cygwin 实用程序发生冲突,Windows 实用程序就会优先 - official docsfindlinksort 为例。

最小路线

您可能希望采用更简单的路线并仅使用您需要的那些项目来定义新的 PATH,而不是从 Windows 获取整个 PATH。当然,您可以在 .bash_profile 中执行此操作,但也可以在 Cygwin 启动之前从 Cygwin.bat 本身执行此操作。例如:

:: Minimal path defined in Cygwin.bat. Note the double quotes!
set PATH="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin"

同样,这在 Cygwin 中存储为 ORIGINAL_PATH,然后您可以按照上面的说明更改默认顺序。请记住,当您计算出依赖关系等时,对于更复杂的场景,最小方法可能会变得有点“繁琐”。

使用选定的环境启动 Cygwin

当我需要使用 MSVC 或其他 Windows 实用程序时,我通常更喜欢保持简单并将 Cygwin 实用程序放在整个 Windows 路径的末尾。但并非总是如此,而且我确实喜欢我的工具具有灵 active 。在下面的最小示例中,标准 Cygwin.bat 文件被修改,以便在 Cygwin 启动时可以通过命令行参数选择几个预定义设置之一。

@echo off
if "%1"==""   (goto run-1)
if "%1"=="-m" (goto prepend-1)
if "%1"=="-w" (goto prepend-2)
if "%1"=="-h" (goto help-1)

:help-1
echo  Cygwin.bat: modifed to set PATH via command line arg
echo  ARG    Description
echo  (none) Default - Prepend Cygwin PATH to the full Windows PATH. [CYG,WIN]
echo  -m     Prepend only MSVC utilities to the Cygwin PATH (minimal approach). [MSVC,CYG]
echo  -w     Prepend full Windows PATH to the Cygwin PATH. [WIN,CYG]
echo  -h     Print this help msg and exit.
exit /b

:prepend-1
set MSVC_KW=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin
goto run-1

:prepend-2
set WIN_KW=1
goto run-1

:run-1
C:
chdir C:\cygwin64\bin
bash --login -i

必须伴随着对 .bash_profile 中的路径进行重新排序,如下所示。

# Modify the default PATH in .bash_profile according to KW from Cygwin.bat
if [ -n "${MSVC_KW}" ] ; then
    PATH="${MSVC_KW}:/usr/local/bin:/usr/bin"
    export PATH
    unset MSVC_KW
elif [ -n "${WIN_KW}" ] ; then
    PATH="${ORIGINAL_PATH}:/usr/local/bin:/usr/bin"
    export PATH
    unset WIN_KW
fi

1 查看类似的 SO 主题 herehere 。这个答案补充了这些。

关于c - 从命令行使用 Visual Studio 2013 编译 aritchk.c 时出现未解析的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37910167/

相关文章:

c - 为什么终端中的输出不会并排排列

c++ - opengl 中的着色器应该做多少工作?

c - 有没有成熟的二元决策图工具可用?

java - Windows 上的 SWT : scroll control at cursor (not focused one)

c# - 如何获取WinForms项目中的所有表单和用户控件?

c - Shell/C中的命令头-1000000是什么意思

windows - 批量处理文件一个一个

windows - 为什么我的 COM 工厂在程序生命周期内从未被释放?

visual-studio-2013 - 阻止 Application Insights 发布到调试控制台

c# - 如何在 Visual Studio 2013 中附加源代码?