亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從Visual Studio中的asm調用C標準庫函數

從Visual Studio中的asm調用C標準庫函數

BIG陽 2019-12-06 09:42:09
我在Visual Studio(Win10 x64,Visual Studio 2015)中創建的asm項目中調用C函數時遇到問題。項目包含一個asm文件:.586.model flat, stdcalloption casemap:noneincludelib msvcrt.libExitProcess PROTO return:DWORDextern printf:near.datatext BYTE "Text", 0.codemain PROC    push offset text    call printf    add esp,4    invoke ExitProcess,0main ENDPend main當我構建項目時,鏈接器輸出錯誤:錯誤_main @ 0中引用的LNK2019無法解析的外部符號_printf鏈接器輸出參數:/OUT:"C:\Users\apple\Documents\SP_Lab7\Debug\SP_Lab7_Demo.exe“ / MANIFEST:否/ NXCOMPAT /PDB:"C:\Users\apple\Documents\SP_Lab7\Debug\SP_Lab7_Demo.pdb” / DYNAMICBASE “ kernel32.lib”“ user32.lib”“ gdi32.lib”“ winspool.lib”“ comdlg32.lib”“ advapi32.lib”“ shell32.lib”“ ole32.lib”“ oleaut32.lib”“ uuid.lib” “ odbc32.lib”“ odbccp32.lib” / MACHINE:X86 / SAFESEH:NO / INCREMENTAL:NO /PGD:"C:\Users\apple\Documents\SP_Lab7\Debug\SP_Lab7_Demo.pgd“ / SUBSYSTEM:WINDOWS / MANIFESTUAC: “ level ='asInvoker'uiAccess ='false'” /ManifestFile:"Debug\SP_Lab7_Demo.exe.intermediate.manifest“ / ERRORREPORT:PROMPT / NOLOGO / TLBID:1如果我發表評論call print,那么一切都會正常執行(甚至是Windows API函數)。有什么方法可以從asm文件中調用C函數,而無需創建包含以下內容的cpp文件<cstdio>?有可能嗎?
查看完整描述

3 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

Microsoft 在VS 2015中重構了大部分C運行時和庫。某些功能不再從C庫導出(某些功能在C頭文件中定義)。Microsoft具有一些兼容性庫,例如legacy_stdio_definitions.liblegacy_stdio_wide_specifiers.lib,但您也可以選擇將較早的Visual Studio 2013平臺工具集與較早的C庫一起使用。

要更改平臺工具集:下拉Project菜單;選擇Properties...; 轉到Configuration PropertiesGeneral,然后更改Platform ToolsetVisual Studio 2013(v120)


查看完整回答
反對 回復 2019-12-06
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

似乎可以對Visual Studio 2015 Toolset進行一些修改。


您需要將以下庫添加到依賴項中:libcmt.lib,libvcruntime.lib,libucrt.lib,legacy_stdio_definitions.lib?;蛘?,您可以includelib用來將這些庫包含在程序集文件中。

為您的過程指定C調用約定mainPROC C

在文件末尾(這很重要)不要使用end main,而end只能使用。不解決此問題可能會導致意外崩潰。

盡管我們可以使用ExitProcess退出我們的應用程序,但是我們也可以將返回代碼放入EAX中并做一個ret返回。該?運行時調用我們的main功能,并在返回時將調用關機代碼為我們。

代碼如下所示:


.586

.model flat, stdcall

option casemap:none


includelib libcmt.lib

includelib libvcruntime.lib

includelib libucrt.lib

includelib legacy_stdio_definitions.lib


ExitProcess PROTO return:DWORD

extern printf:NEAR


.data

text BYTE "Text", 0


.code

main PROC C                    ; Specify "C" calling convention

    push offset text

    call printf

    add  esp, 4

;   invoke ExitProcess,0       ; Since the C library called main (this function)

                               ; we can set eax to 0 and use ret`to have

                               ; the C runtime close down and return our error

                               ; code instead of invoking ExitProcess

    mov eax, 0

    ret

main ENDP

end                            ; Use `end` on a line by itself

                               ; We don't want to use `end main` as that would

                               ; make this function our program entry point

                               ; effectively skipping by the C runtime initialization


查看完整回答
反對 回復 2019-12-06
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

您可以調用C函數,但隨后需要與C庫鏈接。確切的實現方式取決于您要鏈接的C庫。我建議您找到一個最小的C運行時,例如WCRT庫。


該庫可能需要初始化,并且可能需要您在其某個地方定義一堆緩沖區以進行記賬。


建議您不要使用Windows API,而是使用WriteConsole函數,而不要麻煩這些麻煩。


查看完整回答
反對 回復 2019-12-06
  • 3 回答
  • 0 關注
  • 768 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號