[Error] Id returned 1 exit status

I get the error message "[Error] Id returned 1 exit status" on compiling the code below (not mine - copied and pasted)
There is also a message "test1.cpp:(text+0x2f9): undefined reference to '_imp_GetStockObject'

I am compiling in Dev-C ++ 5.11.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  #include <windows.h>

#define IDC_MAIN_BUTTON 101			// Button identifier
#define IDC_MAIN_EDIT 102			// Edit box identifier

HWND hEdit;

LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
	WNDCLASSEX wClass;
	ZeroMemory(&wClass,sizeof(WNDCLASSEX));
	wClass.cbClsExtra=NULL;
	wClass.cbSize=sizeof(WNDCLASSEX);
	wClass.cbWndExtra=NULL;
	wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
	wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wClass.hIcon=NULL;
	wClass.hIconSm=NULL;
	wClass.hInstance=hInst;
	wClass.lpfnWndProc=(WNDPROC)WinProc;
	wClass.lpszClassName="Window Class";
	wClass.lpszMenuName=NULL;
	wClass.style=CS_HREDRAW|CS_VREDRAW;

	if(!RegisterClassEx(&wClass))
	{
		int nResult=GetLastError();
		MessageBox(NULL,
			"Window class creation failed\r\n",
			"Window Class Failed",
			MB_ICONERROR);
	}

	HWND hWnd=CreateWindowEx(NULL,
			"Window Class",
			"Windows application",
			WS_OVERLAPPEDWINDOW,
			200,
			200,
			640,
			480,
			NULL,
			NULL,
			hInst,
			NULL);

	if(!hWnd)
	{
		int nResult=GetLastError();

		MessageBox(NULL,
			"Window creation failed\r\n",
			"Window Creation Failed",
			MB_ICONERROR);
	}

    ShowWindow(hWnd,nShowCmd);

	MSG msg;
	ZeroMemory(&msg,sizeof(MSG));

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
		case WM_CREATE:
		{
			// Create an edit box
			hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
				"EDIT",
				"",
				WS_CHILD|WS_VISIBLE|
				ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
				50,
				100,
				200,
				100,
				hWnd,
				(HMENU)IDC_MAIN_EDIT,
				GetModuleHandle(NULL),
				NULL);
			HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
			SendMessage(hEdit,
				WM_SETFONT,
				(WPARAM)hfDefault,
				MAKELPARAM(FALSE,0));
			SendMessage(hEdit,
				WM_SETTEXT,
				NULL,
				(LPARAM)"Insert text here...");

			// Create a push button
			HWND hWndButton=CreateWindowEx(NULL,
				"BUTTON",
				"OK",
				WS_TABSTOP|WS_VISIBLE|
				WS_CHILD|BS_DEFPUSHBUTTON,
				50,
				220,
				100,
				24,
				hWnd,
				(HMENU)IDC_MAIN_BUTTON,
				GetModuleHandle(NULL),
				NULL);
			SendMessage(hWndButton,
				WM_SETFONT,
				(WPARAM)hfDefault,
				MAKELPARAM(FALSE,0));
		}
		break;

		case WM_COMMAND:
			switch(LOWORD(wParam))
            {
				case IDC_MAIN_BUTTON:
				{
					char buffer[256];
					SendMessage(hEdit,
						WM_GETTEXT,
						sizeof(buffer)/sizeof(buffer[0]),
						reinterpret_cast<LPARAM>(buffer));
					MessageBox(NULL,
						buffer,
						"Information",
						MB_ICONINFORMATION);
				}
				break;
			}
			break;

		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}
		break;
	}

	return DefWindowProc(hWnd,msg,wParam,lParam);
}


Any ideas as to what is wrong?
There is also a message "test1.cpp:(text+0x2f9): undefined reference to '_imp_GetStockObject'

That is wrong.

The code line 93 calls function GetStockObject. It is not defined within this file. There is no compiler error. Therefore, compiler sees the declaration of that function within included header file 'windows.h' (or in headers included therein).

It is the linker that sees an error. No object file included in linking (object file created from this source code by compiler, nor any of the libraries) contains the compiled implementation of that function. Apparently not all necessary libraries are included in the linking command.

If the function is from Microsoft, then MSDN should have its description and details.


It is not Id, but ld that complains.


PS. Dev-C++ is not a compiler. It is an IDE. It does use some C++ compiler. A compiler is probably distributed with it, but the user can replace the compiler.
Sorry, I forget that Dev-C ++ isn't an actual compiler. It's set up with the GCC compiler.

As I said, the code isn't mine. I copied and pasted it from a tutorial. It could be that the tutorial was aimed at Visual C++.

I don't have Visual C++, but I've tried it with a couple of other compilers and several other error messages and warnings have come up. Maybe I'll have to think again and build up the code myself.

Ayway, thank you very much for your response. It WAS helpful to me.
The library seems to be "gdi32":
https://msdn.microsoft.com/en-us/library/vs/alm/dd144925%28v=vs.85%29.aspx


A library can be either static or dynamic. Static library is just a collection of object code and linker effectively copies code into executable during linking. Statically linked executable is a large monolith, but self-sufficient.

Dynamic library is object code too, but rather than copying actual code, the linker adds call code "stubs" into the executable. When the executable (your program) is run, the library file is linked in and code is executed from it. Thus, the executable file does not work without the library file(s). The executable file is smaller and many programs may use same library; less duplication.

In *nix a dynamic library has extension .so
In OS X a dynamic library has extension .dyld
In Windows a dynamic library has extension .dll, but there is also a .lib file created with the dll that is a small static library containing the stubs thatthe linker inserts into the executable for the executable to link the dll.

The linker of GCC in Windows uses some mixture of library types.
I'm just installing the Community Edition of Visual Studio, so I'll check it out in Visual C++.

I'm new to C++, but in other languages, unless it is one of the core Windows libraries, there is usually code that defines the library so it can be used. I can't see that in the code. Maybe I'm trying to run before I can walk!

Thank you adain. I'm learning!
Topic archived. No new replies allowed.