2

I am trying to use the SetCursorPos function to move my cursor in Windows 10 with C++.

Here is my code:

#include <Windows.h>
int main()
{
    SetCursorPos(100,100);
    return 0;
}

Whenever I run the code, I get this error:

undefined reference to `SetCursorPos@8'

I have read through What is an undefined reference/unresolved external symbol error and how do I fix it?, but I could not find a solution to my problem.

I am sure that I missed something, but I have no clue to what that something is; sorry if the answer is posted elsewhere.

wwwwwwww
  • 33
  • 4
  • 1
    Do you link to User32.lib? – tkausl Sep 16 '18 at 18:19
  • Undefined reference == you are not (correctly) linking with whatever defines a symbol you are using. That duplicate you link to explains that quite well IMHO. – Jesper Juhl Sep 16 '18 at 18:25
  • I tried `g++ movecursor.cpp -Luser32.lib` but the error still remained. I have no idea what I am supposed to link; sorry but can you expand on what I should try to link? – wwwwwwww Sep 16 '18 at 18:44
  • are you sure that `SetCursorPos@8` exactly ? not `_SetCursorPos@8` or `__imp__SetCursorPos@8` ? because symbol `SetCursorPos@8` not exist in any standard windows SDK libs – RbMm Sep 16 '18 at 18:50
  • Yes, [I can confirm](https://puu.sh/Bw33Y.png) that the error is `SetCursorPos@8` exactly. Does this mean I am calling the wrong function? – wwwwwwww Sep 16 '18 at 18:53
  • no, this confirm that you use not microsoft toolset (*cl.exe* and *link.exe*) probably you need use special libs for g++, different from user32.lib from sdk. anyway - search `SetCursorPos` symbol in your lib - what form it have here ? (with exactly prefix and suffix) – RbMm Sep 16 '18 at 18:56

1 Answers1

2

Microsoft ships the SetCursorPos function inside user32 library (see MSDN)

If you use Microsoft Visual C++ compiler, you can add statically that library to your project using the name "user32.lib".

If you use GCC instead, the name has another extension: "user32.a". Normally the ".a" extension is defaulted in GCC, so no need to pass it to the compiler.

If you were to add libraries that are not in a GCC-knowing path, then you need the "-L" flag to tell GCC where to look for that libraries. The "-l" flag (lowercase L) tells GCC to use that library.

Sumarizing:

g++ movecursor.cpp -luser32
Ripi2
  • 7,031
  • 1
  • 17
  • 33