Here below an example of how to use CMake to build assembly language programs. The example below assumes you've got a hello.asm in the directory src/asm/ of your project directory.
hello.asm
;name: hello.asm
;
;description: example of a build with cmake and nasm
;
;build:
; in the project root directory
; cmake .
; make
global _start
section .data
msg db 'Hello world', 0x0A
.len equ $ - msg
section .text
_start:
xor rax,rax
inc rax ;syscall for write
mov rdi,rax ;stdout also 1
mov rsi,msg ;address of the message
mov rdx,msg.len ;length of the message
syscall
mov rax,60 ;syscall for exit
xor rdi,rdi ;return 0
syscall
The CMakeList.txt must be in the root directory of your project.
CMakeLists.txt
cmake_minimum_required(VERSION 3.7)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(project_name "hello")
set(CMAKE_NASM_LINK_EXECUTABLE "ld -o ")
set(CAN_USE_ASSEMBLER TRUE)
set(CMAKE_ASM_NASM_FLAGS "")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
project(${project_name} ASM_NASM)
enable_language(ASM_NASM)
set(SOURCE_FILES src/asm/hello.asm)
add_executable(${project_name} ${SOURCE_FILES})
set_target_properties(${project_name} PROPERTIES LINKER_LANGUAGE NASM)
If all went well you should have a construction like in the image
Building the example is fairly simple, just run in your root directory (you can make a shell script if you like to build it from GUI. So open a terminal (CTRL-ALT-T) and run:
cmake .
make
Your project directory should look similar to
running ./hello will show you the message on screen.
https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/languages/Assembler