Thread: Link problem when compiling a x86 Inline asm code with Mingw

  1. #1
    Registered User
    Join Date
    Jun 2022
    Posts
    3

    Link problem when compiling a x86 Inline asm code with Mingw

    hi all

    i want to write an inline asm code and call a windows API in it.
    i am using code::blocks with Mingw and i get this error while trying to compile a x86 version of my code:
    Code:
    undefined reference to `MessageBoxA'
    my x86 code:
    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:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    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";
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Compile with the -c flag, then use the nm tool to find out what the external names actually look like.
    Code:
    $ g++ -c foo.cpp
    $ nm -u foo.o
                     U __cxa_atexit
                     U __dso_handle
                     U _GLOBAL_OFFSET_TABLE_
                     U MessageBoxA
                     U _ZNSolsEi
                     U _ZNSt8ios_base4InitC1Ev
                     U _ZNSt8ios_base4InitD1Ev
                     U _ZSt4cout
                     U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    You might need an initial underscore
    "calll _MessageBoxA\n\t"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2022
    Posts
    3
    Quote Originally Posted by Salem View Post
    Compile with the -c flag, then use the nm tool to find out what the external names actually look like.
    Code:
    $ g++ -c foo.cpp
    $ nm -u foo.o
                     U __cxa_atexit
                     ...
                     U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    You might need an initial underscore
    "calll _MessageBoxA\n\t"
    i cant believe i was stubling with an issue being this easy to solve.
    i already tried _MessageBoxA, no use.
    already tried MessageBoxA@16 too; no use.

    but now, i hopelessly mixed them; _MessageBoxA@16, and it worked.

    thanks for your help.

    now, i have another request, and thats how can i find the full list of APIs + number of bytes taken up by their parameters e.g number 16 in above example.

  4. #4
    Registered User
    Join Date
    Jun 2022
    Posts
    3
    i cant believe it was this simple:
    i have already tried prefixing with under score and also calling like MessageBoxA@16. i did these seperately.
    but now, in absolute hopelessness, i mixed them e.g calll _MessageBoxA@16; compiled successfully.

    now i have another favor to ask, is there any list or index anywhere to have all win API + there respective parameter size?

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Probably call *__imp_MessageBoxA(%%rip).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to link libraries to MinGW?
    By ahernan17 in forum C Programming
    Replies: 10
    Last Post: 10-23-2010, 12:35 PM
  2. (MinGW)I don't know how to link files together
    By hchingwong in forum C Programming
    Replies: 3
    Last Post: 09-20-2010, 11:16 PM
  3. Inline asm with mingw
    By Ducky in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2009, 12:39 PM
  4. Need help compiling example code (MinGW and DirectX9)
    By scwizard in forum Game Programming
    Replies: 10
    Last Post: 07-25-2008, 10:43 AM
  5. Problem Code: Inline
    By CodingFleet in forum C++ Programming
    Replies: 6
    Last Post: 07-25-2007, 11:19 AM

Tags for this Thread