[Cross compilation one] CMake compilation options

1. The specific meanings of commonly used CMake options are as follows:

 PROJECT (target name)

 After ADD_DEFINITIONS(-g) is added, it is equivalent to adding the -g option when compiling

After ADD_DEFINITIONS(-Os) is added, it is equivalent to adding the -Os option when compiling

ADD_DEFINITIONS(-D DEBUG_WARN) will add DEBUG_WARN macro definition on the gcc command line

SET(CMAKE_CXX_FLAGS "") Compiler option settings
SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS ""

SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-export-dynamic") does not export all global symbols to the dynamic symbol table

SET(CMAKE_SYSTEM_NAME linux)
SET(TOOLCHAIN_DIR "编译链目录")
SET(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_DIR})
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}C编译链)
SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}C++编译链)

INCLUDE_DIRECTORIES (directory) header files used for compilation

SET (SRCSRC_DIRS directory) Compiled source file path

list(APPEND SOURCES ${SRCSRC_DIRS }/xx.c) Specify the compiled source file

LINK_DIRECTORIES (directory) The path of the dependent library

SET (LIBS_LIB library name) dependent libraries

LINK_LIBRARIES(pthread rt m ${LIBS_LIB} dl) The library to be linked

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../../../Bin/)   指定生成的文件路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../../../Bin/)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/../../../Bin/)

ADD_LIBRARY(${PROJECT_NAME} SHARED ${SOURCES}) dynamic library

ADD_LIBRARY(${PROJECT_NAME} STATIC ${SOURCES}) static library

ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCES}) executable file

TARGET_LINK_LIBRARIES(${PROJECT_NAME} -Wl,--start-group ${LIBS_LIB} -Wl,--end-group) Solve the problem of static library sequence when linking

2. Part of the content description

-g Generate debugging information

-O0 No optimization processing

-O or -O1 optimize the generated code

-O2 is further optimized.
-O3 is further optimized than -O2, including inline functions

-Os: Execute all -O2 options that do not increase the size of the target file, and execute optimization options that specifically reduce the size of the target file

-Wall generates all warning messages

-Wl passes the following parameters to the linker

3. GCC/G++ parameters

-s delete the symbol table from the final executable file

-ffunction-sections -fdata-sections Create each function or symbol as a section, where each section name is consistent with the function or data name

-fvisibility=hidden Set the default visibility of the symbols in the source file. When set to default, all symbols that are not explicitly marked as hidden are processed as visible; when set to hidden, symbols that are not explicitly marked as visible are processed as hidden For interface functions, you can add __attribute__ ((visibility ("default"))) before the function declaration , so that the symbols are exported separately

-fno-exceptions Disable the exception mechanism. Generally, it will be used only when the efficiency and resource usage of the program are more important.

-fno-rtti disable runtime type information      

-fno-unwind-tables

-fno-asynchronous-unwind-tables prohibit generation .eh_frameand .eh_frame_hdrsection

-fomit-frame-pointer SFP can be optimized at compile time ( the optimization effect of " -fomit-frame-pointer " option on the arm platform is more obvious)

-Wl,-Bsymbolic Force the use of local global variable definitions, so that the global variable definitions of the dynamic link library will not be overwritten by the definition of the same name in the application/dynamic link library

Parameters used when connecting

-Wl,–gc-sections remove unused sections and reduce the size of the program

-Wl, --no-export-dynamic do not export all global symbols to the dynamic symbol table

 

Guess you like

Origin blog.csdn.net/Swallow_he/article/details/110938522