alex parker - 2022-06-22

hi

i want to write an inline asm code and call a windows API in it.
i am using code::blocks with Mingw, in windows environment and i get this error while trying to compile a x86 version of my code:

undefined reference to `MessageBoxA'

my x86 code:

#include <iostream>
#include <windows.h>
#include <winuser.h>

char * msg = "Hello, World!\n";
char * wMsg = "Content of the window..";
char * const wCaption = "Window title";
char * const wCaption222 = "Window title";
int var = 999999999999999;


int main (){
  std::cout << "before call" << "\n";

   asm(
    "movl $0, %%ecx\n\t"
    "movl %2, %%edx\n\t"
    "movl %4, %%ebx\n\t"
    "movl $0, %%eax\n\t"
    "pushl %%eax\n\t"
    "pushl %%ebx\n\t"
    "pushl %%edx\n\t"
    "pushl %%ecx\n\t"
    "calll MessageBoxA\n\t"
    : "=r" (var)
    : "r" (msg), "r" (wMsg) , "r" (wCaption), "r" (wCaption222)
    : "cc");

    std::cout << var << "\n";
    std::cout << "after call" << "\n";
}

i tried Mingw64, Mingw32 and Mingw multi libs, none worked for me.
i tried packages from official site, modified ones and installing with Msys2 (i don't know if i correctly installed the x86 one with Msys2 or not)

it's either i don't know how to properly add the libs or its that i don't have the right package.

interesting part is that i don't have any problem while compiling my x64 asm code with Mingw64; it compiles and runs fine:

#include <iostream>

char * msg = "Hello, World!\n";
char * wMsg = "Content of the window..";
char * const wCaption = "Window title";

char * const wCaption222 = "Window title";
int var = 999999999999999;

int main (){
  std::cout << "before call" << "\n";

   asm(
    "movq $0, %%rcx\n\t"
    "movq %2, %%rdx\n\t"
    "movq %4, %%r8\n\t"
    "movq $0, %%r9\n\t"
    "callq MessageBoxA\n\t"
    : "=r" (var)
    : "r" (msg), "r" (wMsg) , "r" (wCaption), "r" (wCaption222)
    : "cc");

    std::cout << var << "\n";
    std::cout << "after call" << "\n";
}
 

Last edit: alex parker 2022-06-22