编译由VC ++产生集合输出? [英] Compile Assembly Output generated by VC++?

查看:353
本文介绍了编译由VC ++产生集合输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的hello world C程序和/ FA编译。因此,编译器还生成相应的装配上市。现在,我想使用MASM /链接从生成的.ASM上市组装的可执行文件。

以下命令行得到3链接错误:

  \\ MASM32 \\ BIN \\毫升/ IC:\\ Program Files文件(x86)的\\微软的Visual Studio 9.0 \\ VC \\包括/ C / COFF asm_test.asm
\\ MASM32 \\ BIN \\链路/子系统:CONSOLE / LIBPATH:C:\\ Program Files文件(x86)的\\微软的Visual Studio 9.0 \\ VC \\ LIBasm_test.obj

指示C运行时函数没有链接到生成的对象文件前面:


  

asm_test.obj:错误LNK2001:
  解析的外部符号
  @__ security_check_cookie @ 4
  asm_test.obj:错误LNK2001:
  解析的外部符号_printf
  LINK:错误LNK2001:无法解析
  外部符号_wmainCRTStartup
  asm_test.exe:致命错误LNK1120:3
  无法解析的外部


下面是生成汇编列表

 ;由微软(R)优化编译器版本15.00.30729.01清单生成    标题C:\\ asm_test \\ asm_test \\ asm_test.cpp
    .686P
    .XMM
    包括listing.inc
    .MODEL平INCLUDELIB OLDNAMES公共?? _ 3. C @ _0O @ OBPALAEI @你好5world $ CB $ 6节AA @????; `字符串'
EXTRN @__ security_check_cookie @ 4:PROC
EXTRN _printf:PROC
; COMDAT ?? _ 3. C @ _0O @ OBPALAEI @你好?5world?$ CB?6?$ @ AA
CONST段
?? _ 3. C @ _0O @ OBPALAEI @你好5world $ CB 6 $ @ AA DB世界,你好!,0AH,00H????; `字符串'
CONST ENDS
公共_wmain
;功能编译标志:/ Ogtpy
; COMDAT _wmain
_TEXT段
_argc $ = 8;大小= 4
_argv $ = 12;大小= 4
_wmain PROC; COMDAT
;文件C:\\用户\\翱腾\\桌面\\ asm_test \\ asm_test \\ asm_test.cpp
; 21号线
    推OFFSET ?? _ 3. C @ _0O @ OBPALAEI @你好?5world?$ CB?6?$ @ AA
    _printf打电话
    ADD ESP,4
;第22行
    XOR EAX,EAX
; 23号线
    RET 0
_wmain ENDP
_TEXT ENDS
结束

我使用的是最新版本的MASM32(6.14.8444)。

更新

正如齿轮的建议,我在ASM源包含在一个 INCLUDELIB MSVCRT.LIB 。程序编译和可执行文件被创建,但链接器生成一个警告:


  

MSVCRT.LIB(crtmanifestrtm.obj):警告LNK4044:无法识别的选项manifestdependency:类型='的win32的名字='Microsoft.VC90.CRT'版本=9.0.21022.8的ProcessorArchitecture ='86'公钥='1fc8b3b9a1e18e3b ';忽略


当我启动可执行文件,C运行时生成以下错误:


  

运行时错误:R6034应用程序已作出了尝试加载错误的C运行时库



解决方案

最近,我这样做。事实证明,你仍然可以做到这一点,在MSVC2012 32位模式,但是我觉得64位模式无望。

对于32位模式,这里是你做了什么。

