Understanding PureBasic compilation

Everything else that doesn't fall into one of the other PB categories.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Understanding PureBasic compilation

Post by Oso »

Would I be correct in saying that PureBasic operates on the principle of converting the source to C and then calling a C compiler? I notice that the compilers sub-folder contains GCC and there is also an SDK sub-folder which refers in the readme file to Visual C.
Last edited by Oso on Thu Dec 28, 2023 8:47 am, edited 1 time in total.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Understanding PureBasic compilation

Post by skywalk »

Transpiling to C is a new v6 option to capture more uprocessors than the original custom coded ASM compiler supported.
The included C compiler may or may not change. Still early.
The options applied during compilation is also in flux. Aggressive optimizations can break code.
Overall, it is an awesome development with smaller, faster exe's!
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Understanding PureBasic compilation

Post by Oso »

skywalk wrote: Wed Jul 20, 2022 1:52 pm Transpiling to C is a new v6 option to capture more uprocessors than the original custom coded ASM compiler supported.
The included C compiler may or may not change. Still early.
The options applied during compilation is also in flux. Aggressive optimizations can break code.
Overall, it is an awesome development with smaller, faster exe's!
Okay, thanks for the explanation, it's interesting. Switching from generating assembler output to C is a significant step. I can say that I like the small footprint and how easy it is to get up and running.

I looked at pbcompile.exe and put the compilers sub-folder into my path. Two interesting things...

1. The .pb source file saved with the development environment seems to contain some additional metadata that wasn't entered with the source.

Code: Select all

C:\test>type console2*

´╗┐OpenConsole()
PrintN("Hello, welcome to PureBasic")
Print("What is your name ?")
name.s=Input()
PrintN("")
PrintN("It's good to meet you, "+name)
PrintN("")
Print("Please press Enter to end : ")
Input()
End
; IDE Options = PureBasic 6.00 LTS (Windows - x64)
; CursorPosition = 8
; EnableXP
; DPIAware

2. I have compiler errors when I run

Code: Select all

pbcompile console2.pb
Do I need to create another source file containing only my code and not the metadata?

Code: Select all

C:\test>pbcompiler console2.pb
PureBasic 6.00 LTS (Windows - x64) Free
Compiling console2.pb
Loading external libraries...
Starting compilation...
10 lines processed.
Error: Assembler
purebasic.asm [53]:
extrn SetCurrentDirectoryA
processed: extrn SetCurrentDirectoryA
error: symbol already defined.

Incidentally, the code runs fine when I run it (or compile it) via the IDE interface...

Code: Select all

Hello, welcome to PureBasic
What is your name ?Oso

It's good to meet you, Oso

Please press Enter to end :
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Understanding PureBasic compilation

Post by Marc56us »

C:\test>type console2*

´╗┐OpenConsole()

First 3 bytes: This is the BOM (here display as text)
Because source are save as Utf-8 with BOM

and at end, in comment, this is the preferences of the file

:wink:
Last edited by Marc56us on Wed Jul 20, 2022 3:29 pm, edited 3 times in total.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Understanding PureBasic compilation

Post by Paul »

skywalk wrote: Wed Jul 20, 2022 1:52 pm Overall, it is an awesome development with smaller, faster exe's!
For me, the exe's created with the 'C' backend run slower than same exe created with the 'ASM' backend. Might only be milliseconds is some cases but still slower.
Yes the exe size may be a few KB's smaller but certainly not worth the wait. I have one app when compiled with 'C' backend is 192KB smaller than the 'ASM' version but it takes 18 seconds to compile compared to 2 seconds with the 'ASM' backend. I can only imagine how painful the wait on a slow computer.
Image Image
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Understanding PureBasic compilation

Post by mk-soft »

I have also made a comparison between ASM and C-Backend.

It is indeed the case that the ASM backend is minimally faster than the C backend.
BUT, if the compiler option 'Optimize generate code' is enabled, the C backend program is a good 20% faster.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Understanding PureBasic compilation

Post by Paul »

mk-soft wrote: Wed Jul 20, 2022 4:00 pm BUT, if the compiler option 'Optimize generate code' is enabled, the C backend program is a good 20% faster.
If that is what you are seeing then you are very lucky.
Unfortunately I'm just not seeing that here when compiling every day Windows apps used in a daily work environment.

