_main 已在 file.obj 中定义

发布于 2024-12-15 23:16:57 字数 575 浏览 3 评论 0原文

我正在读一本教授通过 Visual C++ Express 使用 MASM 进行汇编的书。

这本书提供了以下示例程序来测试 masm 是否正常工作

    .386
    .model flat, c
    .stack 100h
printf PROTO arg1:Ptr Byte, printlist:VARARG
    .data
msg1fmt byte "%s%d",0Ah,0
msg1 byte "The answer is: ",0
num1 sdword ?
num2 sdword ?
    .code
main proc
    mov num1,5
    mov eax,num1
    mov num2,eax
    INVOKE printf, ADDR msg1fmt, ADDR msg1, num2
    ret
main endp
    end

,然后我收到错误 LNK2005: _main 已在 asm.obj 中定义

我正在使用我书中的示例汇编程序,所以希望它不是错误中。我收到的错误似乎与 main 被列出两次有关,但我不确定如何解决它。

I am reading a book which teaches assembly using MASM via Visual C++ Express.

The book gives the following example program to test that masm is working

    .386
    .model flat, c
    .stack 100h
printf PROTO arg1:Ptr Byte, printlist:VARARG
    .data
msg1fmt byte "%s%d",0Ah,0
msg1 byte "The answer is: ",0
num1 sdword ?
num2 sdword ?
    .code
main proc
    mov num1,5
    mov eax,num1
    mov num2,eax
    INVOKE printf, ADDR msg1fmt, ADDR msg1, num2
    ret
main endp
    end

I then get the error error LNK2005: _main already defined in asm.obj

I am using the sample assembly program from my book so hopefully it is not in error. The error I get seems to have something to do with main being listed twice, but I'm unsure how to resolve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2024-12-22 23:16:57

书本上的编程示例出错的情况并非闻所未闻。

请注意,您收到的错误来自链接器,而不是汇编器。事实上,您到目前为止已经表明汇编器对您的文件感到满意,并且实际上您的汇编文件仅定义了一次 main

我不熟悉 MS 开发工具,尤其是过去十五年的开发工具,但我想您已经在定义 main() 的项目中添加了 C 或 C++ 模块作为该语言中的函数,或者 IDE “有帮助地”提供默认定义。链接器对 main 的第二个定义不满意。

(链接器错误消息中符号中的下划线只是您在该平台上执行操作的方式的产物 - 所有 HLL 符号都以下划线为前缀。我注意到您有 .model flat, c - 这可能会告诉 MASM 为符号添加前缀,尽管汇编不是高级语言。)

您可以尝试在 C 语言部分中查找 main() 的定义您的项目,只需将其删除或省略如果你能找到的话,可以从项目中找到它。如果做不到这一点,请将您的汇编函数重命名为 asm_main,然后从 C 语言调用that:(

int main() {
  return asm_main();
}

这只是为了让您继续;如果您想获得掌握 argc 等人的知识,您需要发挥更多创意。)

It is not unheard of at all for programming examples in books to be in error.

Note that the error you're getting is from the linker, not the assembler. The fact you're getting that far already shows that the assembler is happy with your file, and indeed your assembly file defines main only once.

I'm not familiar with the MS development tools, especially not ones from the last decade and a half, but I imagine that either you have added a C or C++ module to your project that defines main() as a function in that language, or that the IDE is "helpfully" providing a default definition. It is this second definition of main that the linker is unhappy about.

(The underscore in symbol in the linker's error message is just an artifact of how you do things on that platform - all HLL symbols get prefixed by an underscore. I note you have .model flat, c - that may be telling MASM to prefix symbols despite assembly not being a high-level language.)

You could try looking for the definition of main() in the C-land part of your project and simply deleting it or omitting it from the project, if you can find it. Failing that, rename your assembly function to, say, asm_main, and then calling that from C-land:

int main() {
  return asm_main();
}

(This is just to get you going; if you want to get hold of argc et al, you'll need to get more creative.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文