VS Code是目前最流行的C/C++程序开发环境之一,利用VS Code对CMake工程模式的支持,创建面向嵌入式Linux平台的C/C++应用,对工程管理非常方便,值得对此作专门的介绍(以下介绍以英创的嵌入式主板ESM7000为例)。本文已假设VS Code及C/C++插件、CMake插件均已正确安装在开发主机Linux / Ubuntu环境中。
创建基本的工程目录
在Terminal窗口,创建一个测试精简ISA总线的project目录(test_isa):
xps15:~/esm7000/app$ mkdir test_isa
拷贝两个基本配置文件,文件environment-setup-cortexa7hf-neon-poky-linux-gnueabi是交叉编译工具路径下的配置脚本文件,它包含了交叉编译工具链的安装路径信息;文件esm7000_toolchain.cmake是面向交叉编译工具的CMake描述文件,包含在英创的主板资料中。用于CMake Configure。这样在<test_isa>目录下可看到2个配置文件如下:
xps15:~/esm7000/app/test_isa$ ls -l
-rw-r--r-- 1 … 3441 Mar 6 10:17 environment-setup-cortexa7hf-neon-poky-linux-gnueabi
-rw-r--r-- 1 … 1895 Mar 6 10:17 esm7000_toolchain.cmake
在Terminal窗口运行source交叉编译工具,然后启动vscode:
xps15:~/esm7000/app/test_isa$ source environment-setup-cortexa7hf-neon-poky-linux-gnueabi
xps15:~/esm7000/app/test_isa$ code .
VSCODE插件(Extension)的功能,都是通过命令来操作配置的,命令的启动方式一般为:Ctrl-Shft-P -> <命令关键词>….,我们常用的命令有C/C++、CMake等。进入VS Code后,首先需要创建CMake配置:Ctrl-Shft-P -> CMake: Quick Start,选择kit -> GCC 6.2.0 arm-poky-linux-gnueabi:
![](https://i-blog.csdnimg.cn/blog_migrate/2dd30362fd6aef5f0936efb39bd05150.png)
选择工程名称test_isa,生成代码类型为Executable。
![](https://i-blog.csdnimg.cn/blog_migrate/4baa0edb7aa7f2c08f99830f7ec28988.png)
于是,vscode会自动生成CMakeLists.txt如下:
cmake_minimum_required(VERSION 3.0.0)
project(test_isa VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(test_isa main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
为CMake配置交叉编译工具
这时,需要把交叉编译工具联入CMake的configure中,方法是File -> Preferences -> Settings -> Workspace -> Extensions -> CMake Tools,在CMake: Configure Args添加:
![](https://i-blog.csdnimg.cn/blog_migrate/d6457591ca64b91b86d5fe63c40fb46e.png)
上述操作会让vscode在工程的.vscode隐含目录下创建settings.json如下:
{
"cmake.configureArgs": [
"-DCMAKE_TOOLCHAIN_FILE=${workspaceFolder}/esm7000_toolchain.cmake"
]
}
运行CMake命令,Ctrl-Shft-P -> CMake: Delete Cache and Reconfigure,就可从VS Code的OUTPUT窗口(在下方)的CMake Configure信息:
[main] Configuring project: test_isa
[driver] Removing /home/x10/esm7000/app/test_isa/build/CMakeCache.txt
[driver] Removing /home/x10/esm7000/app/test_isa/build/CMakeFiles
[proc] Executing command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_TOOLCHAIN_FILE=/home/x10/esm7000/app/test_isa/esm7000_toolchain.cmake -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/home/x10/esm7000/toolchain/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -DCMAKE_CXX_COMPILER:FILEPATH=/home/x10/esm7000/toolchain/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ -S/home/x10/esm7000/rootfs/usr/app/test_isa -B/home/x10/esm7000/rootfs/usr/app/test_isa/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 6.2.0
[cmake] -- The CXX compiler identification is GNU 6.2.0
[cmake] -- Check for working C compiler: /home/x10/esm7000/toolchain/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc
[cmake] -- Check for working C compiler: /home/x10/esm7000/toolchain/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc -- works
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Check for working CXX compiler: /home/x10/esm7000/toolchain/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++
[cmake] -- Check for working CXX compiler: /home/x10/esm7000/toolchain/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ -- works
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: /home/x10/esm7000/app/test_isa/build
[cmakefileapi-driver] This version of CMake does not support the "toolchains" object kind. Compiler paths will be determined by reading CMakeCache.txt.
CMake配置成功后,点击VS Code下方状态栏上的Build键,即可对应用进行交叉编译:
[main] Building folder: test_isa
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/x10/esm7000/app/test_isa/build --config Debug --target all -j 10 --
[build] Scanning dependencies of target test_isa
[build] [ 50%] Building CXX object CMakeFiles/test_isa.dir/main.cpp.o
[build] [100%] Linking CXX executable test_isa
[build] [100%] Built target test_isa
[build] Build finished with exit code 0
当然,也可切换到Release模式进行Build。
添加实际的应用代码
拷贝之前已经过验证的test_isa的相关代码到~/esm7000/app/test_isa/路径下,并把source code文件加入CMakeLists.txt:
cmake_minimum_required(VERSION 3.1.0)
set(PRJNAME test_isa)
project(${PRJNAME} VERSION 0.2.0)
# Set the variable CMAKE_CXX_STANDARD to 11
# and the variable CMAKE_CXX_STANDARD_REQUIRED to True
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# Use configure_file to configure and copy UserConfig.h.in to UserConfig.h
configure_file(UserConfig.h.in UserConfig.h)
include(CTest)
enable_testing()
set(SOURCEFILES test_isa.cpp isa_api_v3.cpp isa_dma_ext.cpp isa_dma_mmap.cpp)
add_executable(${PRJNAME} ${SOURCEFILES})
# Use target_include_directories to include ${PROJECT_BINARY_DIR}
target_include_directories(${PRJNAME} PRIVATE ${PROJECT_BINARY_DIR})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
点击Build进行编译,可看到多个cpp文件均被正常编译:
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/x10/esm7000/app/test_isa/build --config Release --target all -j 10 --
[build] Scanning dependencies of target test_isa
[build] [ 20%] Building CXX object CMakeFiles/test_isa.dir/isa_dma_ext.cpp.o
[build] [ 40%] Building CXX object CMakeFiles/test_isa.dir/isa_api_v3.cpp.o
[build] [ 60%] Building CXX object CMakeFiles/test_isa.dir/test_isa.cpp.o
[build] [ 80%] Building CXX object CMakeFiles/test_isa.dir/isa_dma_mmap.cpp.o
[build] [100%] Linking CXX executable test_isa
[build] [100%] Built target test_isa
[build] Build finished with exit code 0
让C/C++ IntelliSense正确感知
VS Code的C/C++插件可使编辑器正常感知包含在交叉编译工具里面的目标运行环境。具体做法是通过C/C++命令来进行必要的设置:Ctrl-Shft-P -> C/C++: Edit Configure (UI)。设置完成后,VS Code会自动在.vscode路径在生成c_cpp_properties.json如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${SDKTARGETSYSROOT}/usr/include/**"
],
"defines": [
"__ARM_PCS_VFP"
],
"compilerPath": "${OECORE_NATIVE_SYSROOT}/usr/bin/arm-poky-linux-gnueabi/${CROSS_COMPILE}g++",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "linux-gcc-arm",
"configurationProvider": "ms-vscode.makefile-tools",
"compilerArgs": [
"-march=armv7ve",
"-mfpu=neon ",
"-mfloat-abi=hard ",
"-mcpu=cortex-a7 ",
"--sysroot=${SDKTARGETSYSROOT}"
]
}
],
"version": 4
}
正确配置目标运行环境,使VS Code的Editor可正确索引头文件内容,方便编程和调试。
小结
本文介绍了在VS Code环境下,为嵌入式Linux平台创建CMake的工程的方法。其中重要的是操作顺序:
- 打开Terminal并转至工程所在目录
- 在Terminal命令行执行source environment-setup…脚本,设置基本环境变量
- 在Terminal命令行执行code . 启动VS Code
顺序之所以重要,是因为CMake kit和esm7000_toolchain.cmake文件都依赖于交叉编译工具链的环境变量。
尽管英创的嵌入式主板产品ESM335x、ESM6800、ESM6800H、ESM7000、ESM8000等,采用不同的交叉编译工具,但它们都有各自对应的esm####_toolchain.cmake文件。可采用本文介绍的方法创建各自的CMake工程。
另外在WSL/Ubuntu环境,需要把WSL下的CMake升级最新版本(目前是3.25.2),本文介绍的方法才能够正常运行。