Skip to content
Advertisement

Visual Studio 2015/Linux extension produces a “collect2 : error : ld returned 1 exit status” with Cygwin

I am currently trying the Linux Extension of Visual Studio 2015.

I plan to have several target machines using various distribs. So far, my testing machine is a Windows 7 with an up-to-date Cygwin with all needed packages (openssh, g++, gdb…) installed.

The test I’m conducting consists in a very simple C++ “Hello world” file, and the VS projects settings are the default ones.

Everything runs well but the linkage:

  • architecture is recognized by Visual Studio
  • source files are copied properly to the target machine by the VS extension
  • they do compile using g++
  • but the process ends up with a "collect2 : error : ld returned 1 exit status"

I tried to compile this file on the remote platform, using g++. No problem, an executable is produced and it works.

I absolutely don’t get it. Any idea?


Details if needed:

  • My C++ source file:

     #include <cstdio>
    
     int main()
     {
        printf("hello from SandboxLinux!n");
        return 0;
     }
    
  • My project settings (remote settings and linker settings, in which I see nothing special):

Project settings Linker settings

  • My Visual Studio build log:

enter image description here

  • On the remote machine:
    • main.cpp has been properly copied
    • .o file is properly produced
    • .exe (“.out“) is not
    • project root, obj and bin directories follow:

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

  • When using g++ directly on the remote machine:
    • g++ main.cpp terminates silently and produces an a.exe that works.
    • Using g++ -v does not show anything relevant

Advertisement

Answer

Ok, found it! Default VS/Linux project options are not suitable for Cygwin:

When switching the VS project build output from “minimal” (the default) to “normal” I could notice there was a problem with the generated ld command line:

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

The -z option is not implemented on Cygwin and won’t be. So it has to be removed from the VS linker options where it is used by default:

Before: enter image description here

After: enter image description here (Note 1: the third one has to be manually set to blank as both proposed options use -z)
(Note 2: all other differences between these two screenshots are not tied to this problem. The first is my release configuration, kept by default, where the second is my debug configuration, that uses verbose and print-map.)

Now everything runs well: enter image description here

The program is properly built, it works, and the link with GDB works fine too in debug mode.

Caveat: this -z thing might be an issue when switching from Cygwin to standard Linux platforms later on.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement