Home c++ “ ld returned 1 exit status” constantly, from different implementations of functions...

“[Error] ld returned 1 exit status” constantly, from different implementations of functions making the screen

Author

Date

Category

We needed a function that takes a screenshot in the program and saves it to a file. I found several such functions, but each (!) Of them, when added to the program, causes it to issue the following error during compilation:

C: \ {file_folder.cpp} \ collect2.exe [Error] ld returned 1 exit status

And there are no other errors, or I am fixing them. Usually, this error occurs when my code has already been compiled and running .exe, and I try to compile and run it again. But, of course, I tried everything, even ran the code on another computer (but also in Dev) – the same error.

If you remove this function, then the code works fine again. Tell me what to do?

Here is the current code with one of the function options that takes a screenshot:

# include & lt; Winsock2.h & gt; // Ws2_32.lib
#include & lt; ws2tcpip.h & gt;
#include & lt; Windows.h & gt;
#include & lt; iostream & gt;
#include & lt; stdio.h & gt;
#include & lt; stdlib.h & gt;
#include & lt; conio.h & gt; // getch
#include & lt; string.h & gt;
#include & lt; fcntl.h & gt;
#include & lt; unistd.h & gt;
#include & lt; sys / types.h & gt;
#include & lt; sys / time.h & gt;
// # include & lt; sys / socket.h & gt;
#define ever (;;)
#include & lt; gdiplus.h & gt;
#pragma comment (lib, "GdiPlus.lib")
// Any other code here. In general, a program for working with a network (this is a client).
// But it doesn't even start, throws that error.
using namespace std;
using namespace Gdiplus;
static const GUID png =
{0x557cf406, 0x1a04, 0x11d3, {0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e}};
int PrtScr ()
{
  GdiplusStartupInput gdiplusStartupInput;
  ULONG_PTR gdiplusToken;
  GdiplusStartup (& amp; gdiplusToken, & amp; gdiplusStartupInput, NULL);
  HDC scrdc, memdc;
HBITMAP membit;
  scrdc = GetDC (0);
  int Height, Width;
  Height = GetSystemMetrics (SM_CYSCREEN);
  Width = GetSystemMetrics (SM_CXSCREEN);
  memdc = CreateCompatibleDC (scrdc);
  membit = CreateCompatibleBitmap (scrdc, Width, Height);
  SelectObject (memdc, membit);
  BitBlt (memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
  HBITMAP hBitmap;
  hBitmap = (HBITMAP) SelectObject (memdc, membit);
   Gdiplus :: Bitmap bitmap (hBitmap, NULL);
   char PerMin [12];
   strcpy (PerMin, "screen.png");
  bitmap.Save ((WCHAR *) PerMin, & amp; png, NULL);
  DeleteObject (hBitmap);
  return 0;
}
//////////////////////////////////////////////// ///
int main ()
{
  printf ("OK1 \ n");
  getch ();
  PrtScr ();
  printf ("OK2 \ n");
  getch ();
  // Any other code here
  return 0;
}

Answer 1, authority 100%

ld returned 1 exit status usually means that you have invalid characters in your code. They can be there even if they are not visible, for example, the Russian letter e does not differ from the English one e at first glance. This is usually the result of copy-paste. Since the function is small, the easiest way for you to solve the problem is to rewrite it (or rather, all copy-paste) by hand.

Update
Also, an unconnected library can be a problem. You need to look at the list of included header files, which libraries they refer to and add all missing libraries in the project settings.

An unconnected header file may also be a problem. Here you need to look at the names of the functions used and look for the headers where they are declared.


Answer 2, authority 20%

With an unconnected library or a typo in the method definition, the linker always indicates which method is undefined. By his name, you can stamp where he is identified and link the lib.

I ran into the same problem without any additional information from the linker, I also scratched my turnips for a long time and looked for typos, but in the end I noticed that the disk where I’m going is not enough space. I transferred the source code to a larger drive – it was immediately linked normally.
Perhaps the solution to the problem by AlfredBauer is related to the same, since 32-bit binaries take up less space.

In general, if there is not enough space at the linking stage, a similar message may be displayed without explanatory information.
I caught it on CentOS 6.9, g ++ 4.8.2, glibc 2.12.


Answer 3

I solved this problem by changing the compilation mode from x64 to x32 . At the same time, I had additional libraries connected (which were compiled by MinGW), with which the compiler apparently could not work in this mode.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions