tcc: error: undefined symbol '_GetConsoleWindow#0' - c++

I'm making a program in C/C++ which must run hidden using this code:
#define _WIN32_WINNT 0x0500
#include <windows.h>
int main(){
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
. . .
}
I really want to use tinyc to compile it because it's much better than gcc (almost, the final executable is much tiny than gcc).
The point is that when I try to compile it using:
tcc PROGRAM.c -luser32
It makes an error which says:
tcc: error: undefined symbol '_GetConsoleWindow#0'
But when I use gcc it works! I think I have a missed library but I don't know which one.
Please, some help :)

According to MSDN, GetConsoleWindow is located in Kernel32.dll
Try:
tcc PROGRAM.c -luser32 -lkernel32
EDIT:
tcc's kernel32.def is missing the export for GetConsoleWindow.
Append the string GetConsoleWindow at the end of the def file located in the lib directory inside tcc's installation folder.

remove gdi32.def kernel32.def msvcrt.def user32.def files from C:\tcc\lib; remove all .def files from C:\tcc\lib
#include <windows.h>
#include <wincon.h>
#include <stdio.h>
#include <conio.h>
void main()
{
HWND hwnd;
hwnd = GetConsoleWindow();
HDC hdc;
hdc = GetWindowDC(hwnd);
printf("console hwnd: %p\n", hwnd);
printf("console hdc: %p\n", hdc);
HPEN hPenNull, hPenBlack, hPenRed, hPenGreen, hPenBlue;
hPenNull=GetStockObject(NULL_PEN);
hPenBlack=CreatePen(PS_SOLID, 2, RGB(0,0,0));
hPenRed=CreatePen(PS_SOLID, 2, RGB(255,0,0));
hPenGreen=CreatePen(PS_SOLID, 2, RGB(0,255,0));
hPenBlue=CreatePen(PS_SOLID, 2, RGB(0,0,255));
HBRUSH hBrushNull, hBrushBlack, hBrushRed, hBrushGreen, hBrushBlue, hBrushYellow;
hBrushNull=GetStockObject(NULL_BRUSH);
hBrushBlack=CreateSolidBrush(RGB(0,0,0));
hBrushRed=CreateSolidBrush(RGB(255,0,0));
hBrushYellow=CreateSolidBrush(RGB(255,255,0));
hBrushGreen=CreateSolidBrush(RGB(0,255,0));
hBrushBlue=CreateSolidBrush(RGB(0,0,255));
SelectObject(hdc, hPenRed);
SelectObject(hdc, hBrushYellow);
Ellipse(hdc, 200,50,260,150);
SelectObject(hdc, hPenNull);
SelectObject(hdc, hBrushRed);
Ellipse(hdc, 140, 80, 180, 120);
SelectObject(hdc, hPenBlue);
SelectObject(hdc, hBrushNull);
Ellipse(hdc, 280, 50, 340, 150);
getch();
}
-L"C:\tcc\lib" -lkernel32 -luser32 -lgdi32 -Wl,-subsystem=console

Related

What am I doing wrong? Program crashes. [GDI+, WINAPI, C++]

I wrote a program in C++ which has to print an image on the screen and exit after 2 seconds. Everything was ok until the program has to exit. When the program is on return 0; instruction, it crashes. I think it's because of wrong dealocation, but code seems to be correct.
#include <iostream>
#include <windows.h>
#include <gdiplus.h>
using namespace std;
using namespace Gdiplus;
HDC hdc = GetDC(NULL);
int main() {
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Graphics graphics(hdc);
Rect dst(500, 500, 17, 17);
Image* image = Image::FromFile(L"image.png", false);
cout<<"Image size: "<<image->GetWidth()<<", "<<image->GetHeight()<<endl;
graphics.DrawImage(image, dst);
delete image;
GdiplusShutdown(gdiplusToken);
Sleep(2000);
return 0;
}
I've got the newest compilator and I compile by:
g++ -c main.cpp -std=g++11
g++ -o main main.o -lgdiplus
graphics destructor is executed after GdiplusShutdown. you need enclose block with
Graphics graphics(hoc);
in { }

Properly Link Libraries in the Command Line

Preface: I'm fairly new to C++ and am just beginning to seriously program.
Post-Preface: I tried to post a link for the functions/pages I mention in this post, but Stack Overflow yelled at me because I don't have enough reputation to post more than two links.
I'm attempting to make a few simple GUIs in C++ with the Windows API using MinGW and the command line. I'm trying to change the window background, and one function which helps do this is the CreateSolidBrush function. This function requires the gdi32 library, but each time I try to compile/link to this library, I receive an error along the lines of "can't find that library, sucka".
Page1 and page2 provide useful information about MinGW's library functionality. Stack Overflow post # 5683058 and # 17031290 describe what I think are similar questions/problems to mine. I've searched far and wide for a simple and direct answer about how to link files/libraries from other directories (especially Windows libraries), but no luck in implementing the knowledge from these pages. Perhaps the answer is staring me right in the face, but my valiant efforts to "see the cat, draw the tiger" are in vain. It's possible that I'm entering the incorrect path/name (lib vs dll?), or maybe I'm completely overlooking something more fundamental (missing a header?). One command I've tried to use is
g++ -LC:\WINDOWS\System32 -lgdi32 gui.cpp
but this doesn't seem to work (note: source file named "gui.cpp").
Question 1: To start simple, what is the proper notation/command to link to individual header/source files which are not in the current directory?
Question 2: What is the proper notation/command to link to a library which is in the current directory?
Question 3: What is the proper notation/command to link to a library which is not in the current directory?
I realize these questions are sorta-kinda answered in a variety of ways on other pages, but they're often mingled with directions regarding Visual Studio, Eclipse, Code::Blocks, etc. and therefore unclear for the novices who forgo the luxuries of an IDE. I would appreciate a straightforward answer for your typical, run-of-the-mill noob. Many thanks in advance for any help or guidance.
I'll post my code, but I'm thinking only a couple of the first five lines are relevant:
#include <windows.h>
#include <string>
COLORREF desired_color = RGB(200,200,200);
HBRUSH hBrush = CreateSolidBrush(desired_color);
static char str_class_name[] = "MyClass";
static char str_titlebar[] = "My Window Title";
static int window_width = 300;
static int window_height = 300;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static HINSTANCE program_global_instance = NULL;
int WINAPI WinMain(HINSTANCE program_current_instance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
program_global_instance = program_current_instance;
WNDCLASSEX window_class;
HWND window_handle;
MSG window_message;
window_class.cbSize = sizeof(WNDCLASSEX); // size of struct; always set to size of WndClassEx
window_class.style = 0; // window style
window_class.lpfnWndProc = WndProc; // window callback procedure
window_class.cbClsExtra = 0; // extra memory to reserve for this class
window_class.cbWndExtra = 0; // extra memory to reserve per window
window_class.hInstance = program_global_instance; // handle for window instance
window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); // icon displayed when user presses ALT+TAB
window_class.hCursor = LoadCursor(NULL, IDC_ARROW); // cursor used in the program
window_class.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // brush used to set background color
window_class.lpszMenuName = NULL; // menu resource name
window_class.lpszClassName = str_class_name; // name with which to identify class
window_class.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // program icon shown in taskbar and top-left corner
if(!RegisterClassEx(&window_class)) {
MessageBox(0, "Error Registering Class!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
window_handle = CreateWindowEx(
WS_EX_STATICEDGE, // dwExStyle: window style
str_class_name, // lpClassName: pointer to class name
str_titlebar, // lpWindowName: window titlebar
WS_OVERLAPPEDWINDOW, // dwStyle: window style
CW_USEDEFAULT, // x: horizontal starting position
CW_USEDEFAULT, // y: vertical starting position
window_width, // nWidth: window width
window_height, // nHeight: window height
NULL, // hWndParent: parent window handle (NULL for no parent)
NULL, // hMenu: menu handle (Null if not a child)
program_global_instance, // hInstance : current window instance
NULL // lpParam -Points to a value passed to the window through the CREATESTRUCT structure.
);
if (window_handle == NULL) {
MessageBox(0, "Error Creating Window!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
ShowWindow(window_handle, nCmdShow);
UpdateWindow(window_handle);
while(GetMessage(&window_message, NULL, 0, 0)) {
TranslateMessage(&window_message);
DispatchMessage(&window_message);
}
return window_message.wParam;
}
// window_handle: window ID
// uMsg: window message
// wParam: additional message info; depends on uMsg value
// lParam: additional message info; depends on uMsg value
LRESULT CALLBACK WndProc(
HWND window_handle,
UINT Message,
WPARAM wParam,
LPARAM lParam
) {
switch(Message) {
case WM_CLOSE:
DestroyWindow(window_handle);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(window_handle, Message, wParam, lParam);
}
return 0;
}
Question 1: To start simple, what is the proper notation/command to link to individual header/source files which are not in the current directory?
That's not linking, that's compiling/including (unless you compile those source files to object files, first).
So:
gcc {other options} -o gui.exe gui.cpp /path/to/source_file_one.cpp /path/to/source_file_n.cpp
or, compile the others first:
gcc {other options} -c -o source_file_one.o /path/to/source_file_one.cpp
gcc {other options} -c -o source_file_n.o /path/to/source_file_n.cpp
gcc {other options} -o gui.exe source_file_n.o source_file_one.o gui.cpp
-c tells gcc not to try and link things together, as this is done in the last step.
{other options} can contain -I{include dirs} to inform gcc where to look when you #include something.
Question 2: What is the proper notation/command to link to a library which is in the current directory?
See 3; -L. should work.
Question 3: What is the proper notation/command to link to a library which is not in the current directory?
You're doing it right, so far: -L tells gcc about paths to look into for libraries, and -l{libname} links against a library.

GDI+ GdipCreateBitmapFromHBITMAP in C++

here is my function im trying to create bitmap from hbitmap here. But im getting identifier not found error.
__declspec(dllexport) void __stdcall testFunction()
{
int W = 860, H = 720;
int iLeft = 0, iRight = 860;
int iTop = 0, iBottom = 720;
int iW = iRight - iLeft, iH = iBottom - iTop;
HWND bluestacks = FindWindow(L"BlueStacksApp", L"BlueStacks App Player");
HDC bHDC = GetWindowDC(bluestacks);
HDC cHDC = CreateCompatibleDC(bHDC);
HBITMAP bmp = CreateCompatibleBitmap( bHDC, W, H);
HGDIOBJ sO = SelectObject( cHDC, bmp);
BOOL pW =PrintWindow( bluestacks, cHDC, 0);
SelectObject(cHDC, bmp);
BitBlt(cHDC, 0, 0, iW, iH, bHDC, iLeft, iTop, 0x00CC0020);
Bitmap newBitmap = GdipCreateBitmapFromHBITMAP(bmp);
}
Error code:
identifier "GdipCreateBitmapFromHBITMAP" is undefined
Note loaded header files:
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <string.h>
#include <strsafe.h>
#include <gdiplus.h>
#include <Gdiplusflat.h> // Gdiplusflat.h
#include <ostream>
The function is defined as follows:
GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm,
HPALETTE hpal,
GpBitmap** bitmap);
if you want to get a Bitmap object from a HBITMAP, maybe you shoud use the constructor
Bitmap(HBITMAP,HPALETTE);
for your code it should be:
Bitmap newBitmap(bmp,NULL);
BTW:before use this, you should initialize GDIPLUS.
works for me, after adding additional option to linker -lgdiplus -mwindows
g++ test.c -lgdiplus -mwindows

C++ Pixels In Console Window

In C++ using Code::Blocks v10.05, how do I draw a single pixel on the console screen? Is this easy at all, or would it be easier to just draw a rectangle? How do I color it?
I'm sorry, but I just can't get any code from SOF, HF, or even cplusplus.com to work. This is for a Super Mario World figure on the screen. The game I think is 16-bit, and is for the SNES system. C::B says I need SDK for C::B. It says "afxwin.h" doesn't exist. Download maybe?
This is what I'm trying to make:
It depends on your OS. I suppose you are programming in a Windows platform, therefore you can use SetPixel but you have to use "windows.h" to get a console handle, so here an example for drawing the cos() function:
#include<windows.h>
#include<iostream>
#include <cmath>
using namespace std;
#define PI 3.14
int main()
{
//Get a console handle
HWND myconsole = GetConsoleWindow();
//Get a handle to device context
HDC mydc = GetDC(myconsole);
int pixel =0;
//Choose any color
COLORREF COLOR= RGB(255,255,255);
//Draw pixels
for(double i = 0; i < PI * 4; i += 0.05)
{
SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
pixel+=1;
}
ReleaseDC(myconsole, mydc);
cin.ignore();
return 0;
}
You can also use some others libraries like: conio.h allegro.h sdl, etc.
If you're willing to have the image look blocky, you could take advantage of the block characters from the console code page.
█ = '\xDB' = U+2588 FULL BLOCK
▄ = '\xDC' = U+2584 LOWER HALF BLOCK
▀ = '\xDF' = U+2580 UPPER HALF BLOCK
and space
By using the half-blocks in combination with colored text, you can turn an 80×25 console window into an 80×50 16-color display. (This was the approach used by the QBasic version of Nibbles.)
Then, you just need to convert your image to the 16-color palette and a reasonably small size.
windows.h provides a function SetPixel() to print a pixel at specified location of a window. The general form of the function is
SetPixel(HDC hdc, int x, int y, COLORREF& color);
where, x and y are coordinates of pixel to be display and color is the color of pixel.
Important: to print the pixel in your machine with Code::blocks IDE, add a link library libgdi32.a (it is usually inside MinGW\lib ) in linker setting.
Console is a text device, so in general you don't write to individual pixels. You can create a special font and select it as a font for console, but it will be monochromatic. There are libraries which simplify writing console UI (e.g. Curses), but I believe that you also have more gamelike functionality in mind besides just showing a sprite.
if you want to write a game, I suggest taking a look at some of the graphics/game frameworks/libs, e.g. SDL
I have drawn the straight line using windows.h in code::blocks. I can't explain it in details, but I can provide you a code and procedure to compile it in code::blocks.
go to setting menu and select compiler and debugger.
Click on linker tab and add a link library libgdi32.a which is at C:\Program Files\CodeBlocks\MinGW\lib directory.
Now compile this program
#include <windows.h>
#include <cmath>
#define ROUND(a) ((int) (a + 0.5))
/* set window handle */
static HWND sHwnd;
static COLORREF redColor=RGB(255,0,0);
static COLORREF blueColor=RGB(0,0,255);
static COLORREF greenColor=RGB(0,255,0);
void SetWindowHandle(HWND hwnd){
sHwnd=hwnd;
}
/* SetPixel */
void setPixel(int x,int y,COLORREF& color=redColor){
if(sHwnd==NULL){
MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR);
exit(0);
}
HDC hdc=GetDC(sHwnd);
SetPixel(hdc,x,y,color);
ReleaseDC(sHwnd,hdc);
return;
// NEVERREACH //
}
void drawLineDDA(int xa, int ya, int xb, int yb){
int dx = xb - xa, dy = yb - ya, steps, k;
float xIncrement, yIncrement, x = xa, y = ya;
if(abs(dx) > abs(dy)) steps = abs(dx);
else steps = abs(dy);
xIncrement = dx / (float) steps;
yIncrement = dy / (float) steps;
setPixel(ROUND(x), ROUND(y));
for(int k = 0; k < steps; k++){
x += xIncrement;
y += yIncrement;
setPixel(x, y);
}
}
/* Window Procedure WndProc */
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
switch(message){
case WM_PAINT:
SetWindowHandle(hwnd);
drawLineDDA(10, 20, 250, 300);
break;
case WM_CLOSE: // FAIL THROUGH to call DefWindowProc
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
break; // FAIL to call DefWindowProc //
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){
static TCHAR szAppName[] = TEXT("Straight Line");
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW|CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
// Register the window //
if(!RegisterClass(&wndclass)){
MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR);
exit(0);
}
// CreateWindow //
HWND hwnd=CreateWindow(szAppName,"DDA - Programming Techniques",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if(!hwnd){
MessageBox(NULL,"Window Creation Failed!","Error",MB_OK);
exit(0);
}
// ShowWindow and UpdateWindow //
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
// Message Loop //
MSG msg;
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
/* return no error to the operating system */
return 0;
}
In this program I have used DDA line drawing algorithm. Pixel drawing tasks is done by setPixel(ROUND(x), ROUND(y)) function.
This is windows programing which you can learn details here
To use in CodeBlocks I found this (you have to add a linker option -lgdi32):
//Code Blocks: Project Build Options Linker settings Othoer linker options: add -lgdi32
I forgot: You have to put this before including windows.h:
#define _WIN32_WINNT 0x0500
The whole cosine code again. Ready to compile:
//Code Blocks: Project Build Options Linker settings Othoer linker options: add -lgdi32
#define _WIN32_WINNT 0x0500
#include "windows.h"
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14
int main(){
HWND myconsole = GetConsoleWindow();
HDC mydc = GetDC(myconsole);
int pixel =0;
COLORREF COLOR= RGB(255,255,255);
//Draw pixels
for(double i = 0; i < PI * 4; i += 0.05)
{
SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
pixel+=1;
}
ReleaseDC(myconsole, mydc);
cin.ignore();
return 0;
}

undefined reference to `TextOutA#20' [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I am kind of stuck on this and would appreciate help in solving this please. I am using window GDI, and according to this link, including windows.h is what is needed to work with the TextOut function, among others, but I am still getting some undefined reference error messages and I am stuck.
**undefined reference to `TextOutA#20'|
undefined reference to `CreateDCA#16'|
undefined reference to `Escape#20'|
undefined reference to `DeleteDC#4'|**
#include <Windows.h>// HDC is a typedef defined in the windows.h library
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <strstream>
#include <string.h>
using namespace std;
#define IDM_QUIT 102
#define EXAMPLE 101
#define IDM_PRINT 27
#define IDM_SHOW 1
//this excerpt demonstrates how to get the handle to a device context ysing the BegingPaint() GDI function
long FAR PASCAL _export WINDOWPROCEDURE(HWND hwnd, UINT message, UINT wParam, LONG Param)
{
HDC hdc;
PAINTSTRUCT ps;
RECT area;
char PInfo[80]="";
char DriverName[20];
char DeviceName[40];
char Other[20];
int X,Y;
X=100;
Y=100;
switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc,X,Y, "Speed of Light", 14);
EndPaint(hwnd, &ps);
case IDM_SHOW:
hdc = GetDC(hwnd);
TextOut(hdc, X,Y, "Speed of Light", 14);
ReleaseDC(hwnd, hdc);
case IDM_PRINT:
GetProfileString("windows", "device", "", PInfo,80);
istrstream In(PInfo);
In.getline(DeviceName, 80, ',');
In.getline(DriverName, 80, ',');
In.getline(Other,80, ',');
hdc = CreateDC(DriverName, DeviceName, Other, NULL);
Escape(hdc,
STARTDOC,8,
(LPSTR)"EXAMPLE",0L);
TextOut(hdc, X, Y, "Speed of Light", 14);
Escape(hdc, NEWFRAME, 0,0L, 0L);
Escape(hdc, ENDDOC, 0,0L, 0L);
DeleteDC(hdc);
}
return 0;
}
Linking to gdi32.lib should solve the problem. :)
Ohh friend,if you have faced this type of error in dev cpp,then go to tool->compliler option->
click on the settings tab ->linker->change dont create console window to "YES"->CLICK OK.Your problem may be solved.