cmake cross-compile application: manually set up the link script

Preface

  • When using cmake to cross-compile the application, it seems that the [link script] can be built and generated normally without manually setting the [link script], and the required application can be generated by Make normally.

  • However, some applications need to manually specify the [link script], such as modifying the link address. How to do this in cmake build?

  • There is currently no link script set up, and it is found that the entry address of the compiled application is 0

as follows:Entry point address: 0x0

readelf -h routingmanagerd
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           AArch64
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          64 (bytes into file)
  Start of section headers:          226912 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         8
  Size of section headers:           64 (bytes)
  Number of section headers:         38
  Section header string table index: 37

Learn about cmake

  • By default, cmake's build operation only generates Makefile, and then you need to execute the make command to compile and link.

  • Therefore, you can add the [Link Script] option to the cmake build file.

cmake sets the link script path

  • cmake can seteasily set environment variables through the command. The operation here is to add
set(LINK_SCRIPTS "-T/home/zhangsz/smart/adas/software/userapps/linker_scripts/aarch64/link.so.lds")
  • Note: You can use set to set environment variables in cmake, and the environment variable names can be customized. If the environment variable exists, you can use ${LINK_SCRIPTS}to get the set environment variable

  • The operation here is: set the environment variable ${LINK_SCRIPTS}to the path of the specified link script

  • -T/home/zhangsz/userapps/linker_scripts/aarch64/link.so.ldsHere -Tmeans specifying the link script, used for gcc compilation parameters, followed by the path of the link script

cmake application generation

  • cmake generates target files, using by default: target_link_libraries, here is the dynamic link, as follows:

  • Added target_link_libraries(routingmanagerd ${VSOMEIP_NAME} ${Boost_LIBRARIES} ${DL_LIBRARY} ${DLT_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${LINK_SCRIPTS})the [Environment Variables] just added to the link script, here is${LINK_SCRIPTS}

Insert image description here

  • cmake will eventually convert these operations and environment variables into gcc or cross-compile gcc parameters.

Compilation verification

  • Compiled detailed LOG and found that the link script was successfully set up.

Insert image description here

  • After cmake, use make to compile. It is found that the link address of the compiled application is consistent with the address of the specified link script, indicating that the operation setting of the specified link script is successful.

  • Entry function address Entry point address: 0x201000, the address specified by the link script

readelf -h routingmanagerd
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           AArch64
  Version:                           0x1
  Entry point address:               0x201000
  Start of program headers:          64 (bytes into file)
  Start of section headers:          232208 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         5
  Size of section headers:           64 (bytes)
  Number of section headers:         32
  Section header string table index: 31

How to view the detailed compilation process

  • Set in the cmake configuration file:set(CMAKE_VERBOSE_MAKEFILE ON)

  • make V=1Or VERBOSE=onyou can make the compilation more detailed

  • When compiling make, you can redirect the serial port printing to a file, so it may be more convenient to view the compilation information file.

make VERBOSE=on 2>&1 | tee output_log.txt

summary

  • Since cmake is not often used, in most cases, you can manually write Makefile, and then use make to compile, but the use of cmake should make it easier to build and compile

  • A preliminary understanding of the operation of setting and reading [environment variables] in cmake, which is used to add setting link scripts in gcc compilation parameters

Guess you like

Origin blog.csdn.net/tcjy1000/article/details/132508782