Cmake Learning Notes

Keywords: C Linux cmake

Write CMakeLists.txt
 
# Create a new build directory in the current directory, and then cd build; cmake..
# The advantage is that the content generated by cmake can be separated from the source file.
 
# Setting Compile Result Publishing Path
set( CMAKE_INSTALL_PREFIX ./_install)
# Configuring a header file allows some cmake settings to be passed to the source code, such as version number, macro definition, etc.
configure_file (
"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
"${PROJECT_BINARY_DIR}/TutorialConfig.h"
)
# Associate an executable file with its source code
add_executable(Tutorial tutorial.c)
# If there are many source codes, you can write a variable, first collect the source files, such as the following.
set(SRC_FILES avl.c rb.c splay.c)
add_executable(Tutorial ${SRC_FILES})
# Add library files
add_library(MathFunctions mysqrt.cxx)
# Adding dynamic libraries, SRC_TREE_FILES implements a well-defined list of source files
add_library(tree SHARED ${SRC_TREE_FILES})
# Adding static libraries
add_library(tree_static STATIC ${SRC_TREE_FILES})
# Link library files to executable files
target_link_libraries (Tutorial MathFunctions)
# How to connect system libraries, but it must be added before adding_executable
LINK_LIBRARIES(-lpthread -lm)
# Verify that the system provides certain functions
# does this system provide the log and exp functions?
include (CheckFunctionExists)
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)
# It also needs to be added to the configuration file TutorialConfig.h so that the macro definition can be used in the source code to determine whether the function is supported or not.
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP
# Header file search paths can be increased
include_directories ("${PROJECT_BINARY_DIR}")
# Or add to a target
target_include_directories(os_demo
    PRIVATE
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/inc
)
# How to add compilation options; you can set compilation options by adding_compile_options command, or you can modify CMAKE_CXX_FLAGS or CMAKE_C_FLAGS by setting command
SET(CMAKE_C_FLAGS "-O3 -ffast-math -Wall  -std=gnu99 -fPIC")
Question?
Where are the definitions of ${CMAKE_CURRENT_BINARY_DIR}, ${PROJECT_SOURCE_DIR}, ${PROJECT_BINARY_DIR}?
# Through print display; CMAKE_CURRENT_BINARY_DIR, PROJECT_BINARY_DIR is the current directory, PROJECT_SOURCE_DIR is the directory of CMakeLists.txt.
-- This is the engine CMAKE_CURRENT_BINARY_DIR: /home/xxxx/engine/build.tx1
-- This is the engine PROJECT_SOURCE_DIR: /home/xxxx/engine
-- This is the engine PROJECT_BINARY_DIR: /home/xxxx/build.tx1
# How to cross-compile conveniently? CMAKE_TOOLCHAIN_FILE is commonly used
cmake .. -DCMAKE_TOOLCHAIN_FILE=../tx1.toolchain.cmake
# Here's a simple example of toolchain.cmake
# this is required
SET(CMAKE_SYSTEM_NAME Linux)
 
# specify the cross compiler
SET(CMAKE_C_COMPILER   /opt/nvidia/gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu//bin/aarch64-linux-gnu-gcc)
SET(CMAKE_CXX_COMPILER /opt/nvidia/gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu//bin/aarch64-linux-gnu-g++)
 
# where is the target environment
SET(CMAKE_FIND_ROOT_PATH /opt/nvidia/gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/include/c++/5.3.1)
 
# search for programs in the build host directories (not necessary)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
 

 

Posted by jervo on Sat, 11 May 2019 12:55:16 -0700