我正在使用 Visual Studio 2019 社区版,并且正在尝试从 C 程序运行汇编函数。
我收到以下错误:
Rebuild started...
1>------ Rebuild All started: Project: ArrayReverser, Configuration: Debug Win32 ------
1>Assembling GetValueFromASM.asm...
1>ArrayReverser.cpp
1>ArrayReverser.obj : error LNK2019: unresolved external symbol __imp__srand referenced in function _main
1>ArrayReverser.obj : error LNK2019: unresolved external symbol __imp__rand referenced in function _main
1>ArrayReverser.obj : error LNK2019: unresolved external symbol __imp____acrt_iob_func referenced in function _printf
1>ArrayReverser.obj : error LNK2019: unresolved external symbol __imp____stdio_common_vfprintf referenced in function __vfprintf_l
1>C:\Users\firas\source\repos\ArrayReverser\Debug\ArrayReverser.exe : fatal error LNK1120: 4 unresolved externals
1>Done building project "ArrayReverser.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
我几乎尝试了在互联网上找到的所有解决方案,但没有任何效果,我希望有人能够深入了解正在发生的事情。
这是 C 代码:
#include <iostream>
#include <stdlib.h>
extern "C" void GetValueFromASM(int* y, const int *x, int n);
int main(int argc, char* argv[])
{
const int n = 10;
int x[n], y[n];
srand(41);
for (int i = 0; i < n; i++)
x[i] = rand() % 1000;
GetValueFromASM(y, x, n);
printf("\n---------------Array Reverser-------------\n");
for (int i = 0; i < n; i++)
{
printf("i: %5d y: %5d x: %5d\n", i, y[i], x[i]);
}
return 0;
}
这是汇编代码:
.386
.model flat,c
.code
GetValueFromASM PROC
push ebp ;we pushed the register ebp onto the stack
mov ebp,esp
push esi
push edi
xor eax,eax
;now we load the parameters (int* y, const int* x, int n) onto edi,esi,ecx
mov edi,[ebp+8] ;bp - stack base Pointer Register
mov esi,[ebp+12]
mov ecx,[ebp+16]
; in order to reverse the array elemtns we use the following command
lea esi,[esi+ecx*4-4]
pushfd
std
@@: lodsd
mov [edi], eax
add edi, 4
dec ecx
jnz @B
popfd
mov eax,1
;Epilouge
pop edi
pop esi
pop ebp
GetValueFromASM endp
end
链接到我的项目文件: https://drive.google.com/file/d/1entjG8nHarDTW0AEmvKhv0Cvrw9v0BF2/view?usp=sharing
最佳答案
我下载了你的项目并观察到类似的链接器错误(实际上我有更多)。我检查了该项目的所有链接器选项,注意粗体的条目。粗体选项尚未被继承。突出的一个是入口点
:
您使用 MASM 函数 Reverser
覆盖了默认的 C 启动入口点。我修改了 EntryPoint
,使其空白(将其删除)。当我重建项目时,不再出现链接器错误。
您的Reverser
函数末尾没有ret
,因此运行时会抛出异常。我在 Reverser
函数的底部添加了一个 ret
并运行代码并得到了以下输出:
注意:@rustyx 提出了非常好的建议“对于 MSVC 配置而言,尝试每种解决方案通常都是一个坏主意。事实上,应该非常小心,不要更改您想要的任何内容。不知道效果。现在可能值得创建一个全新的项目。” 。如果您创建一个全新的CONSOLE项目并且不修改入口点,那么应该不会有问题。
关于assembly - 错误 LNK2019 : unresolved external symbol __imp____acrt_iob_func referenced in function _printf in VS2019,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64829387/