Unresolved external symbol C ++ with assembler
I am trying to make a function in assembler so that I can use it in C. My problem is that I cannot compile my code. I am using Visual Studio 2012. I am adding masm to the custiomization assembly so I have no idea what is wrong. FROM:
#include <stdio.h>
int szukaj_max (int a, int b, int c);
int main()
{
int x, y, z, wynik;
printf("\nProszępodaćtrzy liczby całkowite: ");
scanf_s("%d %d %d", &x, &y, &z, 32);
wynik = szukaj_max(x, y, z);
printf("\nSpośród podanych liczb %d, %d, %d, \ liczba %d jest największa\n", x,y,z, wynik);
return 0;
Assembly:
.686
.model flat
public _szukaj_max
.code
_szukaj_max PROC
push ebp ; zapisanie zawartości EBP na stosie
mov ebp, esp ; kopiowanie zawartości ESP do EBP
mov eax, [ebp+8] ; liczba x
cmp eax, [ebp+12] ; porownanie liczb x i y
jge x_wieksza ; skok, gdy x >= y
; przypadek x < y
mov eax, [ebp+12] ; liczba y
cmp eax, [ebp+16] ; porownanie liczb y i z
jge y_wieksza ; skok, gdy y >= z
; przypadek y < z
; zatem z jest liczbąnajwiekszą
wpisz_z:
mov eax, [ebp+16] ; liczba z
zakoncz:
pop ebp
ret
x_wieksza:
cmp eax, [ebp+16] ; porownanie x i z
jge zakoncz ; skok, gdy x >= z
jmp wpisz_z
y_wieksza:
mov eax, [ebp+12] ; liczba y
jmp zakoncz
_szukaj_max ENDP
END
Mistake:
Error 2 error LNK2019: unresolved external symbol "int __cdecl szukaj_max(int,int,int)" (? szukaj_max@@YAHHHH@Z) referenced in function _main C:\Users\Kaczor\Desktop\Ako\4.1\4.1\Source.obj
Error 3 error LNK1120: 1 unresolved externals C:\Users\Kaczor\Desktop\Ako\4.1\4.1\Debug\4.1.exe 1
source to share
The compiler of the main file containing the function call assumes that it is a C ++ file, for example, because the file name ends in ".cpp". Therefore, the compiler interprets the declaration int szukaj_max (int a, int b, int c);
as a C ++ function declaration. However, your assembler file defines a C function.
The main difference is the name change: Symbol names generated for C ++ functions contain parameter information, mainly to facilitate overload resolution by the linker. Therefore, the simple symbol _szukaj_max
does not even search. (I was initially confused __cdecl
by the error message appearing, but this identifies ABI issues such as order of passing parameters, responsibilities to clear the stack, etc., rather than changing the name.)
For more information on "decorated names" as Microsoft calls it, see here.
Decision:
- The canonical and portable way is to declare the "extern C" function in your main file:
extern "C" int szukaj_max(int a, int b, int c);
- Name the function what the compiler and linker expects. You can perhaps copy the name together manually, or just look at the linker error: name in your case
?szukaj_max@@YAHHHH@Z
(no underscore underscore). This is not portable because other compilers have different conventions. (But then the MASM assembler may not be completely portable.) - Or, if you are actually programming in C, change the extension of the main file to ".c" so that the compiler will assume that all declared functions are C functions (this is Ange's correct decision). This should be portable again.
source to share