c++ - Visual Studio 2015/Linux 扩展使用 Cygwin 生成 "collect2 : error : ld returned 1 exit status"

标签 c++ linux visual-studio-2015 cygwin ld

我目前正在尝试 Linux Extension Visual Studio 2015。

我计划有几个目标机器使用各种 distribs。到目前为止,我的测试机器是 Windows 7,带有最新的 Cygwin 和所有需要的包(opensshg++gdb。 ..) 安装。

我正在进行的测试包含一个非常简单的 C++“Hello world”文件,VS 项目设置是默认设置。

一切都运行良好,但链接:

  • 架构被 Visual Studio 认可
  • 源文件通过 VS 扩展正确复制到目标机器
  • 他们使用 g++ 编译
  • 但该过程以 “collect2:错误:ld 返回 1 退出状态”
  • 结束

我尝试使用 g++ 在远程平台上编译此文件。没问题,生成了一个可执行文件并且它可以工作。

我完全不明白。有什么想法吗?


详细信息(如果需要):

  • 我的 C++ 源文件:

     #include <cstdio>
    
     int main()
     {
        printf("hello from SandboxLinux!\n");
        return 0;
     }
    
  • 我的项目设置(远程设置和链接器设置,我看不出有什么特别之处):

Project settings Linker settings

  • 我的 Visual Studio 构建日志:

enter image description here

  • 在远程机器上:
    • main.cpp 已正确复制
    • .o 文件已正确生成
    • .exe (".out") 不是
    • 项目根目录、objbin 目录如下:

enter image description here enter image description here enter image description here

  • 直接在远程机器上使用g++时:
    • g++ main.cpp 静默终止并生成一个可以工作的 a.exe
    • 使用 g++ -v 不显示任何相关信息

最佳答案

好的,找到了!默认的 VS/Linux 项目选项不适合 Cygwin:

当将 VS 项目构建输出从“最小”(默认)切换到“正常”时,我注意到生成的 ld 命令行存在问题:

enter image description here

1>       Link:
1>         Linking objects
1>         Invoking ld
1>         g++ -o "/home/xxxxx/projects/xxxxxx/bin/x86/Release/xxxxx.out" -Wl,--no-undefined -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack /home/xxxxx/projects/xxxxx/obj/x86/Release/main.o
1>         /bin/ld: unrecognized option '-z'
1>         /bin/ld: use the --help option for usage information
1>         collect2: error: ld returned 1 exit status

-z 选项是 not implemented on Cygwinwon't be .所以它必须从默认使用它的 VS 链接器选项中删除:

之前: enter image description here

之后: enter image description here (注意 1:第三个​​必须手动设置为空白,因为两个建议的选项都使用 -z)
(注意 2:这两个屏幕截图之间的所有其他差异与此问题无关。第一个是我的 release 配置,默认情况下保留,第二个是我的 debug 配置,使用了 verbose 和 print-map。)

现在一切运行良好: enter image description here

程序已正确构建,可以正常工作,并且与 GDB 的链接在 Debug模式下也可以正常工作。

注意:以后从 Cygwin 切换到标准 Linux 平台时,这个 -z 可能会成为一个问题。

关于c++ - Visual Studio 2015/Linux 扩展使用 Cygwin 生成 "collect2 : error : ld returned 1 exit status",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40196983/

相关文章:

linux - ffmpeg 将任何文件转换为任何文件

VS 2015 中的 T4 模板

visual-studio-2015 - .pug 文件的 Jade 状语法高亮显示 Visual Studio 2015

xamarin - InspectorPackage 未正确加载

c++ - openCV没有匹配函数fillPoly

c++ - 如何修复 LAN 上的多个 Arduino 导致超时

python - 创建补丁文件时如何使 "diff"命令包含空文件

c++ - std::function、std::bind 和 std::ref 的 Clang 错误?

c++ - 为什么不能消除无限循环?

c - 如何处理 strerror_r 函数返回的指针?