这是图书馆。 库.asm:
section .text
global return_number
return_number:
mov eax, 10
ret
我可以使用命令行轻松创建共享和静态库:
nasm -f elf64 -o library.o library.asm
ld -shared -o libreturn_number.so library.o
ar rcs libreturn_number.a library.o
但是,使用 CMake 会出现问题。 CMakeLists.txt:
enable_language(ASM_NASM)
project(mylibrary)
set(CMAKE_ASM_NASM_CREATE_SHARED_LIBRARY "<CMAKE_ASM_NASM_COMPILER> <CMAKE_SHARED_LIBRARY_CREATE_ASM_NASM_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
add_library(mylibrary_static STATIC library.asm)
add_library(mylibrary_shared SHARED library.asm)
静态库工作得很好,但是共享库会抛出一堆奇怪的错误。
CMakeFiles/mylibrary_shared.dir/library.asm.o:1: error: label or instruction expected at start of line
CMakeFiles/mylibrary_shared.dir/library.asm.o:10: error: label or instruction expected at start of line
CMakeFiles/mylibrary_shared.dir/library.asm.o:11: error: label or instruction expected at start of line
CMakeFiles/mylibrary_shared.dir/library.asm.o:12: error: label or instruction expected at start of line
CMakeFiles/mylibrary_shared.dir/library.asm.o:31: warning: label alone on a line without a colon ...
对于“.o”文件中的几乎每一行。
问题是您正在尝试使用 nasm 编译器链接对象二进制文件。为此,您应该使用 ld 链接器甚至 gcc。所以 nasm 正在读取目标文件并说 wtf 就是这个!
下面的解决方案使用的是您在上面提供的相同 asm 文件。
对于 64 位,CMakeLists.txt 是
set(CMAKE_ASM_NASM_CREATE_SHARED_LIBRARY
"ld -lc -shared <CMAKE_ASM_NASM_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
enable_language(ASM_NASM)
project(mylibrary)
cmake_minimum_required(VERSION 3.12)
add_library(mylibrary_shared SHARED library.asm)
$ cmake ..
-- The ASM_NASM compiler identification is NASM
-- Found assembler: /usr/bin/nasm
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: ../build
$ make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hbucher/git/nasm/build
[ 50%] Building ASM_NASM object CMakeFiles/mylibrary_shared.dir/library.asm.o
[100%] Linking ASM_NASM shared library libmylibrary_shared.so
$ ldd libmylibrary_shared.so
linux-vdso.so.1 (0x00007ffffb59e000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8bf8822000)
/lib64/ld-linux-x86-64.so.2 (0x00007f8bf8a4a000)
这对于 32 位:
set(CMAKE_ASM_NASM_CREATE_SHARED_LIBRARY
"ld -lc -shared -m elf_i386 <CMAKE_ASM_NASM_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
enable_language(ASM_NASM)
project(mylibrary)
cmake_minimum_required(VERSION 3.12)
enable_language(ASM_NASM)
add_library(mylibrary_shared SHARED library.asm)
生产
$ cmake ..
-- The ASM_NASM compiler identification is NASM
-- Found assembler: /usr/bin/nasm
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
$ make
[ 50%] Building ASM_NASM object CMakeFiles/mylibrary_shared.dir/library.asm.o
[100%] Linking ASM_NASM shared library libmylibrary_shared.so
[100%] Built target mylibrary_shared
$ ldd libmylibrary_shared.so
linux-gate.so.1 (0xf7f39000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xf7d10000)
/lib/ld-linux.so.2 (0xf7f3b000)