Recently in learning "the operating system true image Restore", in the course of learning to consider the file may be written in the process of more files, want to use CMake to configure the most basic compilation scheme, to reduce the late process, manual compilation caused by the trouble.
Because there is no ready-made example, many versions of the Web are compiled and mixed with C, the generated code is based on the platform-related running files, as long as all the C to deal with the line.
Requirement: Compiling NASM files
The process of stepping on a lot of pits, leaving a record memo.
1. CMake and NASM conflicts for header file path representations.
In CMake, the include_directories () function can add a header file path and ends with no/symbol, such as ~/include. However, the NASM strict requirements for the-i parameter adds a header file path that contains a backslash to create a stylistic conflict, include_directories () adds a path that is not included when the compilation parameter is compiled, resulting in a compilation failure, where the first display does not find the header file.
Solution:
Add_compile_options (-i ${cmake_current_source_dir}/include/)
As the name implies: Use this function directly to add the compilation parameters, in the string to increase the backslash, to avoid conflicts, although looking a bit awkward, but solved the problem.
Similar example StackOverflow
2. online CMake Build assembly content is too old
Until 2017.11.1
VTK official online about CMake compiled NASM said NASM does not support pre-compilation, in fact, has now been supported, NASM originally with pre-compilation parameters-E will be the file only pre-compiled processing.
3. CMake When compiling the assembly file, it's strange to call Nasm two times.
The first time is the second time that the compilation is a link. In fact, the first time the compilation is complete. In the second link process, will be error, so we need to modify the connection function.
Set (cmake_asm_nasm_link_executable "NASM <OBJECTS>-o <TARGET> <LINK_LIBRARIES>")
Project file:
├──bin
│ ├──1.bin
│ ├──loader.bin
│ └──mbr.bin
├──boot
│ ├──cmakelists.txt
│ ├──include │ │ └──boot.inc
│ ├──loader. S
│ └──mbr. S
├──cmakelists.txt
├──kernel
│ ├──global.h
│ ├──init.c
│ ├──init.h
│ ├──interrupt.c
│ ├──interrupt.h
│ ├──kernel. S
│ └──main.c
└──lib
├──kernel
│ ├──io.h
│ ├──print.h
│ └── Print. S
├──stdint.h
└──user
Boot/cmakelists.txt content:
Set (cmake_asm_nasm_source_file_extensions NASM ASM S) Set (Cmake_asm_nasm_object_format bin) #set (cmake_nasm_link_ Executable nasm) #set (cmake_asm_nasm_flags "${cmake_asm_nasm_flags}-F bin-l ~/1.txt") #只是生成汇编的预处理文件 set (cmake_asm_ Nasm_flags "-E") #set (cmake_asm_nasm_link_executable "LD <FLAGS> <CMAKE_ASM_NASM_LINK_FLAGS> <LINK_ Flags> <OBJECTS>-o <TARGET> <LINK_LIBRARIES> ") #这个地方不需要连接的过程 the original program was called two times NASM will error #set (cmake_asm_ Nasm_link_executable "CP <OBJECTS> <TARGET>") Set (cmake_asm_nasm_link_executable "NASM <OBJECTS>-O <TARGET> <LINK_LIBRARIES> ") enable_language (asm_nasm) #头文件包含的目录 use NASM because the directory does not end with dir/, it is not valid Include_direc Tories (include) #修改方式 add_compile_options (-i ${cmake_current_source_dir}/include/) #修改输出的路径 set (Executable_ou Tput_path ${project_source_dir}/bin) #add_executable (Loader.bin loader.s) #set_target_properties (loader.bin PROPERTIES linker_language NASM) #add_executable (Mbr.bin mbr.s) #Set_target_properties (Mbr.bin Properties Linker_language NASM) add_executable (loader loader. S) add_executable (MBR mbr. S) #add_library (1 STATIC loader.
S) #set_target_properties (1.bin properties Linker_language NASM) set_target_properties (MBR loader properties SUFFIX. Bin)
Root directory CMakeLists.txt content:
Cmake_minimum_required (VERSION 3.0)
Project (System)
add_subdirectory (boot bin)
Process in a lot of pits, basic official website documents can not find, can only see the developer Communications Mail, slowly groping to find, combined with StackOverflow slowly groping out CMake compiled NASM process.