VSCode 使用CMake进行交叉编译

在VSCode中安装CMake Tools工具

在工程目录下创建CMakeLists.txt

【原来的交叉编译环境】
 

aarch64-linux-gnu  (目录)

aarch64-linux-gnu.toolchain 

environment-setup-aarch64-linux-gnu 

README 

setup.sh 

src  (目录)

toolchain (目录)

需要在命令行敲:

. ./environment-setup-aarch64-linux-gnu  (两个点, 中间有空格)

这样就会初始化相应的编译环境。environment-setup-aarch64-linux-gnu 如下:

export SDKTARGETSYSROOT=/xxx
export PATH=/xxx/toolchain/bin:$PATH
export PKG_CONFIG_SYSROOT_DIR=$SDKTARGETSYSROOT
export PKG_CONFIG_PATH="$SDKTARGETSYSROOT/usr/lib/pkgconfig:$SDKTARGETSYSROOT/usr/share/pkgconfig"
export CC="aarch64-linux-gnu-gcc --sysroot=$SDKTARGETSYSROOT"
export CXX="aarch64-linux-gnu-g++ --sysroot=$SDKTARGETSYSROOT"
export CPP="aarch64-linux-gnu-cpp --sysroot=$SDKTARGETSYSROOT"
export AS="aarch64-linux-gnu-as"
export LD="aarch64-linux-gnu-ld --sysroot=$SDKTARGETSYSROOT"
export GDB=aarch64-linux-gnu-gdb
export STRIP=aarch64-linux-gnu-strip
export RANLIB=aarch64-linux-gnu-gcc-ranlib
export OBJCOPY=aarch64-linux-gnu-objcopy
export OBJDUMP=aarch64-linux-gnu-objdump
export AR=aarch64-linux-gnu-gcc-ar
export NM=aarch64-linux-gnu-gcc-nm
export CONFIGURE_FLAGS="--build=x86_64-linux-gnu --host=aarch64-linux-gnu --target=aarch64-linux-gnu"
export CFLAGS="-Os -g -pipe -feliminate-unused-debug-types -march=armv8-a -mtune=cortex-a57.cortex-a53 -ffunction-sections -fdata-sections -Wno-error=deprecated"
export CXXFLAGS="-Os -g -pipe -feliminate-unused-debug-types -march=armv8-a -mtune=cortex-a57.cortex-a53 -ffunction-sections -fdata-sections -std=c++11 -Wno-error=deprecated"
export LDFLAGS="-Wl,-O1 -Wl,--hash-style=gnu -Wl,--gc-sections"
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-

基于此编写CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(MyXXX VERSION 0.1.0)

include(CTest)
enable_testing()

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})


set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
set(CMAKE_SYSTEM_NAME Linux) #目标机target所在的操作系统名称
set(CMAKE_GENERATOR "CodeBlocks - Unix Makefiles")#Unix Makefiles")
set(CMAKE_MAKE_PROGRAM "/usr/bin/make")
set(CMAKE_BUILD_TYPE Release)
#-------

set(CONFIGURE_FLAGS "--build=x86_64-linux-gnu --host=aarch64-linux-gnu --target=aarch64-linux-gnu")
set(ARCH arm64)
set(CROSS_COMPILE aarch64-linux-gnu-)
#---------
set(CROSS_ROOTFS /xxx/aarch64-linux-gnu)
set(TOOLCHAIN_PATH /xxx/toolchain/bin)
set(CMAKE_C_EXTENSIONS "--sysroot=/xxx/aarch64-linux-gnu")#"-pipe -g -Wall -W -fPIE")
set(CMAKE_CXX_EXTENSIONS "--sysroot=/xxx/aarch64-linux-gnu")#-pipe -g -Wall -W -fPIE")
set(CMAKE_EXE_LINKER_FLAGS "--sysroot=/xxx/aarch64-linux-gnu")
set(CMAKE_FIND_ROOT_PATH ${CROSS_ROOTFS})
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)#只在交叉编译库路径中寻找
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/aarch64-linux-gnu-g++)
set(CMAKE_C_COMPILER_AR ${TOOLCHAIN_PATH}/aarch64-linux-gnu-ar)
set(CMAKE_CXX_COMPILER_AR ${TOOLCHAIN_PATH}/aarch64-linux-gnu-ar)
set(CMAKE_C_COMPILER_RANLIB ${TOOLCHAIN_PATH}/aarch64-linux-gnu-ranlib)
set(CMAKE_LINKER ${TOOLCHAIN_PATH}/aarch64-linux-gnu-ld)
set(CMAKE_NM ${TOOLCHAIN_PATH}/aarch64-linux-gnu-nm)
set(CMAKE_OBJCOPY ${TOOLCHAIN_PATH}/aarch64-linux-gnu-objcopy)
set(CMAKE_OBJDUMP ${TOOLCHAIN_PATH}/aarch64-linux-gnu-objdump)
set(CMAKE_RANLIB ${TOOLCHAIN_PATH}/aarch64-linux-gnu-ranlib)
set(CMAKE_READELF ${TOOLCHAIN_PATH}/aarch64-linux-gnu-readelf)
set(CMAKE_STRIP ${TOOLCHAIN_PATH}/aarch64-linux-gnu-strip)



set(CMAKE_CXX_FLAGS "-Os -g -pipe -feliminate-unused-debug-types -march=armv8-a -mtune=cortex-a57.cortex-a53 -ffunction-sections -fdata-sections -std=c++11 -Wno-error=deprecated")
set(CMAKE_C_FLAGS "-Os -g -pipe -feliminate-unused-debug-types -march=armv8-a -mtune=cortex-a57.cortex-a53 -ffunction-sections -fdata-sections -Wno-error=deprecated")

set(CMAKE_EXE_LINKER_FLAGS "-Wl,-O1 -Wl,--hash-style=gnu -Wl,--gc-sections")

#设置执行文件输出目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#设置库输出路径
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
message("++++++++++++++Start Build+++++++++++++++++")


# 添加头文件目录
include_directories(${PROJECT_SOURCE_DIR} 
                    ${PROJECT_SOURCE_DIR}/include 
                    ${CROSS_ROOTFS}/usr/include)

# 添加源文件目录
aux_source_directory(${PROJECT_SOURCE_DIR} ROOTDIR) 
aux_source_directory(${PROJECT_SOURCE_DIR}/src USERSRC)

# 添加子目录 子目录里面放一些别的编译好的模块
#ADD_SUBDIRECTORY(src)

# 链接库搜索路径
link_directories(. 
                 ./lib 
                 ${CROSS_ROOTFS}/lib 
                 ${CROSS_ROOTFS}/usr/lib )


# 添加动态库

set(EXTRA_LIBS libxxx.so
                ...
                libyyy.so 
                )

# 添加可执行文件(可执行文件名 [配置] 源文件)
#add_library( [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL][source1] [source2] [...])

set(MyTarget shm)
set(ShmSRC main.cpp ...)
add_executable(${MyTarget} ${ShmSRC} )

#add_library( [STATIC | SHARED | MODULE] [EXCLUDE_FROM_ALL][source1] [source2] [...])
# 执行文件链接属性
TARGET_LINK_LIBRARIES(${MyTarget} ${EXTRA_LIBS})


include(CPack)

文件目录下建立Build,

mkdir Build
cmake ..
make

你可能感兴趣的:(vscode,c++)