collect2.exe: error: ld returned 5 exit status
Asked Answered
I

3

11

I've recently updated all msys2 packages and now I can't build my project. The error occurs at the moment of linking one of .dlls - target_library.dll.

The error message isn't clear to me. What does status 5 mean?

collect2.exe: error: ld returned 5 exit status

Full message:

g++ -Wl,-s,--relax,--gc-sections -shared -Wl,-subsystem,windows -mthreads -Wl,--out-implib,C:/msys64/workspace/Project_Root_Directory/project/src/../build/release/plugins/libtarget_library.dll.a -o ../../../../build/release/plugins/target_library.dll object_script.target_library.Release  -lglu32 -lopengl32 -luser32 -LC:/msys64/workspace/Project_Root_Directory/project/build/release -LC:/msys64/workspace/Project_Root_Directory/project/third-party/out/lib -LC:/msys64/workspace/Project_Root_Directory/project/deps/lib -lexif -ldecimal -lsqlite3 -lutils -LC:/Python35/libs -lpython35 -lkmlbase -lkmlengine -lkmldom -lkmlxsd -lminizip -luriparser -lkmlconvenience -lcore -lrender -lraster -lgui -LC:/msys64/workspace/Project_Root_Directory/project/deps/lib/target_library/win64 -LC:/msys64/mingw64/lib  C:/msys64/mingw64/lib/libQt5Widgets.dll.a C:/msys64/mingw64/lib/libQt5WinExtras.dll.a C:/msys64/mingw64/lib/libQt5Gui.dll.a C:/msys64/mingw64/lib/libQt5Network.dll.a C:/msys64/mingw64/lib/libQt5Concurrent.dll.a C:/msys64/mingw64/lib/libQt5Xml.dll.a C:/msys64/mingw64/lib/libQt5Core.dll.a

collect2.exe: error: ld returned 5 exit status
make[4]: *** [Makefile.Release:159: ../../../../build/release/plugins/target_library.dll] Error 1
make[4]: Leaving directory '/workspace/Project_Root_Directory/project/src/modules/plugins/target_library'
make[3]: Leaving directory '/workspace/Project_Root_Directory/project/src/modules/plugins/target_library'
make[2]: *** [Makefile:384: sub-target_library-make_first] Error 2
make[2]: Leaving directory '/workspace/Project_Root_Directory/project/src/modules/plugins'
make[1]: *** [Makefile:297: sub-plugins-make_first] Error 2
make[1]: Leaving directory '/workspace/Project_Root_Directory/project/src/modules'
make: *** [Makefile:44: sub-modules-make_first] Error 2

gcc 7.1.0, Qt5 5.9.1, qmake, Win7 x64

Isahella answered 13/7, 2017 at 10:21 Comment(6)
You haven't posted the linker error diagnostic, just the "That didn't work. Goodbye" epilogue. Post the complete build log from the failing g++ ... command to the end.Usage
@MikeKinghan The complete build log takes more than 12Mb. It is a huge project. I suppose the link command itself is enough.Isahella
Just the complete build log from the failing g++ ... command to the end of the above.Usage
@MikeKinghan Linkage fails. It is in the original post. Before that there's a bunch of object files being successfully created.Isahella
I think a lot of people are upvoting this because we've heard of or seen this error before. Yes, sometimes ld just fails without giving an error message. Maybe you should just use a different programming environment. But if you really want to fix MSYS2, I'd probably start by reducing the test case as much as possible. Ideally, you'd just start with a few simple source files and run some GCC commands to produce the error. Then you could use -v to see exactly how gcc is invoking ld, and you could invoke it yourself. You could then debug ld using gdb (with a debug version of ld).Width
@DavidGrayson this is a good advice. Thank you.Isahella
S
5

I had the same issue. I link my project with static library. After mingw64 upgrade, my project stoped building with the same error. I found out that debug version built successfully. So i compared linker flags. In release version Qt add this flags "-Wl,-s,--relax,--gc-section". I couldn't find any information about "-s" flag, but due to this link i figured out that -gc-section flag was the reason of fail. I added the following code to my .pro-file, and it worked

mingw {
        contains(QT_ARCH, x86_64): {
            LIBS+=-Wl,--no-gc-sections
        }
}
Surplusage answered 14/7, 2017 at 15:56 Comment(2)
Apparently that was exactly it.Isahella
Weeeird. That linked for me, but refused to run afterwards. Adding QT += widgets did the trick for me.Decalescence
G
3

Just in case someone finds this due the same problem, please, add the following flag to your compiler -Wl,--verbose (or --verbose to the linker). This will show which library was not found or give you more information about the problem.

Grimbly answered 25/7, 2019 at 17:20 Comment(1)
Interestingly, adding --verbose to the linker fixed the error for me. If I leave it away again, the error comes back. xDLulu
C
0

I have the exact same issue with a custom GCC build. I analyzed the issue by performing different tests on different machines, debugging and analyzing the dumps generated by Windows. I could not find the exact cause, only workarounds. I paste here the knowledge I built up for reference.

  • The issue affects only the Windows (MingW) builds
  • Affected versions: GCC 11.3.1, Binutils 2.40
  • Moving g++ to another directory seems to solve the issue
  • Adding -Wl,-verbose solves the issue
  • Using gcc for linking solves the issue (only g++ is affected)

Analyzing the dumps generated by windows shows that the failing code belongs to the method lang_size_sections_1 in file ld/ldlang.c in the BinUtils. enter image description here

enter image description here

Cockney answered 13/9, 2023 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.