At least the ASM choice wasn't removed so all is good :)
Image Image
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: Understanding PureBasic compilation

Post by VB6_to_PBx »

I have also made a comparison between ASM and C-Backend.
which one would be more difficult to be Decompiled or Hacked ?
or would there be no difference ?
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Understanding PureBasic compilation

Post by Oso »

Paul wrote: Wed Jul 20, 2022 4:56 pmAt least the ASM choice wasn't removed so all is good :)
How is that done though, incidentally? Is it through Compiler Options? I've just been looking for it and I see there's an option to choose the compiler, but mine only offers PureBasic 6.00 Windows x64. Perhaps that's because I've only just downloaded a trial, maybe?

To be honest I think the flexibility with this product places it substantially ahead of many others, even if there is a performance cost as you've indicated. I feel I've been screwed in the past because Microsoft moves the goalposts and trashes the investment we have in our software.

What does the future hold for PureBasic? I take it that PureBasic isn't open source, so what if the developer decides to do something else in the future? I only ask this in the nicest possible way - not to find fault with it, but just to ask the obvious question.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Understanding PureBasic compilation

Post by Oso »

Marc56us wrote: Wed Jul 20, 2022 3:22 pm First 3 bytes: This is the BOM (here display as text)
Because source are save as Utf-8 with BOM
Thanks for the explanation on the BOM. The question was what do I do to make the following work? Am I meant to strip out that BOM and the metadata, or simply create the source file using a notepad editor that doesn't insert that? What's the correct approach, given that pbcompiler rejects the source file that otherwise compiles okay through the GUI tools?

Code: Select all

pbcompiler console2.pb
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Understanding PureBasic compilation

Post by Marc56us »

Whatever. PB does not care of BOM in source code and if source is ASCII or Utf-8. This is juste for you (ie: take care of locale chars). You can even use Vi or Edline to make source :mrgreen:

Compilers: Even for free version, there is 2 (command line) compilers. C backend have 'c' as suffix of exe

The ASM version (change dir with your)
The version without Backend C in name is the ASM version
C:\Program Files\PureBasicFree\Compilers>pbcompiler.exe /version
PureBasic 6.00 LTS (Windows - x64) Free - (c) 2022 Fantaisie Software

The C version
C:\Program Files\PureBasicFree\Compilers>pbcompilerc.exe /version
PureBasic 6.00 LTS - C Backend (Windows - x64) - (c) 2022 Fantaisie Software

If you don't see C version in IDE, add-it
File > Preferences > Compiler > [...] (search pbcompilerc.exe) > Add > Ok

Very few people are able to ensure the continuity of the work of a single person. We see this for a small part of PB: the IDE is opensource and few people are able to modify it.
So for your programs, having the source of the compiler is not a guarantee of choice of solutions. In real life, when it comes to programs, the work of the analyst (pseudo-code) is often more important than that of the coder to ensure the durability of an application.

And for last question: to compile with command line tool, you need to set PUREBASIC_HOME to the root of you PB dir
PUREBASIC_HOME=C:\Program Files\PureBasic

Enjoy
:wink:
Last edited by Marc56us on Wed Jul 20, 2022 9:03 pm, edited 1 time in total.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Understanding PureBasic compilation

Post by skywalk »

Paul wrote: Wed Jul 20, 2022 4:56 pm
mk-soft wrote: Wed Jul 20, 2022 4:00 pm BUT, if the compiler option 'Optimize generate code' is enabled, the C backend program is a good 20% faster.
If that is what you are seeing then you are very lucky.
Unfortunately I'm just not seeing that here when compiling every day Windows apps used in a daily work environment.

At least the ASM choice wasn't removed so all is good :)
Yes, I was referring to C backend + optimized.
I believe the C backend can only get better.
The ASM may not get the attention you seek. It already does not support ARM uprocessors.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Understanding PureBasic compilation

Post by Oso »

Marc56us wrote: Wed Jul 20, 2022 8:48 pmAnd for last question: to compile with command line tool, you need to set PUREBASIC_HOME to the root of you PB dir
PUREBASIC_HOME=C:\Program Files\PureBasic
Thanks for the reply. Setting PUREBASIC_HOME to C:\Program Files\PureBasic made no difference to the result. Clearly there's something fundamental and very simple that I'm misunderstanding here.

