Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sketch with math function fails to compile in PlatformIO #8262

Closed
6 tasks done
maxgerhardt opened this issue Aug 9, 2021 · 0 comments · Fixed by #8263
Closed
6 tasks done

Sketch with math function fails to compile in PlatformIO #8262

maxgerhardt opened this issue Aug 9, 2021 · 0 comments · Fixed by #8263

Comments

@maxgerhardt
Copy link
Contributor

maxgerhardt commented Aug 9, 2021

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: ESP-12
  • Core Version: 3.0.1
  • Development Env: Platformio
  • Operating System: Windows

Settings in IDE

  • Module: Wemos D1 mini
  • Flash Mode: dio
  • Flash Size: 4MB
  • lwip Variant: v2 Lower Memory
  • Reset Method: [ck|nodemcu]
  • Flash Frequency: [40Mhz]
  • CPU Frequency: 80Mhz
  • Upload Using: SERIAL
  • Upload Speed: 115200

Problem Description

A sketch making use of certain math functions, like ceill() and some C++ constructs fails to compile in PlatformIO, but compiles fine in the Arduino IDE.

MCVE Sketch

Per https://community.platformio.org/t/multiple-definition-of-ceill/22823.

src\main.cpp:

#include <Arduino.h>
#include <buggy_lib.h>

void setup() {
  long double res = (long double) Serial.parseFloat();
  res = ceill(res);
  Serial.print("Result: ");
  Serial.println((double) res);
  
  //make use of the global variable, otherwise we don't get that error.
  for(auto& x : RANGE_VOLTAGE) {
    Serial.println(x.first);
  }
}

void loop() {
}

with

lib\buggy_lib\buggy_lib.h:

#ifndef _BUGGY_LIB_H
#define _BUGGY_LIB_H

#include <cstdlib> // Needed for uint8_t
#include <string>
#include <unordered_map>
using std::unordered_map;
using std::string;

struct Range_Dict
{
		float value_multiplier;
		int dp_digit_position;
		string display_unit;
};

typedef unordered_map<uint8_t, Range_Dict> range_dict_map_t;

extern range_dict_map_t RANGE_VOLTAGE;

#endif /* _BUGGY_LIB_H */

lib\buggy_lib\buggy_lib.cpp:

#include "buggy_lib.h"

range_dict_map_t RANGE_VOLTAGE = {
    {0b0110000, {1e0, 4, "V"}},  //2.2000V
    {0b0110001, {1e0, 3, "V"}},  //22.000V
    {0b0110010, {1e0, 2, "V"}},  //220.00V
    {0b0110011, {1e0, 1, "V"}},  //2200.0V
    {0b0110100, {1e-3, 2,"mV"}}  //220.00mV
};

Result in PlatformIO:

c:/users/max/.platformio/packages/[email protected]/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/bin/ld.exe: C:\Users\Max\.platformio\packages\framework-arduinoespressif8266\tools\sdk\lib\libstdc++.a(math_stubs_long_double.o): in function `ceill':
/workdir/repo/gcc-gnu/libstdc++-v3/src/c++98/math_stubs_long_double.cc:76: multiple definition of `ceill'; c:/users/max/.platformio/packages/[email protected]/bin/../lib/gcc/xtensa-lx106-elf/10.3.0/../../../../xtensa-lx106-elf/lib\libm.a(lib_a-ceill.o):/workdir/repo/newlib/newlib/libm/common/ceill.c:38: first defined here
collect2.exe: error: ld returned 1 exit status

Result in the Arduino IDE when the same library is also added in a new library folder:

Linking everything together...
"C:\\Users\\Max\\AppData\\Local\\Arduino15\\packages\\esp8266\\tools\\xtensa-lx106-elf-gcc\\3.0.3-gcc10.3-9bcba0b/bin/xtensa-lx106-elf-gcc" -fno-exceptions -Wl,-Map "-Wl,C:\\Users\\Max\\AppData\\Local\\Temp\\arduino_build_985255/ceill_test.ino.map" -g -w -Werror=return-type -Os -nostdlib -Wl,--no-check-sections -u app_entry -u _printf_float -u _scanf_float -Wl,-static "-LC:\\Users\\Max\\AppData\\Local\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\3.0.1/tools/sdk/lib" "-LC:\\Users\\Max\\AppData\\Local\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\3.0.1/tools/sdk/lib/NONOSDK22x_190703" "-LC:\\Users\\Max\\AppData\\Local\\Temp\\arduino_build_985255" "-LC:\\Users\\Max\\AppData\\Local\\Arduino15\\packages\\esp8266\\hardware\\esp8266\\3.0.1/tools/sdk/libc/xtensa-lx106-elf/lib" -Tlocal.eagle.flash.ld -Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,spi_flash_read -o "C:\\Users\\Max\\AppData\\Local\\Temp\\arduino_build_985255/ceill_test.ino.elf" -Wl,--start-group "C:\\Users\\Max\\AppData\\Local\\Temp\\arduino_build_985255\\sketch\\ceill_test.ino.cpp.o" "C:\\Users\\Max\\AppData\\Local\\Temp\\arduino_build_985255\\libraries\\buggy_lib\\buggy_lib.cpp.o" "C:\\Users\\Max\\AppData\\Local\\Temp\\arduino_cache_987583\\core\\core_826f2e61193dda5f059720593f4d9b02.a" -lhal -lphy -lpp -lnet80211 -llwip2-536-feat -lwpa -lcrypto -lmain -lwps -lbearssl -lespnow -lsmartconfig -lairkiss -lwpa2 -lstdc++ -lm -lc -lgcc -Wl,--end-group "-LC:\\Users\\Max\\AppData\\Local\\Temp\\arduino_build_985255"
..
Der Sketch verwendet 268841 Bytes (25%) des Programmspeicherplatzes. Das Maximum sind 1044464 Bytes.
Globale Variablen verwenden 29144 Bytes (35%) des dynamischen Speichers, 52776 Bytes für lokale Variablen verbleiben. Das Maximum sind 81920 Bytes.

The cause of this lies within the linking order of the libraries libstdc++ and libm (libmath). The Arduino does

-lhal -lphy -lpp -lnet80211 -llwip2-536-feat -lwpa -lcrypto -lmain -lwps -lbearssl -lespnow -lsmartconfig -lairkiss -lwpa2 -lstdc++ -lm -lc -lgcc

While PlatformIO, due to the platformio-build.py doing a simple Append() to the end in regards to the libraries and not respecting the original position dicated by the platform.txt, does

-lhal -lphy -lpp -lnet80211 -lwpa -lcrypto -lmain -lwps -lbearssl -lespnow -lsmartconfig -lairkiss -lwpa2 -lm -lc -lgcc -llwip2-536-feat -lstdc++

A PR with a fix will follow soon.

Debug Messages

None relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant