error LNK2019: unresolved external symbol referenced in function main

ぐ巨炮叔叔 提交于 2019-12-23 12:24:43

问题


Am trying to run my simple assembly code in C++.I have only two files ".cpp" file and ".asm" file. On compiling it gives an error (see below ).I would appreciate if anyone could help...:)

This is my "main.cpp" file

#include <iostream>
using namespace std;

extern "C" int GetValueFromASM(); 

int main(int argc, char *argv[]){

cout<<"value is:"<<GetValueFromASM()<<endl;
cin.get();

return 0;
}

Also i have a simple "asm.asm" file

.code
   GetValueFromASM proc
   mov rax,3254
   ret
   GetValueFromASM endp
end

When try to build i get this error :

1>main.obj : error LNK2019: unresolved external symbol GetValueFromASM referenced in             
function main

1>..\visual studio 2012\Projects\AllAssembly\x64\Debug\AllAssembly.exe : fatal error LNK1120: 1 unresolved externals

I tried go to PROPERTIES->LINKS->ADDITIONAL LIBRARY DIRECTORIES and change the path but it didn't work.

I tried also go to LINKER->SYSTEM->SUBSYSTEM and Select "Windows (/SUBSYSTEM:WINDOWS)" or "Console (/SUBSYSTEM:CONSOLE)" but neither one worked. Can anyone help !!..BIG THANKS


回答1:


extern "C" implies a leading underscore (c-style function naming). Just prefix your function in .asm file and it'll start linking.

UPD: if your project does not build, be sure, you included MASM build customizations: go to project Context Menu -> Build Customizations... -> check the tickbox for masm (.targets, .props).

Then go to properties of you .asm file and select Item Type as 'Microsoft Macro Assembler'.




回答2:


I confused it with 32bit. :(

So this should work for 64bit.

section .code
global GetValueFromASM

GetValueFromASM:
    ...



回答3:


No Changes i made to the codes.I just changed some configurations as "Arty" suggests.

Here is How : Right click your project->Go to Build Customization -> check MASM -> Click OK Then go to properties of your .asm file and select Item Type as 'Microsoft Macro Assembler'.

You Done!!..Thanks to Arty & Devolus for the help.

Can be helpful For similar problem: http://scriptbucket.wordpress.com/2011/10/19/setting-up-visual-studio-10-for-masm32-programming/



来源:https://stackoverflow.com/questions/20326262/error-lnk2019-unresolved-external-symbol-referenced-in-function-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!