I have my source code in C:\Test

I've added C:\Program Fles\PureBasic\Compilers to PATH
I open a shell and change directory to C:\Test

The result of pbcompilerc :

Code: Select all

C:\test>pbcompilerc console2.pb
PureBasic 6.00 LTS - C Backend (Windows - x64) Free
Loading 'C' subsystem
Compiling console2.pb
Loading external libraries...
Starting compilation...
10 lines processed.
Creating and launching executable.
Error: Linker
error: Unresolved external symbol '_SetCurrentDirectoryA' - referenced from 'PureBasic.obj'.
POLINK: fatal error: 1 unresolved external(s).
And pbcompiler :

Code: Select all

C:\test>pbcompiler console2.pb
PureBasic 6.00 LTS (Windows - x64) Free
Compiling console2.pb
Loading external libraries...
Starting compilation...
10 lines processed.
Error: Assembler
purebasic.asm [53]:
extrn SetCurrentDirectoryA
processed: extrn SetCurrentDirectoryA
error: symbol already defined.
Just to add further, I've continued to investigate this after posting the above and I can see that the command-line compiler isn't operating according to the documentation, because if I add /COMMENTED to the command, the whole compilation process works fine and in fact even launches the new executable. The documentation is at https://www.purebasic.com/documentation ... piler.html

It indicates that /COMMENTED "creates a commented '.asm' output file when creating an executable". It doesn't mention anything about C source, although that works too for pbcompilerc.

There's also a /EXE given in an example command, but /EXE is not explained in the list of allowed options, so there's indication what it's for,...
Examples:
CLI> pbcompiler "C:\Project\Source\DLLSource.pb" /EXE "C:\Project\project.dll" /DLL
... Anyway, it's very odd that the compilers do not compile valid source code until I add the /COMMENTED switch, although those routines compile perfectly through the GUI interface. It seems therefore that something is not quite right.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Understanding PureBasic compilation

Post by Marc56us »

[Edit 10:15]
Tested on a PC with only one PB installation (PB 6.00 FREE Win 11 x64), the compilation works well in ASM and C (without PATH neither PB_HOME)
But in both cases, you have to put /OUTPUT followed by a name

Code: Select all

C:\Test>"\Program Files\PureBasic\Compilers\pbcompiler.exe" console2.pb /output console2.exe
PureBasic 6.00 LTS (Windows - x64) Free
Compiling console2.pb
Loading external libraries...
Starting compilation...
10 lines processed.
Creating executable "console2.exe".

- Feel the ..PuRe.. Power -

C:\Test>"\Program Files\PureBasic\Compilers\pbcompilerc.exe" console2.pb /output console3.exe
PureBasic 6.00 LTS - C Backend (Windows - x64) Free
Loading 'C' subsystem
Compiling console2.pb
Loading external libraries...
Starting compilation...
10 lines processed.
Creating executable "console3.exe".

- Feel the ..PuRe.. Power -
:wink:
Last edited by Marc56us on Thu Jul 21, 2022 9:15 am, edited 1 time in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Understanding PureBasic compilation

Post by mk-soft »

Parameters wrong ...

Examples:
Compiler File Path: D:\WinAPP\PureBasic600x64\Compilers\pbcompiler.exe
Compiler Parameters: --commented -co PB_Editor_BuildCount=0 -co PB_Editor_CompileCount=0 --unicode --output "C:\Users\XXX\AppData\Local\Temp\purebasic"
ASM Code File Path:D:\WinAPP\PureBasic600x64\Compilers\purebasic.asm
Compiler File Path: D:\WinAPP\PureBasic600x64\Compilers\pbcompilerc.exe
Compiler Parameters: --commented -co PB_Editor_BuildCount=0 -co PB_Editor_CompileCount=0 --unicode --output "C:\Users\XXX\AppData\Local\Temp\purebasic"
C-Code File Path:D:\WinAPP\PureBasic600x64\Compilers\purebasic.c
Link: IDE-Tool Display C Code
Link: IDE-Tool Display ASM Code
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply