激情综合丁香-激情综合六月-激情综合婷婷亚洲图片-激情综合图区-激情综合网五月

VS Code / CMake創建嵌入式Linux C/C++應用

 2023-3-8              

VS Code是目前最流行的C/C++程序開發環境之一,利用VS CodeCMake工程模式的支持,創建面向嵌入式Linux平臺的C/C++應用,對工程管理非常方便,值得對此作專門的介紹(以下介紹以英創的嵌入式主板ESM7000為例)。本文已假設VS CodeC/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

cmake-kit.png

 

選擇工程名稱test_isa,生成代碼類型為Executable

cmake-project-name.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配置交叉編譯工具

這時,需要把交叉編譯工具聯入CMakeconfigure中,方法是File -> Preferences -> Settings -> Workspace -> Extensions -> CMake Tools,在CMake: Configure Args添加:

cmake-configure-args.png

 

上述操作會讓vscode在工程的.vscode隱含目錄下創建settings.json如下:

{
    "cmake.configureArgs": [
        "-DCMAKE_TOOLCHAIN_FILE=${workspaceFolder}/esm7000_toolchain.cmake"
    ]
}


運行CMake命令,Ctrl-Shft-P -> CMake: Delete Cache and Reconfigure,就可從VS CodeOUTPUT窗口(在下方)的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 CodeC/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 CodeEditor可正確索引頭文件內容,方便編程和調試。

 

小結

本文介紹了在VS Code環境下,為嵌入式Linux平臺創建CMake的工程的方法。其中重要的是操作順序:

1.       打開Terminal并轉至工程所在目錄

2.       Terminal命令行執行source environment-setup…腳本,設置基本環境變量

3.       Terminal命令行執行code . 啟動VS Code

順序之所以重要,是因為CMake kitesm7000_toolchain.cmake文件都依賴于交叉編譯工具鏈的環境變量。

英創的嵌入式主板產品ESM335xESM6800ESM6800HESM7000ESM8000等,采用不同的交叉編譯工具,它們的產品資料中都有各自對應的esm####_toolchain.cmake文件。可采用本文介紹的方法創建各自的CMake工程。

 

另外在WSL/Ubuntu環境,需要WSL下的CMake升級最新版本(目前是3.25.2),本文介紹的方法才能夠正常運行。

主站蜘蛛池模板: 日韩美女毛片| 91久久亚洲精品一区二区| 成人黄色片在线观看| 国产在线精品一区二区高清不卡| 第一页亚洲| 国产精品久久久久影视不卡| 成年网站视频在线观看| 国产成人拍精品视频网| 黄色aa视频| 91精品国产91久久久久久| 国产亚洲福利| 91精品天美精东蜜桃传媒免费| 日韩一区在线播放| 亚洲特级毛片| 全免费毛片在线播放| 欧美日韩高清一区| 国产成人18黄网站免费网站| 草草影院欧美| 黄色网址网站| 欧美一级黄色片| 亚洲精品色综合色在线观看| 91热爆在线精品| 一级大片黄色| 久久国产精品2020免费m3u8| 日韩高清在线高清免费| 亚洲一区二区三区视频| 在线视频毛片| 成年人网站在线观看视频| 免费在线看黄的网站| 欧美成人看片黄a免费| 国产女人毛片| 国产丝袜制服| 黄色片免费看视频| 精品日本亚洲一区二区三区| 草草在线播放| a级国产| jpnesxxx日本| 国产91久久精品一区二区| 国产剧情麻豆mv在线观看| 国产日韩欧美| 国产欧美另类久久久精品免费 |