猿问

从Visual Studio中的asm调用C标准库函数

我在Visual Studio(Win10 x64,Visual Studio 2015)中创建的asm项目中调用C函数时遇到问题。项目包含一个asm文件:


.586

.model flat, stdcall

option casemap:none

includelib msvcrt.lib


ExitProcess PROTO return:DWORD

extern printf:near


.data

text BYTE "Text", 0


.code

main PROC

    push offset text

    call printf

    add esp,4

    invoke ExitProcess,0

main ENDP

end 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>?有可能吗?


蝴蝶刀刀
浏览 1047回答 3
3回答

白衣染霜花

Microsoft&nbsp;在VS 2015中重构了大部分C运行时和库。某些功能不再从C库导出(某些功能在C头文件中定义)。Microsoft具有一些兼容性库,例如legacy_stdio_definitions.lib和legacy_stdio_wide_specifiers.lib,但您也可以选择将较早的Visual Studio 2013平台工具集与较早的C库一起使用。要更改平台工具集:下拉Project菜单;选择Properties...;&nbsp;转到Configuration Properties/&nbsp;General,然后更改Platform Toolset为Visual Studio 2013(v120)

翻过高山走不出你

似乎可以对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, stdcalloption casemap:noneincludelib libcmt.libincludelib libvcruntime.libincludelib libucrt.libincludelib legacy_stdio_definitions.libExitProcess PROTO return:DWORDextern printf:NEAR.datatext BYTE "Text", 0.codemain PROC C&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; Specify "C" calling convention&nbsp; &nbsp; push offset text&nbsp; &nbsp; call printf&nbsp; &nbsp; add&nbsp; esp, 4;&nbsp; &nbsp;invoke ExitProcess,0&nbsp; &nbsp; &nbsp; &nbsp;; Since the C library called main (this function)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; we can set eax to 0 and use ret`to have&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; the C runtime close down and return our error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; code instead of invoking ExitProcess&nbsp; &nbsp; mov eax, 0&nbsp; &nbsp; retmain ENDPend&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ; Use `end` on a line by itself&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; We don't want to use `end main` as that would&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; make this function our program entry point&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; effectively skipping by the C runtime initialization查看

大话西游666

您可以调用C函数,但随后需要与C库链接。确切的实现方式取决于您要链接的C库。我建议您找到一个最小的C运行时,例如WCRT库。该库可能需要初始化,并且可能需要您在其某个地方定义一堆缓冲区以进行记账。建议您不要使用Windows API,而是使用WriteConsole函数,而不要麻烦这些麻烦。
随时随地看视频慕课网APP
我要回答