如何为 arm gcc 构建 mbedtls
Posted
技术标签:
【中文标题】如何为 arm gcc 构建 mbedtls【英文标题】:How to build mbedtls for arm gcc 【发布时间】:2021-07-11 07:22:13 【问题描述】:我想在我的 stm32 项目中使用 mbedtls,但我在构建时遇到了一些问题。 我必须用 arm-none-gcc 编译器构建 mbedtls,对吗? 我的命令是:(在构建目录中)。
CC=/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc CFLAGS='-fstack-protector-strong' cmake -DUSE_SHARED_MBEDTLS_LIBRARY=On ../
我在编译测试程序时出错:
none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc CFLAGS='-fstack-protector-strong' cmake -
DUSE_SHARED_MBEDTLS_LIBRARY=On ../
-- The C compiler identification is GNU 10.2.1
-- Check for working C compiler:
/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4-
major/bin/arm-none-eabi-gcc
-- Check for working C compiler:
/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4-
major/bin/arm-none-eabi-gcc -- broken
CMake Error at /usr/share/cmake-3.16/Modules/CMakeTestCCompiler.cmake:60 (message):
The C compiler
"/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4-
major/bin/arm-none-eabi-gcc"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/jareeeeczek/Arczbit/Projects/Parkometr/software/FreeRtos/FreeRTOS-
Plus/ThirdParty/mbedtls/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/make cmTC_47d0a/fast && /usr/bin/make -f
CMakeFiles/cmTC_47d0a.dir/build.make CMakeFiles/cmTC_47d0a.dir/build
make[1]: Entering directory
'/home/jareeeeczek/Arczbit/Projects/Parkometr/software/FreeRtos/FreeRTOS-
Plus/ThirdParty/mbedtls/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_47d0a.dir/testCCompiler.c.o
/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4-
major/bin/arm-none-eabi-gcc -fstack-protector-strong -o
CMakeFiles/cmTC_47d0a.dir/testCCompiler.c.o -c
/home/jareeeeczek/Arczbit/Projects/Parkometr/software/FreeRtos/FreeRTOS-
Plus/ThirdParty/mbedtls/build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_47d0a
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_47d0a.dir/link.txt --verbose=1
/home/jareeeeczek/Arczbit/Firmware/ProgramingRelated/ARM_GCC/gcc-arm-none-eabi-10-2020-q4-
major/bin/arm-none-eabi-gcc -fstack-protector-strong -rdynamic
CMakeFiles/cmTC_47d0a.dir/testCCompiler.c.o -o cmTC_47d0a
arm-none-eabi-gcc: error: unrecognized command-line option '-rdynamic'
make[1]: *** [CMakeFiles/cmTC_47d0a.dir/build.make:87: cmTC_47d0a] Error 1
make[1]: Leaving directory
'/home/jareeeeczek/Arczbit/Projects/Parkometr/software/FreeRtos/FreeRTOS -
Plus/ThirdParty/mbedtls/build/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_47d0a/fast] Error 2
有人知道为什么 cmake 编译测试程序有问题吗?
【问题讨论】:
告诉你出了什么问题的那一行是“error: unrecognized command-line option '-rdynamic'” @Arkadiusz Bryń:答案有帮助吗?是否需要澄清/改进?如果是,您希望如何对其进行修改/改进?反馈将不胜感激 - 谢谢。 【参考方案1】:我通过将这些行添加到 mbedtls Cmake 解决了这个问题:
设置(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") 设置(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
感谢您的帮助!
【讨论】:
【参考方案2】:我认为您没有配置 mbedtls
以便可以为裸机系统构建它。需要禁用一些默认选项,因为您的目标不是通用操作系统:(没有文件系统,所以标准 BSD 套接字接口,...)。
这就是您无法编译测试程序的原因,也是您需要通过在 cmake 构建命令中指定 -DENABLE_TESTING=OFF
来禁用它们的编译的原因。
请参阅 mbedtls-2.25.0/include/mbedtls/config.h
中大量有用的 cmets,以获取有关可用的各种配置选项的更多详细信息。
下文描述的过程确实有效,您应该能够通过将其用作工作示例来修改自己的构建过程。
toochain.cmake
:
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR armv7-m)
set(CMAKE_STAGING_PREFIX /tmp/staging)
set(CMAKE_C_COMPILER /opt/arm/10/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER /opt/arm/10/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-g++)
set(CMAKE_MAKE_PROGRAM=/usr/bin/make)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_C_FLAGS=-mtune=cortex-m3)
set(CMAKE_EXE_LINKER_FLAGS " --specs=nosys.specs")
构建过程:
# extracting
tar zxf v2.25.0.tar.gz
# modifying default configuration using config.py:
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h set MBEDTLS_NO_PLATFORM_ENTROPY 1
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_FS_IO
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_PSA_CRYPTO_STORAGE_C
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_TIMING_C
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_NET_C
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_PSA_CRYPTO_CONFIG
mbedtls-2.25.0/scripts/config.py --file mbedtls-2.25.0/include/mbedtls/config.h unset MBEDTLS_PSA_ITS_FILE_C
# building:
mkdir mbedtls
pushd mbedtls
cmake -DCMAKE_BUILD_TYPE=Release -DUSE_SHARED_MBEDTLS_LIBRARY=OFF -DUSE_STATIC_MBEDTLS_LIBRARY=ON -DENABLE_ZLIB_SUPPORT=OFF -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake ../mbedtls-2.25.0
make all install
popd
tree /tmp/staging
/tmp/staging
├── include
│ ├── mbedtls
│ │ ├── aes.h
│ │ ├── aesni.h
│ │ ├── arc4.h
│ │ ├── aria.h
│ │ ├── asn1.h
│ │ ├── asn1write.h
│ │ ├── base64.h
│ │ ├── bignum.h
│ │ ├── blowfish.h
│ │ ├── bn_mul.h
│ │ ├── camellia.h
│ │ ├── ccm.h
│ │ ├── certs.h
│ │ ├── chacha20.h
│ │ ├── chachapoly.h
│ │ ├── check_config.h
│ │ ├── cipher.h
│ │ ├── cipher_internal.h
│ │ ├── cmac.h
│ │ ├── compat-1.3.h
│ │ ├── config.h
│ │ ├── config_psa.h
│ │ ├── ctr_drbg.h
│ │ ├── debug.h
│ │ ├── des.h
│ │ ├── dhm.h
│ │ ├── ecdh.h
│ │ ├── ecdsa.h
│ │ ├── ecjpake.h
│ │ ├── ecp.h
│ │ ├── ecp_internal.h
│ │ ├── entropy.h
│ │ ├── entropy_poll.h
│ │ ├── error.h
│ │ ├── gcm.h
│ │ ├── havege.h
│ │ ├── hkdf.h
│ │ ├── hmac_drbg.h
│ │ ├── md2.h
│ │ ├── md4.h
│ │ ├── md5.h
│ │ ├── md.h
│ │ ├── md_internal.h
│ │ ├── memory_buffer_alloc.h
│ │ ├── net.h
│ │ ├── net_sockets.h
│ │ ├── nist_kw.h
│ │ ├── oid.h
│ │ ├── padlock.h
│ │ ├── pem.h
│ │ ├── pkcs11.h
│ │ ├── pkcs12.h
│ │ ├── pkcs5.h
│ │ ├── pk.h
│ │ ├── pk_internal.h
│ │ ├── platform.h
│ │ ├── platform_time.h
│ │ ├── platform_util.h
│ │ ├── poly1305.h
│ │ ├── psa_util.h
│ │ ├── ripemd160.h
│ │ ├── rsa.h
│ │ ├── rsa_internal.h
│ │ ├── sha1.h
│ │ ├── sha256.h
│ │ ├── sha512.h
│ │ ├── ssl_cache.h
│ │ ├── ssl_ciphersuites.h
│ │ ├── ssl_cookie.h
│ │ ├── ssl.h
│ │ ├── ssl_internal.h
│ │ ├── ssl_ticket.h
│ │ ├── threading.h
│ │ ├── timing.h
│ │ ├── version.h
│ │ ├── x509_crl.h
│ │ ├── x509_crt.h
│ │ ├── x509_csr.h
│ │ ├── x509.h
│ │ └── xtea.h
│ └── psa
│ ├── crypto_accel_driver.h
│ ├── crypto_compat.h
│ ├── crypto_config.h
│ ├── crypto_driver_common.h
│ ├── crypto_entropy_driver.h
│ ├── crypto_extra.h
│ ├── crypto.h
│ ├── crypto_platform.h
│ ├── crypto_se_driver.h
│ ├── crypto_sizes.h
│ ├── crypto_struct.h
│ ├── crypto_types.h
│ └── crypto_values.h
└── lib
├── libmbedcrypto.a
├── libmbedtls.a
└── libmbedx509.a
4 directories, 96 files
【讨论】:
嗨@Arkadiusz Bryń:如果这个或任何答案解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。当然没有义务这样做。以上是关于如何为 arm gcc 构建 mbedtls的主要内容,如果未能解决你的问题,请参考以下文章