开发者

Why does using GetPixel result in an "undefined reference"?

#include <iostream>
#include <windows.h>
#incl开发者_Go百科ude <string>
#include <ctime>
#include<stdio.h> 

using namespace std;

int main()
{
    HDC dc = GetDC(NULL); 
    COLORREF color = GetPixel(dc, 10, 10); 
    ReleaseDC(NULL, dc);
    cout << color;

    return 0;
}

There's a error said :

[Linker error] undefined reference to `GetPixel@12'   
 ld returned 1 exit status    
[Build Error]  [Project1.exe] Error 1

I m using Dev-C++ complier


You need to add Gdi32.lib to your dependency list. The GetPixel() function is not in the default libraries.

EDIT:

In Visual Studio, you can add dependencies like this:

Menu: Project -> Properties -> Configuration Properties -> Linker -> Input

The "Additional Dependencies" option will look like this:

kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)

Add Gdi32.lib to it.


I was given that error and I solved it by scrapping Visual Studio Code as my IDE and switching over to Visual Studio 2019. VS19 solved my dependency error automatically! I hope this helps somebody!


In order to use the Win32 API you need to create a Windows Application.

This means using a WinMain function instead of a main() function.

It can (optional) also include creating a window and processing window messages with a window procedure.

After you do that, then you will be able to call the Win32 API GetPixel.

If you insist on creating a console or non-windows application, then you would have to define your own GetPixel which would probably access video memory directly.

So, start by creating a WinMain function instead of a main(). (Of course you'll need to link to the appropriate libraries as well.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