Page 1 of 2
Compiler Options in source code
Posted: Fri May 10, 2019 1:29 am
by BarryG
Hi, can we please set Compiler Options in our sources as well as through Compiler Options? If in the source, it could override whatever Compiler Options are set.
Rationale: I literally just spent THREE HOURS going through my 830 KB (20,000 lines) source trying to find why my program kept crashing with memory errors. Couldn't find a reason, and only now just discovered it was simply because I forgot to turn thread safety on. This was because I pasted my source into a new PureBasic instance earlier today, and totally forgot that pasting the source will not have any Compiler Options in it. Not fun.
Thanks for considering.
Re: Compiler Options in source code
Posted: Fri May 10, 2019 6:18 am
by Marc56us
+1
But in the meantime, there is a method to avoid this classic mistake
Code: Select all
EnableExplicit
CompilerIf #PB_Compiler_Thread = #False
MessageRequester("Warning !!",
"You must enable ThreadSafe support in compiler options",
#PB_MessageRequester_Ok )
End
CompilerEndIf
; ...
(source: many codes in this forum)

Re: Compiler Options in source code
Posted: Fri May 10, 2019 6:22 am
by Josh
This will not be possible because some settings will have to be specified when the compiler is started.
Do it as you can see in many examples here in the forum. Ask for the settings at the beginning of your program and if one is not correct, issue an error message. You can do it on a simple way:
Code: Select all
CompilerIf #PB_Compiler_Thread = #False
"ThreadSave must be activated"
CompilerEndIf
Re: Compiler Options in source code
Posted: Fri May 10, 2019 6:26 am
by BarryG
Josh wrote:This will not be possible because some settings will have to be specified when the compiler is started.
Ok, I didn't realise. I've put this in instead, at the recommendations above:
Code: Select all
CompilerIf #PB_Compiler_Thread=0
Debug "Enable thread-safety in Compiler Options!"
End
CompilerEndIf
Marc56us wrote:classic mistake
It was a nightmare for those three hours!
Thought: Since the compiler knows that thread-safety is OFF at compile time (via #PB_Compiler_Thread), why can't it throw a message when it first encounters a CreateThread() command when compiling, and alert us of this fact? Good idea? Would've solved my pain and time.
Re: Compiler Options in source code
Posted: Fri May 10, 2019 6:48 am
by Josh
BarryG wrote:
Marc56us wrote:classic mistake
It was a nightmare for those three hours!
Thought: Since the compiler knows that thread-safety is OFF at compile time (via #PB_Compiler_Thread), why can't it throw a message when it first encounters a CreateThread() command when compiling, and alert us of this fact? Good idea? Would've solved my pain and time.
I'm not an expert on threads but I believe that it is not necessary in all cases that the ThreadSave option must be set. So an automatic error message would be counterproductive.
Three hours troubleshooting? You noob
Have spent the last two days finding a very nasty mistake
Re: Compiler Options in source code
Posted: Fri May 10, 2019 6:50 am
by BarryG
Josh wrote:it is not necessary in all cases that the ThreadSave option must be set.
But a warning message would've at least been useful when compiling (at the bottom of the IDE in the status area, maybe). It could be something as simple as: "FYI: Thread safety is not enabled, and you're using threads."
Josh wrote:Three hours troubleshooting? You noob
Have spent the last two days finding a very nasty mistake
Yuck. I feel your pain.
Re: [Solved] Compiler Options in source code
Posted: Fri May 10, 2019 7:38 am
by mk-soft
Code: Select all
CompilerIf #PB_Compiler_Thread = #False
CompilerError "ThreadSave must be activated at main file"
CompilerEndIf
Re: Compiler Options in source code
Posted: Fri May 10, 2019 7:40 am
by Little John
+1
Josh wrote:This will not be possible because some settings will have to be specified when the compiler is started.
It
will be possible if those compiler options are at the very beginning of the source code.
Re: Compiler Options in source code
Posted: Fri May 10, 2019 1:48 pm
by skywalk
The settings are in the file as trailing comments. Better if at top in user viewable code.
Re: Compiler Options in source code
Posted: Fri May 10, 2019 1:59 pm
by Little John
skywalk wrote:The settings are in the file as trailing comments.
Yes ... if the preferences are set accordingly. And actually that's the proof that storing the settings in the source file
does work.
skywalk wrote:Better if at top in user viewable code.
I disagree. The problem currently is, that those settings are not visible in the part of the code shown in the IDE. That means that the visible part of the source -- which is also e.g. shared here on the forum -- is
not self-documenting.
Re: Compiler Options in source code
Posted: Fri May 10, 2019 2:38 pm
by kenmo
You cannot set a compiler constant in your code, but you CAN prevent compiling if the settings are incorrect.
Quick and dirty way:
Code: Select all
; Build fails if Thread is #False
#PB_Compiler_Thread = #True
Nicer way:
Code: Select all
CompilerIf (Not #PB_Compiler_Thread)
CompilerError #PB_Compiler_Filename + " requires Threadsafe mode"
CompilerEndIf
Re: Compiler Options in source code
Posted: Fri May 10, 2019 3:06 pm
by Josh
Little John wrote:skywalk wrote:The settings are in the file as trailing comments.
Yes ... if the preferences are set accordingly. And actually that's the proof that storing the settings in the source file
does work.
skywalk wrote:Better if at top in user viewable code.
I disagree. The problem currently is, that those settings are not visible in the part of the code shown in the IDE. That means that the visible part of the source -- which is also e.g. shared here on the forum -- is
not self-documenting.
That is not right. The settings listed in the source files at the end of the code are set by the Ide and evaluated exclusively by the Ide. The Ide stores here the settings from the compiler options and when the ide starts the compiler, these settings are passed as parameters. The compiler does not care about these 'notes' in the code.
In any case, not many #PB_Compiler_... settings are suitable for a change in the source code, or a change in the source code is already implemented. Whether this would be possible in the concrete case with ThreadSave can probably only be answered by Fred.
Re: Compiler Options in source code
Posted: Fri May 10, 2019 3:22 pm
by skywalk
Yeah, I would prefer to encapsulate the make file instructions within the source code.
For source control and diff'ing purposes, it really has to be in a separate file.
The trailing comments in individual files is convenient but creates a lot of noise in source code repositories.
Re: Compiler Options in source code
Posted: Fri May 10, 2019 4:45 pm
by Little John
Josh wrote:That is not right. The settings listed in the source files at the end of the code are set by the Ide and evaluated exclusively by the Ide. The Ide stores here the settings from the compiler options and when the ide starts the compiler, these settings are passed as parameters. The compiler does not care about these 'notes' in the code.
In the context of what I wrote, it doesn't matter whether the compiler reads those settings directly, or wheter the IDE reads them and passes them to the compiler.
Re: Compiler Options in source code
Posted: Tue May 14, 2019 4:29 pm
by Michael Vogel
I would love to have a command like
SetCompilerOptions("...") no matter if it would have to be the first line of the code or whatever...
...but also a
CheckCompilerOptions("..."), which could be placed into a (generally used) include file could help a little bit - so here's a simple start:
Code: Select all
Procedure CheckCompilerOption(options.s)
Protected i
Protected quit
Protected check
Protected error.s
Protected *Char.Character
options=UCase(options)
*Char=@options
Repeat
Select *Char\c
Case ' '
Case 'T'
check=#PB_Compiler_Thread
error="Threadsafe"
Case 'U'
check=#PB_Compiler_Unicode
error="Unicode"
Case '+'
If check=#Null
Break
EndIf
Case '-'
If check
Break
EndIf
Case #Null
quit=#True
EndSelect
*Char+SizeOf(Character)
Until quit
If quit=0
If error
Debug "Set "+error+" compiler option to "+StringField("on. off.",check+1," ")
Else
Debug "Unknown compiler switch"
EndIf
EndIf
ProcedureReturn quit
EndProcedure
CheckCompilerOption("T+U-")
; Main program
; :
; :