Page 1 of 1

console, windows, dll - is there a way to tell which one ?

Posted: Fri Apr 17, 2009 10:06 pm
by luis
Is there a way to know at compile time which target format was selected ?

I'm doing a console and windowed application.

I have a single source to create both the exe, with some conditional compilation of course.

I defined a constant to select the exe I want to create, but it would be a lot easier if I could simply change the target in the compiler options and hit the compile button without trying to remember every time I have to change the defined constant too.

Something like

CompilerIf #PB_Compiler_Target = 0 ; console
; console code here
CompilerEndIf

CompilerIf #PB_Compiler_Target = 1 ; windows
; windows code here
CompilerEndIf

Since a compiler constant like the above seems not supported, is there some other way to know the value of that setting ?

Thank you.

Posted: Wed Apr 22, 2009 8:50 am
by lexvictory
In the PB IDE, not by default. (I think jaPBe might have this option)

So out of curiosity, I created a tool to do it. (I have not extensively tested it, so beware)

It creates a constant called #PB_Compiler_Type that is set to either "Windows", "Console" or "DLL" (this is quite easy to modify however; the name is on the 2nd line.)
Set it up as a tool (you will need two entries - one for compile/run, and another for create exe).
Set the parameters to "%COMPILEFILE" (with the Double Quotes) No other options should be needed.

Code: Select all

tempfile.s = ProgramParameter()
constant.s = "#PB_Compiler_Type = "+#DQUOTE$

CopyFile(tempfile, tempfile+".orig")
If ReadFile(0, tempfile+".orig")
  filelen = Lof(0)
  *origbuffer = AllocateMemory(filelen+2)
  fileencoding = ReadStringFormat(0)
  ReadData(0, *origbuffer, filelen-Loc(0))
  *fileptr = *origbuffer+filelen-20
  While *fileptr >= *origbuffer;so we dont reverse beyond it.
    readoptions.s = PeekS(*fileptr, -1, fileencoding)
    ;Debug readoptions
    If FindString(readoptions, "; IDE Options", 1)
      If FindString(readoptions, "; ExecutableFormat = ", 1)
        exeformat.s = LTrim(Mid(readoptions, FindString(readoptions, "; ExecutableFormat = ", 1)+Len("; ExecutableFormat = ")))
        If Left(exeformat, 10) = "Shared Dll"
          constant+"DLL"
        ElseIf Left(exeformat, 7) = "Console"
          constant+"Console"
        Else
          constant+"Windows"
        EndIf
      Else
        constant+"Windows"
      EndIf
      
      Break
    EndIf
    *fileptr - 1
  Wend
  
  CloseFile(0)
  If CreateFile(0, tempfile)
    WriteStringFormat(0, fileencoding);set the BOM correctly
    WriteStringN(0, constant+#DQUOTE$)
    WriteData(0, *origbuffer, filelen-Loc(0))
    CloseFile(0)
  EndIf
  DeleteFile(tempfile+".orig")
EndIf
This should work cross platform too, although the "Windows" value may be a bit confusing on mac and linux... (plus the values looked for in the compiler options may need to be changed.)

Posted: Wed Apr 22, 2009 11:16 am
by gnozal
luis wrote:Is there a way to know at compile time which target format was selected ?
If you use jaPBe :
Constants available at runtime :
#jaPBe_ExecuteBuild : Build number
#jaPBe_CompilerVersion : PB compiler version
#jaPBe_OnError : If #True, OnError is enabled in Project Options
#jaPBe_IsDebuggerRunning : If #True, project is compiled with debugger
#jaPBe_IsExecute : If #True, compiled with "Create Executable" (else "Compile / Run")
#jaPBe_ExecuteType : Executable type (0 : Windows, 1 : DLL, 2 : Console)
#jaPBe_Compiler_File : Compiled source path + filename (like #PB_Compiler_File)
#jaPBe_SourcePath : Compiled source path
#jaPBe_SourceFile : Compiled source filename

Posted: Wed Apr 22, 2009 2:19 pm
by luis
lexvictory wrote: So out of curiosity, I created a tool to do it. (I have not extensively tested it, so beware)
Thank you very much, it's nice :)

I would probably ask for a native support of this in the wishlists section of the forum, but anyway this is a good solution for the time being.

Posted: Wed Apr 22, 2009 2:20 pm
by luis
Thank you Gnozal, I'm using the PB ide at the moment, but it's nice to know.