Pelles C forum

C language => Beginner questions => Topic started by: Fuzzlix on May 29, 2013, 03:51:41 PM

Title: Compiling LUA scripting language
Post by: Fuzzlix on May 29, 2013, 03:51:41 PM
Hi.
I am allmost new to pelles c. I did some simple console programs in pelles c long time ago. But i am still a beginner in c.

Now i tried to compile the newest lua sources to get a simple lua interpreter running. (using PellesC/64bit)
sources at http://www.lua.org/ftp/lua-5.2.2.tar.gz (http://www.lua.org/ftp/lua-5.2.2.tar.gz)

1st i tried to compile the lua dll:
- new project: dll64
- inserted all c-files to the project (except lua.c & luac.c)
  ok (with options/compiler/use_ms_extensions=on)
  switching options/compiler/use_ms_extensions=off results in a error:
Code: [Select]
C:\Program Files\PellesC\Include\Win\winnt.h(559): fatal error #1014: #error: "No target architecture".  Why?

so i kept options/compiler/use_ms_extensions=on and i got a compiled dll file without errors.

next step: try to make the lua.exe file:
- new project: win64 console app
- added lua.c as sourcefile
- same compiler options as for the dll:options/compiler/use_ms_extensions=on
Code: [Select]
d:\Pelles C Projects\lua522\lua.c(178): warning #2145: Assignment of 'void __fastcall function(int)' to 'void __cdecl function(int)'.
d:\Pelles C Projects\lua522\lua.c(480): warning #2203: Function 'main' can't be __fastcall, changed to __cdecl.
- switching options/compiler/use_ms_extensions=off eliminites those 2 warnings but my exe cant probably load the dll ?

And i got many unresolved external symbols defined in the dll. How to link the dll-infos into my exe-project?

i tried to compile the lua-interpreter statically into my exe. I got errors too. I dont know it is possible without hacking the orginal lua source files)

Thanks a lot for your help.

Title: Re: Compiling LUA scripting language
Post by: TimoVJL on May 29, 2013, 05:33:23 PM
For dll remember to define LUA_BUILD_AS_DLL for exports.
Title: Re: Compiling LUA scripting language
Post by: Fuzzlix on May 29, 2013, 05:39:44 PM
Thanks a LOT!

My exe is compiled and running  :)

Fuzzlix.
Title: Re: Compiling LUA scripting language
Post by: Bitbeisser on May 30, 2013, 01:33:48 AM
Thanks a LOT!

My exe is compiled and running  :)

Fuzzlix.
Would you mind making the resulting project files available?

Ralf
Title: Re: Compiling LUA scripting language
Post by: Fuzzlix on May 30, 2013, 02:17:27 AM
Would you mind making the resulting project files available?
Shure!

Ok, i am using PellesC 7.0 /64bit.
- copy all files from http://www.lua.org/ftp/lua-5.2.2.tar.gz (http://www.lua.org/ftp/lua-5.2.2.tar.gz) into a new folder.
- copy my 3 project files into the same folder.
- build lua522dll (dll-lib) or lua522lib (static lib)
- build lua

you are done. :)
Title: Re: Compiling LUA scripting language
Post by: Fuzzlix on May 30, 2013, 02:29:48 AM
One more Question:
The orginal lua.exe has a nice blue icon. Is it possible to attach a custom icon to my console app using PellesC ?
Title: Re: Compiling LUA scripting language
Post by: TimoVJL on May 30, 2013, 05:39:15 AM
One more Question:
The orginal lua.exe has a nice blue icon. Is it possible to attach a custom icon to my console app using PellesC ?
with resource file.
Title: Re: Compiling LUA scripting language
Post by: Fuzzlix on May 30, 2013, 07:26:59 AM
with resource file.
Thank you. It is working :)

Fuzzlix.
Title: Re: Compiling LUA scripting language
Post by: frankie on May 30, 2013, 10:04:50 AM
Works only for 64 bits because the calling convention is unique (__fastcall).
For 32 bits you have to hack header files adding __stdcall to all functions (you have to modify also some source files as lua.c and luac.c adding calling convention for some functions locally).
Optionally you can force calling convention for dll project to __cdecl (EDITED) so you don't have to change files, but there will be problems if you try to link the dll against something else than a console application.
Title: Re: Compiling LUA scripting language
Post by: Fuzzlix on May 30, 2013, 10:39:02 AM
You are right.

I createt new project files from scratch for win32. No changes nessesary in the c source files. I attached all 6 project files AND the res files (by timovjl).
Title: Re: Compiling LUA scripting language
Post by: czerny on May 30, 2013, 05:09:21 PM
I createt new project ...
The export library and the static library have same name.
Title: Re: Compiling LUA scripting language
Post by: Fuzzlix on May 30, 2013, 05:20:47 PM
The export library and the static library have same name.
True!
But this sould not be a issue. For instance i want to build lua.exe in w32 and w64 version:
- i build lua522lib_32
- i build luaexe_32
- i copy lua.exe to somwhere ( it is not named lua32.exe or somewhat else)
- i build lua522lib_64
- i build luaexe_64
- i copy lua.exe to somwhere ( it is not named lua64.exe or somewhat else)

Within 3 minutes you have built both static linked versions of lua.exe

The dll projects shows that is possible to built the dlls. The orginal dll names are different to my dll names anyway. So you may want to change them.

Fuzzlix.

EDIT:
Ok, if you want to keep all libs and exe in the project tree, we need to define a directory structure for the files made. So what is your sugestion?
Title: Re: Compiling LUA scripting language
Post by: czerny on May 30, 2013, 06:50:44 PM
If anyone wants to compile the whole lua archive (btw. luac is missing) she or he should include all the project files in a workspace (ppw)
and if you build the workspace, the first lib overwrites the second.

I (and most other users I suppose) have their private lib and include directories where the produced files and header files should be copied. You can do this automatically in the build process. They should be renamed in this stage at the latest.

Reading the documentation it seems to me, that the static be named liblua52.lib and the dll lua52.dll.


Title: Re: Compiling LUA scripting language
Post by: Fuzzlix on May 30, 2013, 07:02:38 PM
Ok. i see. Give me some time ... :)
Title: Re: Compiling LUA scripting language
Post by: Fuzzlix on May 30, 2013, 09:11:41 PM
Wow, this was a good trainig!  :)

I redid all the projects using different folders for temp files and output files.
I placed the workspace files in the project root and the project files in the .\src folder.
Now you have 2 workspaces - one for w32 and one for w64. Putting w32 and w64 into one workspace didnt look nice because you see all project names twice.

Please unzip the attached zip file into the project root folder.

I hope it looks better now. :)

Fuzzlix.
Title: Re: Compiling LUA scripting language
Post by: czerny on June 01, 2013, 11:25:36 PM
I hope it looks better now. :)
Yes!  :)
Title: Re: Compiling LUA scripting language
Post by: q1q2q3q4 on October 03, 2014, 05:01:50 PM
I need to build lua static library and then use it in another programming language (PureBasic).

This is my PureBasic source code for checking if lua library works:
Code: [Select]
ImportC "liblua52.lib"
  luaL_newstate()
EndImport

L=luaL_newstate()

But when i compile and run this source, i get error:
Code: [Select]
POLINK: error: Unresolved external symbol '_errno'.
POLINK: error: Unresolved external symbol '___stdin'.
POLINK: error: Unresolved external symbol '___stderr'.
POLINK: error: Unresolved external symbol '___stod'.
POLINK: error: Unresolved external symbol '___locale'.
POLINK: fatal error: 5 unresolved external(s).

I used dev c++ in the past to compile lua static library and that worked but now with PellesC i get this error. Can anyone help me or give any tips how to make it work?

Thanks
Title: Re: Compiling LUA scripting language
Post by: Bitbeisser on October 04, 2014, 12:06:07 AM
Zip the project and post the ZIP file here, so we can see what your settings are...

Ralf
Title: Re: Compiling LUA scripting language
Post by: q1q2q3q4 on October 04, 2014, 04:27:06 PM
Zip the project and post the ZIP file here, so we can see what your settings are...

Ralf

I used project file attached in this thread by user Fuzzlix:
http://forum.pellesc.de/index.php?topic=5365.msg20665#msg20665
And lua sources:
http://www.lua.org/ftp/lua-5.2.3.tar.gz

I am trying to build 32bit static library to use it in PureBasic 5.30.

Thanks in advance
Title: Re: Compiling LUA scripting language
Post by: Fuzzlix on October 05, 2014, 08:10:21 AM

