include c headers and modify compiler flags with inline c

Everything else that doesn't fall into one of the other PB categories.
User avatar
idle
Always Here
Always Here
Posts: 5089
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: include c headers and modify compiler flags with inline c

Post by idle »

Tried web assembly raw via llvm and with emscripten which will probably be the easiest route as it has its own std lib opengl and sdl. But it really needs a deeper look.

I've also started on the missing link to facilitate working with c libs directly with an automatic header converter. So it will only take a couple of lines of code to use and import an entire c lib. I still have to add constants and enums but otherwise you just call c functions with inline c and you really only need to know about the types and structures in pb.
Cyllceaux
Enthusiast
Enthusiast
Posts: 464
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: include c headers and modify compiler flags with inline c

Post by Cyllceaux »

I changed it a little bit for doing this:

Code: Select all

CompilerIf #PB_Compiler_Backend=#PB_Backend_C
	!//gccflags -mtune=znver3 -march=znver3 ;
	!//gccpath C:\Users\Silko Pillasch\Downloads\gcc11\bin\gcc.exe ;
	!//fakegcc
CompilerEndIf
fakegcc creates a MessageRequester to show the parameters for the gcc
gccpath override the path to the gcc

I use this gcc version: https://sourceforge.net/projects/gcc-win64/

Code: Select all

;fake gcc to add compiler flags and to make c header includes available with inline c  
;In the pb compliers folder rename gcc to realgcc and compile this as gcc.exe 
;note c errors won't be displayed after this just the exit code. 

;useage
;you can specify additional compiler flags on the command line as below 
;use an inline c comment !// and keyword gccflags followed by flags and end in ;  
;   !//gccflags -fno-schedule-insns -fno-schedule-insns2 ;

;if you need to add a header use inline c comment !// with #include keyword and header end in ;
;   !//#include /usr/include/portaudio.h ;  
; this will ensure that the macros and constants of the header are available. 

; Tested on windows xp


EnableExplicit 
OpenConsole() 

Global Flags.s,Fn,a,Command.s,Param.s,ParamCount,Find.s,CompilerHome.s,Pos,Pos1,Pos2,Pos3
Global Output.s,Gcc,Len,tCommand.s,error.s,fakegcc.b,gccpath.s

