1

我正在尝试使用 CMake 和 cmake-tools for Visual Studio Code 为我的 STM32 蓝色药丸板配置模板项目。它构建得很好,但 cmake-tools 的 Intellisense 功能似乎不适用于外部库(在本例中为 libopencm3)。

这很可能是我这边的配置错误。按照我的 CMakeLists.txt。工具链在单独的 .cmake 文件中定义。

set(PRJ_NAME STM32_Template_Project)

cmake_minimum_required(VERSION 3.6)

project(${PRJ_NAME} C ASM)

set(COMMON_FLAGS "-mcpu=${MCU_ARCH} -mthumb -mthumb-interwork -mfloat-abi=${MCU_FLOAT_ABI} -ffunction-sections -fdata-sections -g -fno-common -fmessage-length=0 -DSTM32F1")

if (MCU_FLOAT_ABI STREQUAL hard)
    set(COMMON_FLAGS "${COMMON_FLAGS} -mfpu=${MCU_FPU}")
endif ()

set(CMAKE_CXX_FLAGS "${COMMON_FLAGS} -std=c++11")
set(CMAKE_C_FLAGS "${COMMON_FLAGS} -std=gnu99")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-gc-sections -T ${CMAKE_SOURCE_DIR}/${MCU_LINKER_SCRIPT}")

set(DEFINITIONS "${DEFINITIONS} -D${MCU_LINE}")
set(LIBOPENCM3_DIR "../libopencm3")

#headers
include_directories(sys)
include_directories(Inc)
include_directories(${LIBOPENCM3_DIR}/include)

# libopencm3
# Make sure that git submodule is initialized and updated
if (NOT EXISTS ${LIBOPENCM3_DIR}/Makefile)
    message(FATAL_ERROR "libopencm3 submodule not found. Initialize with 'git submodule update --init' in the source directory")
endif()


add_custom_target(libopencm3 make TARGETS=stm32/f1 WORKING_DIRECTORY ${LIBOPENCM3_DIR})
link_directories(${LIBOPENCM3_DIR}/lib)


#files
file(GLOB_RECURSE USER_SOURCES Src/*.c)
set(CMSIS_STARTUP startup_${MCU_LINE}.s)


set(SOURCE_FILES ${USER_SOURCES} ${CMSIS_STARTUP} ${MCU_LINKER_SCRIPT})

# target
add_executable(${PROJECT_NAME}.elf ${SOURCE_FILES})

target_link_libraries(${PROJECT_NAME}.elf opencm3_stm32f1)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.map")
set(HEX_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.bin)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMENT "Building ${HEX_FILE} \nBuilding ${BIN_FILE}")

我对 CMake 和 cmake-tools 不是很有经验。

4

0 回答 0