c++assemblywinapix86mingw32

I get an error trying to compile assembly code/C++ with G++


So i wrote a small program just to test if everything is working. It should take two inputs und output them summed up.

test.cpp:

#include <iostream>
#include <stdio.h>

extern "C" int test(int a, int b);

int main(){
    int x = 0;
    std::cout << test(10, 20);
    std::cin >> x;
    return 0;
}

test.s:

.global test

test:

    mov %eax, %ecx
    add %eax, %edx

    ret

I then tried compiling it with g++: g++ -o main.exe test.cpp test.s But i get an error: undefined reference to `test'

I am completely new to programming with assembly. Any advice?


Solution

  • You have to include test.s file in your build command:

    g++ -o main.exe test.cpp test.s
    

    otherwise the compiler will complain that the test function is undefined because it cannot find it