创建一个空的项目和源文件的 Source.cpp

 的#include<&stdio.h中GT;
 诠释主(){
     的printf(世界你好\\ n);
     返回0;
 }


  1. 右键舔你的项目,并选择生成定制和结果
    这里 http://www.masm32.com/board/index描述选择MASM。 PHP?主题= 9231.0

  2. 在C ++ / OutputFiles选择装配输出/ FA

  3. Comipile在32位模式下拍摄模式

  4. 加载的 Source.asm 的文件到MSVC以便您可以查看它。它暂时还不能正常工作。一些修改是必要的。

  5. 在C ++ /优化关闭整个程序优化(删除 / GL )。这增加了该行 INCLUDELIB MSVCRT

  6. 在连接器/高级设置的最后一个选项的形象安全异常处理程序为No( / SAFESEH:NO

  7. 现在,你应该有一个Source.asm文件,它会做Source.cpp文件做同样的事情。在Source.cpp从发布目录复制到同一目录Source.cpp(所以当你建立/清洁它不会被删除)。

  8. 添加的 Source.asm 的(与现有文件)的源文件和删除的 Source.cpp 的从构建。

  9. 重建,你应该看到Hello World,而无需改变手的组装生产线。

我已经使用这个更复杂​​的功能。我通常做一个独立的模块并使用的externC的函数名,除去C ++名称重整。

I have a simple hello world C program and compile it with /FA. As a consequence, the compiler also generates the corresponding assembly listing. Now I want to use masm/link to assemble an executable from the generated .asm listing.

The following command line yields 3 linker errors:

\masm32\bin\ml /I"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include" /c /coff asm_test.asm
\masm32\bin\link /SUBSYSTEM:CONSOLE /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\lib" asm_test.obj

indicating that the C-runtime functions were not linked to the object files produced earlier:

asm_test.obj : error LNK2001: unresolved external symbol @__security_check_cookie@4 asm_test.obj : error LNK2001: unresolved external symbol _printf LINK : error LNK2001: unresolved external symbol _wmainCRTStartup asm_test.exe : fatal error LNK1120: 3 unresolved externals

Here is the generated assembly listing

; Listing generated by Microsoft (R) Optimizing Compiler Version 15.00.30729.01 

    TITLE   c:\asm_test\asm_test\asm_test.cpp
    .686P
    .XMM
    include listing.inc
    .model  flat

INCLUDELIB OLDNAMES

PUBLIC  ??_C@_0O@OBPALAEI@hello?5world?$CB?6?$AA@   ; `string'
EXTRN   @__security_check_cookie@4:PROC
EXTRN   _printf:PROC
;   COMDAT ??_C@_0O@OBPALAEI@hello?5world?$CB?6?$AA@
CONST   SEGMENT
??_C@_0O@OBPALAEI@hello?5world?$CB?6?$AA@ DB 'hello world!', 0aH, 00H ; `string'
CONST   ENDS
PUBLIC  _wmain
; Function compile flags: /Ogtpy
;   COMDAT _wmain
_TEXT   SEGMENT
_argc$ = 8                      ; size = 4
_argv$ = 12                     ; size = 4
_wmain  PROC                        ; COMDAT
; File c:\users\octon\desktop\asm_test\asm_test\asm_test.cpp
; Line 21
    push    OFFSET ??_C@_0O@OBPALAEI@hello?5world?$CB?6?$AA@
    call    _printf
    add esp, 4
; Line 22
    xor eax, eax
; Line 23
    ret 0
_wmain  ENDP
_TEXT   ENDS
END

I am using the latest masm32 version (6.14.8444).

Update:

As suggested by Cogwheel, I included an INCLUDELIB msvcrt.lib in the asm source. The program compiles and an executable is created, but the linker generates a warning:

msvcrt.lib(crtmanifestrtm.obj) : warning LNK4044: unrecognized option "manifestdependency:type='win32' name='Microsoft.VC90.CRT' version='9.0.21022.8' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b'"; ignored

When i start the executable, the C runtime generates the following error:

Runtime error: R6034 An application has made an attempt to load the C runtime library incorrectly

解决方案

I did this recently. It turns out you can still do this in 32-bit mode in MSVC2012 but I think 64-bit mode is hopeless.

For 32-bit mode here is what you do.

Create an empty project and a source file Source.cpp

 #include <stdio.h>
 int main() {
     printf("hello world\n");
     return 0;
 }

  1. Right lick on your project and select "Build Customization" and
    select masm as described here http://www.masm32.com/board/index.php?topic=9231.0
  2. Under C++/OutputFiles select Assembly Output /FA
  3. Comipile in 32-bit mode Release mode
  4. Load the Source.asm file into MSVC so you can view it. It won't work yet. A few changes are necessary.
  5. Under C++/Optimization turn off Whole Program Optimization (removes /GL). This adds the line INCLUDELIB MSVCRT
  6. In the Linker/Advanced set the last option "Image Has Safe Exception Handlers"to No (/SAFESEH:NO)
  7. Now you should have a Source.asm file which will do the same thing that Source.cpp file did. Copy the Source.cpp from the Release directory to the same directory as Source.cpp (so it's not deleted when you build/clean).
  8. Add Source.asm (as an existing file) to the Source Files and remove Source.cpp from the build.
  9. Rebuild and you should see "Hello World" without having to change any assembly lines by hand.

I have used this for more complicated functions. I usually do it on a separate module and use extern "C" on the function name to remove the C++ name mangling.

这篇关于编译由VC ++产生集合输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