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

"initializer is not a constant" error in Visual Studio. #34

Closed
cuinjune opened this issue Jan 20, 2018 · 7 comments
Closed

"initializer is not a constant" error in Visual Studio. #34

cuinjune opened this issue Jan 20, 2018 · 7 comments

Comments

@cuinjune
Copy link

cuinjune commented Jan 20, 2018

Hi, I tried to build tinyexpr using Visual Studio on Windows 10 but an error occurs saying "initializer is not a constant".
screen shot 2018-01-21 at 4 04 27 am
If I remove all elements in functions[] array, the error no longer occurred.
Could someone please teach me how to fix this error?
Thank you so much in advance!

@codeplea
Copy link
Owner

I don't have VS installed right now, so this is only a wild guess, but what would happen if you replace TE_FUNCTION1 | TE_FLAG_PURE with only TE_FUNCTION1? Please try that and let me know.

@EvilPudding
Copy link
Contributor

It would be pretty sad if windows C compiler cannot solve that into a constant expression... It could also be that we are not feeding a size to that array. If all else fails, we might have to have ifdefs specific to that platform, and possibly add a constructor attribute to an initialization function... in gcc this would be easy, although I'm not aware of how it would work on Visual's C compiler.

@cuinjune
Copy link
Author

cuinjune commented Jan 20, 2018

@codeplea Hi, I actually modified the original code a bit.
if you see my screenshot, I added 'f's to the end of the functions. (e.g acos -> acosf)
Removing all these 'f's almost fixed the problem.
Now only two functions generate this error that are "ceil" and "floor".
These functions generate the error even after removing 'f's from the function.
Any idea why this happens and how can I use "ceil" and "floor" without the error?
Thank you so much.

@cuinjune
Copy link
Author

@codeplea I tried changing "ceil" and "floor" to "ceill" and "floorl" and the error no longer occurred.
This is pretty strange.

@cuinjune
Copy link
Author

cuinjune commented Jan 24, 2018

I could totally fix this error by creating separate static functions like how fac(), ncr() and npr() are currently implemented.

For example for ceil(), I created this function.
static double func_ceil(double a) {return ceil(a);}

And then call it when initializing the te_variable functions[]array.
{"ceil", func_ceil, TE_FUNCTION1 | TE_FLAG_PURE, 0},

I did this to all the elements of te_variable functions[]array.
This way, I could avoid the weird "initializer is not a constant" error in Visual Studio.
Maybe you can apply this change to your code? (if you think it's necessary)
Thanks anyway!

@codeplea
Copy link
Owner

@cuinjune What version of Visual Studio are you using?

Here is a discussion where someone else is having the same problem almost 10 years ago. There are a few workaround ideas there too.

@cuinjune
Copy link
Author

cuinjune commented Jan 24, 2018

@codeplea Hey I just tried your link and I could fix the issue by simply adding the below two lines anywhere above static const te_variable functions[] = {}

#pragma function (ceil)
#pragma function (floor)

Now it works without the problem and it is a lot simpler than my above solution.
Thank you so much for your finding!

You can close this issue if you want.

bavay added a commit to bavay/tinyexpr that referenced this issue Aug 16, 2021
As I got hit with the same issue when compiling tinyexpr on Windows with QtCreator (that itself was calling Visual C++ 2017 in the background) and realized that there was a solution in the issue's thread but no fix in the source code, I've implemented it (wrapped with ifdef to prevent warnings on other platforms).
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

No branches or pull requests

3 participants