energia / Energia

Fork of Arduino for the Texas Instruments LaunchPad's
http://energia.nu
Other
795 stars 672 forks source link

undefined reference to `log' #110

Closed VenomXNL closed 12 years ago

VenomXNL commented 12 years ago

The math.h function 'log' doesn't seem to work in Energia, is this a known problem or is it just not supported by Energia?

I created an log calculation but when i compiled i received an error: undefined reference to `log' collect2: ld returned 1 exit status

I know that in a 'normal' C environment i could solve this problem by adding -lm to the compiler command line, because of these functions are not implemented in libc.

But i could not find any way to add the -lm option to the command line. I've tried searching on the net for answers and tried several options in Energia, I've even tried to add -lm to the Energia command-line (just a guess) but no luck.

Is this an no-support issue or a bug? because the syntax-highlighting DOES respond to log, i also realize that this could be the result of the port from the Arduino software (the syntax).

Maybe it is an option to add compiler settings/command-line options to the Energia.

robertinant commented 12 years ago

final elf file is linked with -lm and both log and log10 are in the library. Below is a snip of the archiver headers and files in libm. As you can see log and log10 are both in there. That leads me to the question: What version of Energia are you running? If you are on Linux, what toolchain are you using if not using the one baked into Energia.

Can you also copy paste your Sketch?

If you do use log then not that note that the msp430 is a fixed point processor. If using a double or float to hold the result of log the floating point emulation get's pulled in which will add about 4k to the final compiled Sketch.

bash-3.2$ msp430-objdump -a libm.a | grep log
sf_log1p.o:     file format elf32-msp430
rw-r--r-- 501/20   6968 May 20 18:21 2012 sf_log1p.o
ef_log.o:     file format elf32-msp430
rw-r--r-- 501/20   6504 May 20 18:21 2012 ef_log.o
sf_logb.o:     file format elf32-msp430
rw-r--r-- 501/20   2832 May 20 18:21 2012 sf_logb.o
ef_log10.o:     file format elf32-msp430
rw-r--r-- 501/20   4128 May 20 18:21 2012 ef_log10.o
sf_ilogb.o:     file format elf32-msp430
rw-r--r-- 501/20   2664 May 20 18:21 2012 sf_ilogb.o

Robert

robertinant commented 12 years ago

If you want to avoid floating points and can live with rounding the this takes care of it:

int a = log(10);
RickKimball commented 12 years ago

I tried log() on linux and it worked ok for me. Maybe you could move this discussion to the forum for further help. You might post the code you are using there. http://www.43oh.com/forum/viewforum.php?f=38

VenomXNL commented 12 years ago

Thanks I've tried a simple int a = log(10); like Robert said and then there is no problem, but when using my own code (over 8K then the problem occurs again). Now because my own code is meant for a larger security project (like i mentioned in on of the other posts) i just can't send my own code (and it is way to complicated to only strip it to this function).

Now i have downloaded a sample code from the Internet that uses about the same calculation, and this code also produces exactly the same error:

