Need help importing a function from a DLL

Can anyone help me with this problem? Basically, I'm trying to call the function 'MessageBoxA' without including windows.h (and by rather linking to 'libuser32.a'). Can this be done?

The following gives me an error ([Linker error] undefined reference to 'MessageBoxA'):

extern "C" int MessageBoxA(int hWnd,const char* lpText,const char* lpCaption,unsigned int uType);

int main()
{
MessageBoxA(0,"Test","Test",0);
return 0;
}

I have already tried adding 'pragma comment(lib, "libuser32.a")'.
Could it be an issue of how I compile it? I have tried it both under Dev-C++ (I included the library under 'Project Options'->'Linker') and under Command Prompt (gcc -Os -s Debug.cpp -luser32).

Thanks in advance!

Note: I am using MinGW as my compiler.

Comments

  • : Can anyone help me with this problem? Basically, I'm trying to call the function 'MessageBoxA' without including windows.h (and by rather linking to 'libuser32.a'). Can this be done?
    :
    : The following gives me an error ([Linker error] undefined reference to 'MessageBoxA'):
    :
    : extern "C" int MessageBoxA(int hWnd,const char* lpText,const char* lpCaption,unsigned int uType);
    :
    : int main()
    : {
    : MessageBoxA(0,"Test","Test",0);
    : return 0;
    : }
    :
    : I have already tried adding 'pragma comment(lib, "libuser32.a")'.
    : Could it be an issue of how I compile it? I have tried it both under Dev-C++ (I included the library under 'Project Options'->'Linker') and under Command Prompt (gcc -Os -s Debug.cpp -luser32).
    :
    : Thanks in advance!
    :
    : Note: I am using MinGW as my compiler.
    :
    You still need to include the windows.h file. Even though the function resides in an external library, the compiler still needs to know what the function looks like when it is compiling your program - to figure out what piece of code to link to. The undefined reference is telling you that the compiler doesn't know what you are talking about since it hasn't been told what MessageBoxA is or where to find it.

  • : : Can anyone help me with this problem? Basically, I'm trying to call the function 'MessageBoxA' without including windows.h (and by rather linking to 'libuser32.a'). Can this be done?
    : :
    : : The following gives me an error ([Linker error] undefined reference to 'MessageBoxA'):
    : :
    : : extern "C" int MessageBoxA(int hWnd,const char* lpText,const char* lpCaption,unsigned int uType);
    : :
    : : int main()
    : : {
    : : MessageBoxA(0,"Test","Test",0);
    : : return 0;
    : : }
    : :
    : : I have already tried adding 'pragma comment(lib, "libuser32.a")'.
    : : Could it be an issue of how I compile it? I have tried it both under Dev-C++ (I included the library under 'Project Options'->'Linker') and under Command Prompt (gcc -Os -s Debug.cpp -luser32).
    : :
    : : Thanks in advance!
    : :
    : : Note: I am using MinGW as my compiler.
    : :
    : You still need to include the windows.h file. Even though the function resides in an external library, the compiler still needs to know what the function looks like when it is compiling your program - to figure out what piece of code to link to. The undefined reference is telling you that the compiler doesn't know what you are talking about since it hasn't been told what MessageBoxA is or where to find it.
    :
    :
    [blue]Or you can copy/paste the MessageBoxA() prototype from windows.h into your code, but you need it anyhow. Also, remember, that including whole windows.h does not add even a byte to your final EXE.[/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories