我已经盯着这个太久了,可能只是没有看到一些明显的东西。
我正在使用 Raspberry Pi Zero2 W 的 /usr/bin/aarch64-linux-gnu-g++-12 交叉编译器在 Ubuntu 24.04 上进行交叉编译。我的工作正常。我想创建一个只包含我正在访问的设备的一些延迟值的地图。在包含文件中我有:
154 typedef enum {
155 SHT4X_TIMING_SOFT_RESET,
156 SHT4X_TIMING_MEASUREMENT_LOW_REPEATABILITY,
157 SHT4X_TIMING_MEASUREMENT_MED_REPEATABILITY,
158 SHT4X_TIMING_MEASUREMENT_HIGH_REPEATABILITY,
159 SHT4X_TIMING_HEATER_DURATION_LONG,
160 SHT4X_TIMING_HEATER_DURATION_SHORT,
161 } Sht4xMaxTimings;
162
163 /*
164 * These are in microseonds
165 */
166 map<Sht4xMaxTimings, int> sht4x_min_delays = {
167 {SHT4X_TIMING_SOFT_RESET,1000}, /* tpu 1 millisecond */
168 /*
169 * For the next 3 the table says including tpu. I take that
170 * to mean I should add the value above, tpu, to these values
171 * in the table. I may have that wrong though.
172 */
173 {SHT4X_TIMING_MEASUREMENT_LOW_REPEATABILITY, 1600 + 1000}, /* 1.6 millisecond + tpu = 2.6 milliseconds*/
174 {SHT4X_TIMING_MEASUREMENT_MED_REPEATABILITY, 500 + 1000}, /* 4.5 milliseconds + tpu = 5.5 milliseconds */
175 {SHT4X_TIMING_MEASUREMENT_HIGH_REPEATABILITY, 8300 + 1000}, /* 8.3 milliseconds + tpu = 9.3 milliseconds */
176 {SHT4X_TIMING_HEATER_DURATION_LONG, 1100000}, /* 1.1 seconds */
177 {SHT4X_TIMING_HEATER_DURATION_SHORT, 110000} /* 0.11 seconds */
178 };
这些值只是根据调用的命令传递给 usleep() 。现在,我将所有 usleep() 命令行注释掉,只是为了确保编译器看到的具有此名称的变量的唯一机会是在第 166 行定义的。当我在这里编译时,出现错误:
[main] Building folder: /home/chrisk/Projects/RaspberryPi/WS/build
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/chrisk/Projects/RaspberryPi/WS/build --config Debug --target all --
[build] [2/4 25% :: 0.757] Building CXX object src/CMakeFiles/WeatherStation.dir/main.cpp.o
[build] [2/4 50% :: 0.878] Building CXX object src/lib/devices/i2c/CMakeFiles/i2cdevices.dir/sht4x.cpp.o
[build] [3/4 75% :: 0.931] Linking CXX static library src/lib/devices/i2c/libi2cdevices.a
[build] [4/4 100% :: 1.044] Linking CXX executable src/WeatherStation
[build] FAILED: src/WeatherStation
[build] : && /usr/bin/aarch64-linux-gnu-g++-12 --sysroot=/opt/sdk/rpi_zero_2_w -g src/CMakeFiles/WeatherStation.dir/main.cpp.o -o src/WeatherStation src/lib/devices/i2c/libi2cdevices.a src/lib/units/libunits.a && :
[build] /usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux-gnu/bin/ld: src/lib/devices/i2c/libi2cdevices.a(sht4x.cpp.o):/home/chrisk/Projects/RaspberryPi/WS/src/lib/devices/i2c/include/sht4x.h:166: multiple definition of `sht4x_min_delays'; src/CMakeFiles/WeatherStation.dir/main.cpp.o:/home/chrisk/Projects/RaspberryPi/WS/src/lib/devices/i2c/include/sht4x.h:166: first defined here
[build] collect2: error: ld returned 1 exit status
[build] ninja: build stopped: subcommand failed.
[proc] The command: /usr/bin/cmake --build /home/chrisk/Projects/RaspberryPi/WS/build --config Debug --target all -- exited with code: 1
[driver] Build completed: 00:00:01.069
[build] Build finished with exit code 1
因此,链接器认为 sht4x_min_delay 已被定义多次。可以看出,第一次定义是在上面的第166行。它懒得告诉我它还在哪里被定义。
我尝试更改变量名称 sht4x_min_delays,然后它说该名称有多个定义。我已经对项目中的所有文件进行了 grep 查找,这是唯一找到 sht4x_min_delays 的地方,除了我注释掉的那些。
我已经完成 make clean,然后重新构建,但仍然遇到相同的错误。 sht4x.h 文件有适当的保护以确保它不会被包含两次: '''
#ifndef SRC_LIB_DEVICES_I2C_INCLUDE_SHT4X_H_
#define SRC_LIB_DEVICES_I2C_INCLUDE_SHT4X_H_
<bunch o stuff>
#endif
''' 所以,我很困惑为什么链接器认为有多个定义。 任何想法都会有帮助
我只是在定义前面添加了一个 const,这就是我想要的,因为什么都不会改变,现在它可以编译了。
const map<Sht4xMaxTimings, int> sht4x_min_delays = {
{SHT4X_TIMING_SOFT_RESET,1000}, /* tpu 1 millisecond */
/*
* For the next 3 the table says including tpu. I take that
* to mean I should add the value above, tpu, to these values
* in the table. I may have that wrong though.
*/
{SHT4X_TIMING_MEASUREMENT_LOW_REPEATABILITY, 1600 + 1000}, /* 1.6 millisecond + tpu = 2.6 milliseconds*/
{SHT4X_TIMING_MEASUREMENT_MED_REPEATABILITY, 500 + 1000}, /* 4.5 milliseconds + tpu = 5.5 milliseconds */
{SHT4X_TIMING_MEASUREMENT_HIGH_REPEATABILITY, 8300 + 1000}, /* 8.3 milliseconds + tpu = 9.3 milliseconds */
{SHT4X_TIMING_HEATER_DURATION_LONG, 1100000}, /* 1.1 seconds */
{SHT4X_TIMING_HEATER_DURATION_SHORT, 110000} /* 0.11 seconds */
};
现在编译没有问题了:
[main] Building folder: /home/chrisk/Projects/RaspberryPi/WS/build
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/chrisk/Projects/RaspberryPi/WS/build --config Debug --target all --
[build] [2/4 25% :: 1.081] Building CXX object src/CMakeFiles/WeatherStation.dir/main.cpp.o
[build] [2/4 50% :: 1.180] Building CXX object src/lib/devices/i2c/CMakeFiles/i2cdevices.dir/sht4x.cpp.o
[build] [3/4 75% :: 1.245] Linking CXX static library src/lib/devices/i2c/libi2cdevices.a
[build] [4/4 100% :: 1.329] Linking CXX executable src/WeatherStation
[driver] Build completed: 00:00:01.369
[build] Build finished with exit code 0
我不能说我真的确定为什么将其标记为常量会改变事情,但因为我希望它成为常量,所以它对我有用。