Windows x64下CMake+MSVC编译汇编代码报错的修复方法咨询
解决Visual Studio中CMake编译MASM汇编代码的LNK1104错误
在Visual Studio 2022中使用CMake编译包含C++和MASM汇编代码的项目时,出现链接错误LNK1104,提示无法找到JustValue.asm.obj文件。编译过程中cl.exe报错无法识别汇编文件类型,直接忽略了汇编文件的编译,导致链接时缺少目标文件。
项目原文件
CMakeLists.txt
cmake_minimum_required(VERSION 3.14) project( asm_test LANGUAGES CXX ) enable_language(C ASM) add_executable(${PROJECT_NAME} main.cpp JustValue.asm) set_target_properties( ${PROJECT_NAME} PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON )
main.cpp
#include <iostream> extern "C" int JustValue(); int main() { std::cout << "Value = " << JustValue() << std::endl; }
JustValue.asm(MASM格式)
.code JustValue proc mov eax, 7 ret JustValue endp end
编译报错信息
>------ Build started: Project: CMakeLists, Configuration: Debug ------ [1/3] Building ASM object CMakeFiles\asm_test.dir\JustValue.asm.obj Microsoft (R) C/C++ Optimizing Compiler Version 19.32.31332 for x64 Copyright (C) Microsoft Corporation. All rights reserved. C:\Users\ruden\source\repos\asm_test\out\build\x64-Debug\cl : Command line warning D9035: option 'o' has been deprecated and will be removed in a future release C:\Users\ruden\source\repos\asm_test\out\build\x64-Debug\cl : Command line warning D9024: unrecognized source file type 'C:\Users\ruden\source\repos\asm_test\JustValue.asm', object file assumed C:\Users\ruden\source\repos\asm_test\out\build\x64-Debug\cl : Command line warning D9027: source file 'C:\Users\ruden\source\repos\asm_test\JustValue.asm' ignored C:\Users\ruden\source\repos\asm_test\out\build\x64-Debug\cl : Command line warning D9021: no action performed [2/3] Building CXX object CMakeFiles\asm_test.dir\main.cpp.obj [3/3] Linking CXX executable asm_test.exe FAILED: asm_test.exe cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\asm_test.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests -- C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1432~1.313\bin\Hostx64\x64\link.exe /nologo CMakeFiles\asm_test.dir\main.cpp.obj CMakeFiles\asm_test.dir\JustValue.asm.obj /out:asm_test.exe /implib:asm_test.lib /pdb:asm_test.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ." LINK Pass 1: command "C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1432~1.313\bin\Hostx64\x64\link.exe /nologo CMakeFiles\asm_test.dir\main.cpp.obj CMakeFiles\asm_test.dir\JustValue.asm.obj /out:asm_test.exe /implib:asm_test.lib /pdb:asm_test.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\asm_test.dir\intermediate.manifest CMakeFiles\asm_test.dir\manifest.res" failed (exit code 1104) with the following output: C:\Users\ruden\source\repos\asm_test\out\build\x64-Debug\LINK : fatal error LNK1104: cannot open file 'CMakeFiles\asm_test.dir\JustValue.asm.obj' ninja: build stopped: subcommand failed.
错误原因
CMake默认的ASM语言在Visual Studio环境下并未关联到MASM汇编器,而是错误调用了C/C++编译器cl.exe处理汇编文件,导致汇编文件被忽略,未生成对应的目标文件,最终链接时找不到.obj文件。
解决方案
修改CMakeLists.txt,明确指定使用MASM汇编器(ASM_MASM):
修改后的CMakeLists.txt
cmake_minimum_required(VERSION 3.14) project( asm_test LANGUAGES CXX ASM_MASM ) add_executable(${PROJECT_NAME} main.cpp JustValue.asm) set_target_properties( ${PROJECT_NAME} PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED ON )
操作步骤
- 指定ASM_MASM语言:在
project的LANGUAGES参数中添加ASM_MASM,告诉CMake使用MASM汇编器处理.asm文件。 - 移除多余配置:删除原有的
enable_language(C ASM),因为project指令已启用指定语言。 - 重新生成缓存:删除现有build目录,重新运行CMake生成项目文件后再编译。
修改后,CMake会调用ml64.exe(x64平台MASM汇编器)编译JustValue.asm,生成对应目标文件,链接即可成功完成。
内容的提问来源于stack exchange,提问作者Arkady Rudenko
相关产品推荐
相关产品推荐