The sample code used here is from the Arduino.cc website (http://arduino.cc/playground/ComponentLib/Thermistor2) [code] double Thermister(int RawADC) { double Temp; Temp = log(((10240000/RawADC) - 10000)); Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); Temp = Temp - 273.15; // Convert Kelvin to Celcius Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit return Temp; }

void setup() { Serial.begin(115200); }

void loop() { Serial.println(int(Thermister(analogRead(0)))); // display Fahrenheit delay(100); } [/code]

[Error] sketch_sep16b.cpp.o: In function Thermister(int)': **************build2251118110959267482.tmp/sketch_sep16b.cpp:3: undefined reference tolog' collect2: ld returned 1 exit status [/Error]

I'm actually working on Windows (several versions causing the same problem with this code (Win XP/7/8). and i'm just using the default Energia software (latest), no external editors or compilers.

If i'm doing something wrong with the code tell me ;), mistakes are there to learn from :) because the microcontrollers are still fairly new to me. I know that the reference voltage and is different from the Arduino compared to the MSP (5.0v against 3.3v). But in my own code i have created an Subroutine that calculates to current input voltage (comparing a static value etc) and this works without a problem. Also the resistor i'm using is 25K (10K in the sample), but this is not the problem.

Currently i have a separate temperature detector an a user terminal for the thermostat, i hoped that i could merge those two in one device. The original (currently running) thermostat is an DS18B20 on the PC Serial port. but i can't get the 1-Wire working on the MSP430, so i thought lets try a Thermistor. But now i got this undefined reference error. :(

I hope you could see or reproduce the problem.

Kind regards, Venom

rei-vilo commented 12 years ago

Does the same code compile right in Arduino IDE?

Wouldn't be the forum a better place for specific issues?

See http://www.43oh.com/forum/viewforum.php?f=38

VenomXNL commented 12 years ago

Yes, normally i would refer to forum's for problems when not understanding code and such, but here i do understand the code and like you asked (just tested it again with the posted code here) Arduino IDE does compile it without problems and like Robert said: it indeed resulted in a 4K addup to the compiled code because of the Fixed Point MCU but this is no problem for me (Using a 16K MCU).

But because the code is working in Arduino and is not working in Energia, It looked like Energia bug to me, because Energia does not find the reference.

And because Robert requested me to post the sketch which contained the problem i posted it here ;)

I just hope i could help you guys to sort out a bug, or you guys could tell me a workaround for Energia to achieve the same result but without the log function.

Kind regards, Venom

pbrier commented 12 years ago

Could it be that the linker script memory for the selected device is limited to 8k and dropping some functions?

In any case, I would not use doubles and log functions for a thermistor calculation in a microcontroller. Use a (integer) approximation polynome or look-up table. Many examples are available on-line.

Will investigate one-wire problem and mentioned log() bug

Verzonden van mijn Android telefoon met K-9 Mail.

VenomXNL notifications@github.com schreef:

Thanks I've tried a simple int a = log(10); like Robert said and then there is no problem, but when using my own code (over 8K then the problem occurs again). Now because my own code is meant for a larger security project (like i mentioned in on of the other posts) i just can't send my own code (and it is way to complicated to only strip it to this function).

Now i have downloaded a sample code from the Internet that uses about the same calculation, and this code also produces exactly the same error:

The sample code used here is from the Arduino.cc website (http://arduino.cc/playground/ComponentLib/Thermistor2) [code] double Thermister(int RawADC) { double Temp; Temp = log(((10240000/RawADC) - 10000)); Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); Temp = Temp - 273.15; // Convert Kelvin to Celcius Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit return Temp; }

void setup() { Serial.begin(115200); }

void loop() { Serial.println(int(Thermister(analogRead(0)))); // display Fahrenheit delay(100); } [/code]

[Error] sketch_sep16b.cpp.o: In function Thermister(int)': **build2251118110959267482.tmp/sketch_sep16b.cpp:3: undefined reference tolog' collect2: ld returned 1 exit status [/Error]

I'm actually working on Windows (several versions causing the same problem with this code (Win XP/7/8). and i'm just using the default Energia software (latest), no external editors or compilers.

If i'm doing something wrong with the code tell me ;), mistakes are there to learn from :) because the microcontrollers are still fairly new to me. I know that the reference voltage and is different from the Arduino compared to the MSP (5.0v against 3.3v). But in my own code i have created an Subroutine that calculates to current input voltage (comparing a static value etc) and this works without a problem. Also the resistor i'm using is 25K (10K in the sample), but this is not the problem.

Currently i have a separate temperature detector an a user terminal for the thermostat, i hoped that i could merge those two in one device. The original (currently running) thermostat is an DS18B20 on the PC Serial port. but i can't get the 1-Wire working on the MSP430, so i thought lets try a Thermistor. But now i got this undefined reference error. :(

I hope you could see or reproduce the problem.

Kind regards, Venom

— Reply to this email directly or view it on GitHub.

VenomXNL commented 12 years ago

Normally i would say yes (8K limit.. dropping...) but when i use the small sample code that i posted above, then there should be no problem because the output will only reach a little above 4K.

And thanks on the tip for the integer approximation polynome tip, i will try to approach it this way. The look-up table isn't an option for me unfortunately, because extreme precision is required and the input voltage could vary between 2.9v and 3.6v when the application if finished, so an look-up table would be to inaccurate this way.

Thanks in advance, Venom

robertinant commented 12 years ago

Not an Energia bug but most likely a mspgcc bug. The following code compiles just fine. var++ is to trick the compiler not to optimize out the call to log.

#include <msp430.h>
#include <math.h>

unsigned int foo = 0;
int main()
{
    double var = log(1024);
    var++;
}

Whereas this code does not:

#include <msp430.h>
#include <math.h>

unsigned int foo = 0;
int main()
{
    double var = log(foo);
    var++;
}

Not sure what's going on but log() definitely does not like a variable argument. This is one for Peter Bigot.

If you don't need double precision then replacing log() with logf() does the trick as a work around.

Will file a bug for mspgcc.

Robert

pbrier commented 12 years ago

He venom,

A piecewise (linear) approximation (if implemented corectly) is most likely not less accurate than the proposed calculation. Other physical influences will be dominant.

Verzonden van mijn Android telefoon met K-9 Mail.

VenomXNL notifications@github.com schreef:

Normally i would say yes (8K dropping...) but when i use the small sample code that i posted above, then there should me no problem because the output will only reach a little above 4K.

And thanks on the tip for the integer approximation polynome tip, i will try to approach it this way. The look-up table isn't an option for me unfortunately, because extreme precision is required and the input voltage could vary between 2.9v and 3.6v when the application if finished, so an look-up table would be to inaccurate this way.

Thanks in advance, Venom

— Reply to this email directly or view it on GitHub.

robertinant commented 12 years ago

Confirmed to be a bug. msp430 compiler treats double the same as float. All math functions that take a double are a wrapper for their float equivalents. So log() is simply a wrapper for logf() which in this case seems to be broken. The suggested work around is to use logs() as I suggested. Details as explained by master mspgcc Peter Bigot can be found here: http://sourceforge.net/mailarchive/message.php?msg_id=29818206

VenomXNL commented 12 years ago

Ahh okay then i will convert the code to an float based calculation to achieve the same result as it will be not an high priority to fix this ;)

Any way's thanks to all for the help to confirm the found bug and for providing other workarounds for my problem. I'm for sure that i can continue my work tomorrow and let you guy's know if the workaround worked ;). It's around 3:40AM now here so i will go to sleep now, but i will keep you up-to-date of the solution, thanks in advance Robert you will hear from me asap and all the others also thanks for the time and effort.

Kind Regards, Venom

Ashok0410 commented 7 years ago

here i am also getting same problem in Energia, when i am trying to compile my code i got following errors

Energia: 1.6.10E18 (Linux), Board: "CC3200-LAUNCHXL (80MHz)"

sketch/SX_01a_TX_LoRa.pde.cpp.o: In function setup': /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:52: undefined reference toSX1272::ON()' /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:57: undefined reference to SX1272::setMode(unsigned char)' /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:62: undefined reference toSX1272::setHeaderON()' /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:67: undefined reference to SX1272::setChannel(unsigned long)' /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:72: undefined reference toSX1272::setCRC_ON()' /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:77: undefined reference to SX1272::setPower(char)' /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:82: undefined reference toSX1272::setNodeAddress(unsigned char)' /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:91: undefined reference to sx1272' sketch/SX_01a_TX_LoRa.pde.cpp.o: In functionloop': /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:96: undefined reference to SX1272::sendPacketTimeout(unsigned char, char*)' /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:103: undefined reference toSX1272::sendPacketTimeout(unsigned char, char*)' /home/quantum/Energia/libraries/LLOra/arduinoLoRa/examples/SX_01a_TX_LoRa/SX_01a_TX_LoRa.pde:108: undefined reference to `sx1272' collect2: error: ld returned 1 exit status exit status 1 Error compiling for board CC3200-LAUNCHXL (80MHz).

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

please anyone help me on this thanks in advance Regards, Ashok

aggarwalvinayak commented 6 years ago

I think it has not been solved completely... I still faced this problem of /tmp/ccJ4nZFW.o: In function `main': abc.c:(.text+0x2a): undefined reference to `log2' collect2: error: ld returned 1 exit status just a simple test program and copied it from net to check how thelog function works http://p.ip.fi/OGgU this is the sample program i wrote..