Compiler Options in source code
Compiler Options in source code
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.
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.
Last edited by BarryG on Fri May 10, 2019 7:52 am, edited 2 times in total.
Re: Compiler Options in source code
+1
But in the meantime, there is a method to avoid this classic mistake(source: many codes in this forum)
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
; ...
Re: Compiler Options in source code
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:
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
Last edited by Josh on Fri May 10, 2019 6:27 am, edited 1 time in total.
sorry for my bad english
Re: Compiler Options in source code
Ok, I didn't realise. I've put this in instead, at the recommendations above:Josh wrote:This will not be possible because some settings will have to be specified when the compiler is started.
Code: Select all
CompilerIf #PB_Compiler_Thread=0
Debug "Enable thread-safety in Compiler Options!"
End
CompilerEndIf
It was a nightmare for those three hours!Marc56us wrote:classic mistake
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
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.BarryG wrote:It was a nightmare for those three hours!Marc56us wrote:classic mistake
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.
Three hours troubleshooting? You noob
Have spent the last two days finding a very nasty mistake
Last edited by Josh on Fri May 10, 2019 6:51 am, edited 1 time in total.
sorry for my bad english
Re: Compiler Options in source code
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:it is not necessary in all cases that the ThreadSave option must be set.
Yuck. I feel your pain.Josh wrote:Three hours troubleshooting? You noob
Have spent the last two days finding a very nasty mistake
Re: [Solved] Compiler Options in source code
Code: Select all
CompilerIf #PB_Compiler_Thread = #False
CompilerError "ThreadSave must be activated at main file"
CompilerEndIf
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
-
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Compiler Options in source code
+1
It will be possible if those compiler options are at the very beginning of the source code.Josh wrote:This will not be possible because some settings will have to be specified when the compiler is started.
Re: Compiler Options in source code
The settings are in the file as trailing comments. Better if at top in user viewable code.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
-
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Compiler Options in source code
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:The settings are in the file as trailing comments.
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.skywalk wrote:Better if at top in user viewable code.
Re: Compiler Options in source code
You cannot set a compiler constant in your code, but you CAN prevent compiling if the settings are incorrect.
Quick and dirty way:
Nicer way:
Quick and dirty way:
Code: Select all
; Build fails if Thread is #False
#PB_Compiler_Thread = #True
Code: Select all
CompilerIf (Not #PB_Compiler_Thread)
CompilerError #PB_Compiler_Filename + " requires Threadsafe mode"
CompilerEndIf
Re: Compiler Options in source code
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.Little John wrote: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:The settings are in the file as trailing comments.
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.skywalk wrote:Better if at top in user viewable 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.
sorry for my bad english
Re: Compiler Options in source code
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.
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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
-
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Compiler Options in source 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.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.
- Michael Vogel
- Addict
- Posts: 2680
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Compiler Options in source code
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:
...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
; :
; :