It took 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 produce such an error when compiling:

C:\{папка_с_файлом.cpp}\collect2.exe [Error] ld returned 1 exit status

And there are no other mistakes, or I correct them. Usually such an error occurs when my code is already 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, the code works fine again. Tell me what to do?

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

 #include <Winsock2.h>//Ws2_32.lib #include <ws2tcpip.h> #include <Windows.h> #include <iostream> #include <stdio.h> #include <stdlib.h> #include <conio.h> //getch #include <string.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/time.h> //#include <sys/socket.h> #define ever (;;) #include <gdiplus.h> #pragma comment(lib, "GdiPlus.lib") //Тут всякий другой код. Вообще, программа для работы с сетью (это клиент). //Но она даже не запускается, выдаёт ту ошибку. 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(&gdiplusToken, &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, &png,NULL); DeleteObject(hBitmap); return 0; } ///////////////////////////////////////////////////// int main() { printf ("OK1\n"); getch(); PrtScr(); printf ("OK2\n"); getch(); //Тут всякий иной код return 0; } 
  • The full log of the assembly, I think, will help you better understand what is happening with you. - Vladimir Martyanov
  • Did you forget to authenticate Ws2_32.lib? - VladD
  • @VladD, and this is perhaps the reason? After all, these are libraries that relate to advice, but not to getting a screenshot - Vladimir
  • #include <Winsock2.h> : Well, you have #include <Winsock2.h> , which means that maybe functions are used from there somewhere. - VladD

3 answers 3

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

Update Also, the problem may be an unconnected library. It is necessary to look at the list of connected header files, which libraries they belong to and add all missing libraries to the project settings.

The problem may be some kind of unconnected header file. Here it is necessary to look at the names of the functions used and look for the headers where they are declared.

  • thank! I'll try! True, it is strange that this happened with several different implementations of this function, well, anything can happen) - Vladimir
  • Perhaps the very first option was to blame. If they were inserted into the same file, something could remain of it. It is better, generally speaking, to try in a completely new file. Another (but, in my opinion, less likely) it may be that the site is to blame, if the examples were from one site, or the browser, which added something extra to the texts. - Vladimir
  • Slightly supplemented the answer, there may still be a problem in that you did not connect any necessary library. - Vladimir
  • I tried in different files and on different computers (but in Dev), and from different sites. At the expense of libraries, it is strange, but will it not give an error that something (type, function, etc.) is not described? If the required library or header file is not connected? And yet: I do not have a project, but just a separate program, and I don’t have the project settings. But, probably, it is possible to connect libraries and in the IDE. I understand that I need to go into all the included files (which are related to the screenshot function) and look for all connections ending in .lib and connect them somewhere in the IDE settings? - Vladimir
  • No, you need on MSDN to find the description of each header file or the function used and connect the libraries specified in the description in the project settings (or on the command line when launching the linker). - Vladimir

When a library is not connected or a typo in the method definition, the linker always indicates which particular method is undefined. By his name you can get where he is defined and authenticate.

I ran into the same problem without any additional information from the linker, I also scratched my turnips for a long time and searched for typos, but in the end I noticed that there was not enough space on the disk where I was going. Transferred the source code to the drive more - there it was immediately normal linkage. Perhaps the solution to AlfredBauer’s problem is 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 can be displayed without explanatory information. I caught it on CentOS 6.9, g ++ 4.8.2, glibc 2.12.

    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.