If ExamineEnvironmentVariables()
    
  CompilerHome = #PB_Compiler_Home
  
  If CompilerHome <> "" 
    ParamCount = CountProgramParameters()
    If ParamCount 
      For a = 0 To ParamCount-1 
        Param = ProgramParameter(a)
        Command + Param + " " 
      Next 
      Command + ProgramParameter(a) 
      
      If FileSize("purebasic.c")
        Fn = OpenFile(#PB_Any,"purebasic.c")  
        If Fn 
          Repeat 
            Flags.s = ReadString(Fn,#PB_UTF8)
            Pos =  FindString(Flags,"//gccflags",1) 
            Pos1 = FindString(Flags,"//#include",1) 
            Pos2 = FindString(Flags,"//fakegcc",1) 
            Pos3 = FindString(Flags,"//gccpath",1) 
            If Pos 
              tCommand.s = " " + Right(flags,Len(flags)-10) 
              pos = FindString(tCommand,";") 
              If pos   
                Command.s + " " + Trim(Left(tCommand,pos-1))
              EndIf   
            EndIf   
            If Pos1
              tCommand = " -include "  + Right(flags,Len(flags)-10) 
              pos = FindString(tCommand,";") 
               If pos   
                Command.s + " " + Trim(Left(tCommand,pos-1))
              EndIf  
            EndIf
            If Pos2
            	fakegcc=#True
            EndIf
            If Pos3
              tCommand = Right(flags,Len(flags)-10) 
              pos = FindString(tCommand,";") 
               If pos   
                gccpath= Trim(Left(tCommand,pos-1))
              EndIf 
              If gccpath<>"" And FileSize(gccpath)<=0
              	gccpath=""
              EndIf
            EndIf
         Until Eof(Fn) 
         CloseFile(Fn)   
        EndIf 
      EndIf 
      
      If fakegcc
      	If gccpath<>""
      		MessageRequester("fake gcc",command+#CRLF$+#CRLF$+gccpath) 
      	Else
      		MessageRequester("fake gcc",command) 
      	EndIf
      EndIf
      ;RunProgram("gccreal.exe",Command,GetCurrentDirectory()+"\",#PB_Program_Wait) 
      
      If gccpath<>""
      	Gcc = RunProgram(gccpath,command,GetCurrentDirectory(), #PB_Program_Open | #PB_Program_Read | #PB_Program_Error )
      Else
      	Gcc = RunProgram(CompilerHome + "Compilers\gccreal.exe",command,GetCurrentDirectory(), #PB_Program_Open | #PB_Program_Read | #PB_Program_Error )
      EndIf
      Output = ""
      If Gcc
        While ProgramRunning(Gcc)
          If AvailableProgramOutput(Gcc)
            Output + ReadProgramString(Gcc) + #CRLF$
            error + ReadProgramError(Gcc) + #CRLF$
           EndIf
        Wend
        If ProgramExitCode(Gcc) <> 0 
         error + ReadProgramError(Gcc) + #CRLF$
         MessageRequester("Error " +  GetCurrentDirectory(),error)
        EndIf   
        CloseProgram(Gcc) 
      EndIf            
    EndIf 
  EndIf 
EndIf 
User avatar
idle
Always Here
Always Here
Posts: 5089
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: include c headers and modify compiler flags with inline c

Post by idle »

Thanks I have been using clang 13 on windows and Linux for compiles with a flag !//useclang mingw64 build on windows with clang 13 and same on Linux. I will take a look in the morning thanks.
User avatar
idle
Always Here
Always Here
Posts: 5089
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: include c headers and modify compiler flags with inline c

Post by idle »

From a question in tailbite thread. I've often been irritated that PB doesn't natively make static libs but then do we really need them and the answer is no we don't really need them but we don't want dependencies..
well thinking about it I thought along the lines of why not having precompiling objects to shorten compile times for large projects with loads of includes.

consider this as a work flow. you create your lib compile it as a shared object / dll and it produces the static object

Code: Select all

CompilerIf #PB_Compiler_IsMainFile 
  
  !//precompile /tmp/foolib.o;
  
  ProcedureDLL foo_mul(a,b)
    ProcedureReturn a * b 
  EndProcedure 
  
  ProcedureDLL foo_div(a,b) 
    If (a And b)
      ProcedureReturn a / b 
    EndIf  
  EndProcedure  
  
CompilerElse 
  
  ImportC "/tmp/foolib.o"
    foo_mul(a,b)
    foo_div(a,b) 
  EndImport 
  
CompilerEndIf   

Now you can include the file in your project and it will statically link the object to your exe

Code: Select all

XIncludeFile "foolib.pbi" 
Debug foo_mul(2,3)
This would greatly reduce the compile times for large projects with lots of stand alone modules includes...
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: include c headers and modify compiler flags with inline c

Post by fsw »

!//precompile /tmp/foolib.o;
Is clang only...

BTW: I looked at your fake gcc stuff a little bit closer, and I must say it's awesome!

There is only one thing that is really unfortunate: it doesn't work on macOS.
On macOS clang is the default c compiler, and renaming the original clang file is a no-no.

Oh well...

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
idle
Always Here
Always Here
Posts: 5089
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: include c headers and modify compiler flags with inline c

Post by idle »

I haven't shared the code to do it as its still manual intervention required to edit the purebasicc file. Plus there would still be the case of needing to use an auxiliary file to gather the required libs for pbobjects.

If you follow the linux setup it might work
Is clang in your pb compilers directory? You may just need to compile from a terminal and set the path environment variable if I compile from ide linux it goes straight to gcc but if I use the terminal I've set the environment path to look in pbcompilers first. Gets the fakegcc which then compiles with clang I'm doing the same on windows but it still uses polink.
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: include c headers and modify compiler flags with inline c

Post by fsw »

On macOS there is no clang in the pb compilers directory.

So yeah, doing it manually could work...

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
idle
Always Here
Always Here
Posts: 5089
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: include c headers and modify compiler flags with inline c

Post by idle »

If you use the linux code and save that as clang in your pbcompiler directory and change the path to the real clang it might work.
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: include c headers and modify compiler flags with inline c

Post by fsw »

A strange thing is happening on macOS:
setting the PATH variable with a shell script doesn't work, but doing the same manually in a terminal works.

Somewhere I read that macOS reverts changes to the PATH variable when it is made by an executable file.
...out of security reasons.

:(

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
idle
Always Here
Always Here
Posts: 5089
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: include c headers and modify compiler flags with inline c

Post by idle »

What happens if you set the path variable with a pb console app and then launch purebasic ide from that. It should pass on the path then.

something like this

Code: Select all

Global path.s,envpath.s,newpath.s 

OpenConsole() 

path = #PB_Compiler_Home + "\compilers" 
envpath = GetEnvironmentVariable("PATH") 
newpath = path + ";" + envpath 
SetEnvironmentVariable("PATH",newpath) 

program = RunProgram(#PB_Compiler_Home + "purebasic") 
If IsProgram(program)  
  WaitProgram(program)
  End ProgramExitCode(program) 
EndIf   

User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: include c headers and modify compiler flags with inline c

Post by fsw »

Good idea Idle.

This is the macOS version:

Code: Select all

Global path.s,envpath.s,newpath.s 

OpenConsole() 

path = #PB_Compiler_Home + "\compilers" 
envpath = GetEnvironmentVariable("PATH") 
newpath = path + ";" + envpath 
SetEnvironmentVariable("PATH",newpath) 

program = RunProgram(#PB_Compiler_Home + "../MacOS/PureBasic") 
If IsProgram(program)  
  WaitProgram(program)
  End ProgramExitCode(program) 
EndIf 
Running this code from the newly started PB-IDE:

Code: Select all

  OpenConsole()
  If ExamineEnvironmentVariables()
    While NextEnvironmentVariable()
      PrintN(EnvironmentVariableName() + " = " + EnvironmentVariableValue())
    Wend
  EndIf
  PrintN("")
  PrintN("Press Enter to quit.")
  Input()
reveals that the PATH environment variable was temporarily changed and the:

Code: Select all

#PB_Compiler_Home + "\compilers"
path was added.

One step at the time...

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: include c headers and modify compiler flags with inline c

Post by fsw »

Here another tidbit.

This code:

Code: Select all

  OpenConsole()
  If ExamineEnvironmentVariables()
    While NextEnvironmentVariable()
      PrintN(EnvironmentVariableName() + " = " + EnvironmentVariableValue())
    Wend
  EndIf
  PrintN("")
  PrintN("Press Enter to quit.")
  Input()
  
needs to be compiled as:
Executable Format -> MacOS
and not Console.

If Console is chosen the TEMP variable does not include #PB_Compiler_Home + "\compilers".
It's only added if Executable Format -> MacOS.

BTW: this post only makes sense if you (at least) read the 2 posts before this one...

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
idle
Always Here
Always Here
Posts: 5089
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: include c headers and modify compiler flags with inline c

Post by idle »

that's odd. If you can get it working on Mac that would be great.
punak
User
User
Posts: 66
Joined: Tue Sep 07, 2021 12:08 pm

Re: include c headers and modify compiler flags with inline c

Post by punak »

Hi, is this method also used for older versions of pb like 5.72 or 5.73?
User avatar
idle
Always Here
Always Here
Posts: 5089
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: include c headers and modify compiler flags with inline c

Post by idle »

No it's only relevant to version 6.0
Post Reply