我尝试在我的嵌入式stm32f4微控制器代码中使用complex.h
和math.h
。考虑下面的示例代码:
#include <math.h>
#include <complex.h>
#define PI 3.141593f
void sample_function(uint8_t s) {
float t = (float)1 / s;
float complex var1 = cexpf(2 * PI * I * t); // linker error undefined reference to cexpf
float complex var2 = cexpf(2 * PI * I); // this works
// print creal and cimag parts of var1 and var2
}
代码编译,但我得到链接器错误,因为未定义的引用到cexpf
。但是,将编译时已知的参数(如PI
和const编号)传递给cexpf
不会产生错误。sinf
和cosf
函数也是如此。CmakeLists.txt
中的链接器选项:
target_link_options(${APPLICATION} PRIVATE
-T${APP_LINKER_SCRIPT}
${CPU_PARAMETERS}
-Wl,-Map=${APPLICATION}.map
-Wl,--start-group
-Wl,--no-warn-rwx-segments
-lc
-lm
# -lnosys
-lstdc++
-Wl,--end-group
-Wl,--print-memory-usage)
arm-none-eabi-gcc -v
命令的输出:
$ arm-none-eabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-none-eabi/13.2.0/lto-wrapper
Target: arm-none-eabi
Configured with: /build/arm-none-eabi-gcc/src/gcc-13.2.0/configure --target=arm-none-eabi --prefix=/usr --with-sysroot=/usr/arm-none-eabi --with-native-system-header-dir=/include --libexecdir=/usr/lib --enable-languages=c,c++ --enable-plugins --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --disable-tls --with-gnu-as --with-gnu-ld --with-system-zlib --with-newlib --with-headers=/usr/arm-none-eabi/include --with-python-dir=share/gcc-arm-none-eabi --with-gmp --with-mpfr --with-mpc --with-isl --with-libelf --enable-gnu-indirect-function --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-pkgversion='Arch Repository' --with-bugurl=https://bugs.archlinux.org/ --with-multilib-list=rmprofile
Thread model: single
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.0 (Arch Repository)
同样,如果编译和链接没有var1
的代码,arm-none-eabi-readelf -a path/to/application.elf | grep "libmath"
的结果也是空的。
有人知道我该怎么解决这个问题吗?
1条答案
按热度按时间ssm49v7z1#
好吧,经过一番搜索,我发现了。这个错误可以通过在目标文件后添加
-lm
标志来解决。但是正如here所解释的,target_link_options
命令在所有其他命令之前设置选项,我应该使用target_link_libraries
命令来实现这个目的。因此,下面的行在所有对象文件之后添加了到数学库的链接。