I am writing some code for a C2000 microcontroller, and I am trying to time certain segments of my code using time.h. However, when i enter this code:

clock_t begin = clock();

It sends back a bunch of errors. Including one that says: error: function call is not allowed in a constant expression. But I don't believe it is a constant expression.

Does anyone have an idea what might be going on here?

Further errors include:

--output_all_syms --cdebug_asm_data --preproc_with_compile -- preproc_dependency="Example_2802xAdcTempSensor.pp" "../Example_2802xAdcTempSensor.c" "../Example_2802xAdcTempSensor.c", line 155: error: expected a "}" clock_t start = clock();

"../Example_2802xAdcTempSensor.c", line 155: error: function call is not allowed in a constant expression clock_t start = clock(); ^

Thanks in advance.

有帮助吗?

解决方案 2

It turns out that the definition just had to be moved outside of the main loop. Once I did this it worked fine.

其他提示

Split it into two lines:

clock_t begin;
begin = clock();

The compiler wants the initialization to be a constant expression (per the error message) - which a function call is not.

By splitting it like this you are saying "when the program is running and gets to this point, that is the time I want to evaluate this function". And all will be well.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top