I used project file attached in this thread by user Fuzzlix:
http://forum.pellesc.de/index.php?topic=5365.msg20665#msg20665
And lua sources:
http://www.lua.org/ftp/lua-5.2.3.tar.gz

I am trying to build 32bit static library to use it in PureBasic 5.30.

Thanks in advance

I am very sorry but building a bug free lua using PellesC is not possible caused by a unfixed bug:
For instance:  "local a = 19267 + 22387" does not result in 41654 but in 41654.000000001 (or something similar) You can print this result using "print(a)" and you get "41654" but using this computation result in a for loop leeds to unwantet behaviour.
This bug is well known many years now but still unfixed. So there was only one way for me to get a working lua: Step away from  PellesC. I am very sorry about that.

Fuzzlix.
Title: Re: Compiling LUA scripting language
Post by: jj2007 on October 05, 2014, 11:44:01 AM
I am very sorry but building a bug free lua using PellesC is not possible caused by a unfixed bug:
For instance:  "local a = 19267 + 22387" does not result in 41654 but in 41654.000000001 (or something similar) You can print this result using "print(a)" and you get "41654" but using this computation result in a for loop leeds to unwantet behaviour.

Hi Fuzzlix,

adding two floats will never result in an exact result - this is the nature of float numbers, not a bug. Use integers for loops.
Title: Re: Compiling LUA scripting language
Post by: frankie on October 05, 2014, 04:10:05 PM
Yes JJ the floating number are never correct because some of them cannot be represented exactly in binary radix (they generate irrational number or irridicible fractions), but unfortunately lua has only double precision floats  :(.
But this problem is well known in lua programming. Infact there are a lot of raccomandations to not use equal operator (see (http://lua-users.org/wiki/NumbersTutorial)), that is true also in 'C' when dealing with floats. The reason is that the language can be used in a lot of machines and compilers so you cannot expect that the floating point behavior is always the same.
To correctly program numeric comparisons in lua is to prefer 'less than' or 'greater then' instead of 'equal'.
A special case is when you expect always an integer number (the fractional part is expected to be 0) in that case you can use one of the two math functions ceil or floor.
Fuzzlix you made a very good job creating the workspace to compile lua (it works like a charm  :D), may I suggest to adjust project dependencies (the two executables depending on libraries) so by simply clicking 'project->rebuild workspace' the whole project compiles correctly?
Title: Re: Compiling LUA scripting language
Post by: Fuzzlix on October 05, 2014, 07:12:27 PM
You may want to modify my project files for your personal needs. Thats ok.

But still this strange behaviour of Numbers in PellesC makes it impossible to compile a working lua interpreter. It works using MSC. It works using GCC. And i found no solution. There need something beeing fixed in the C runtime.

Fuzzlix.
Title: Re: Compiling LUA scripting language
Post by: frankie on October 06, 2014, 03:00:24 PM
You may want to modify my project files for your personal needs. Thats ok.
It was a suggestion to you to fix a problem, i.e. if you try to buld the whole workspace, without project dependencies, you may experience errors due to missing libs. Defining dependencies you can compile from scratch in correct order (first libraries than executables) with a single click (project->rebuild workspace).

But still this strange behaviour of Numbers in PellesC makes it impossible to compile a working lua interpreter. It works using MSC. It works using GCC. And i found no solution. There need something beeing fixed in the C runtime.
It is not a 'strange behaviour', but a rounding problem. For a compiler that works as you expected (GCC use the MSVCRT runtime so acts as MSVC) there are many more that could behave differently: like or even worst than PellesC. That's why there are special raccomandations about numbers compare in lua.
Anyway you can compile the lua libraries with msvcrt so using the same runtime the problem will disappear  ;)
Title: Re: Compiling LUA scripting language
Post by: czerny on October 06, 2014, 10:05:15 PM
I am very sorry but building a bug free lua using PellesC is not possible caused by a unfixed bug:
For instance:  "local a = 19267 + 22387" does not result in 41654 but in 41654.000000001 (or something similar)
I can not reconstruct this behavior!
The following code prints 41654.000000000000000 as expected:
Code: [Select]
#include <stdio.h>

int main(int argc, char **argv)
{
printf("%.15f\n", 19267.0 + 22387.0);
double a=19267.0;
double b=22387.0;
printf("%.15f\n", a+b);
double c = a + b;
printf("%.15f\n", c);
return 0;
}