georgeparry4
georgeparry4

Reputation: 11

how to reference global variable from C that was defined in .asm file in visual studio

I have a .asm file that defines variable foo

file.asm:

.data
foo DWORD 0

.code
{ ... }

I want to reference foo outside of file.asm in main.c , This what i have tried:

main.c

extern int foo;

int main()
{
    foo++;
}

However this causes a linker error: LNK2001 unresolved external symbol foo , how can i reference it from main.c ?

UPDATE:

I have also tried using the PUBLIC directive:

file.asm:

.data
PUBLIC foo
foo DWORD 0

This causes same linker error LNK2001

Upvotes: 1

Views: 913

Answers (1)

Nate Eldredge
Nate Eldredge

Reputation: 58805

MSVC uses underscores at the beginning of symbol names, and apparently uses PUBLIC to export a symbol (otherwise labels are internal, like static variables in C). Try

.data
PUBLIC _foo
_foo DWORD 0

This is essentially what MSVC itself does, see https://godbolt.org/z/Phb4Pv

Upvotes: 2

Related Questions