A quick hack to facilitate setting command line and importing c headers so you can utilise almost any c lib directly with inline c with the constant and macros intact.
Just include and use

Usage
do compiler flags before imports.
use an inline c comment !// and keyword gccflags followed by flags and end in ;
;if you need to add a header use inline c comment !// with #include keyword and the header and end in ;!//gccflags -fno-schedule-insns -fno-schedule-insns2 ;
Linux code:ImportC "-lasound" : EndImport
!//#include /usr/include/alsa/asoundlib.h;
compile it as gcc to purebaisc\compilers folder
Code: Select all
;fake gcc to add compiler flags and make c header includes available with inline c
;compile this as gcc into purebasic compilers folder
;when you compile programs from a terminal it should catch the output
;when you compile from the ide it should remain unaffected
;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 are available
;tested on manjaro x64 and raspberry pi
;add this to the end of your .bashrc or /etc/rc.local or addit to the launch.sh if you want it from ide
;export PUREBASIC_HOME=/home/andrew/purebasic
;export PATH=$PUREBASIC_HOME/compilers:$PATH
EnableExplicit
OpenConsole()
Global Flags.s,Fn,a,Command.s,Param.s,ParamCount,Find.s,CompilerHome.s,Pos,Pos1
Global Output.s,Gcc,Len,tCommand.s,error.s
If ExamineEnvironmentVariables()
CompilerHome = GetEnvironmentVariable("PUREBASIC_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)
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
Until Eof(Fn)
CloseFile(Fn)
EndIf
EndIf
; SetClipboardText(command)
; MessageRequester("fake gcc",command)
Gcc = RunProgram("/usr/bin/gcc",Command,GetCurrentDirectory(),#PB_Program_Open)
If WaitProgram(gcc)
End ProgramExitCode(gcc)
EndIf
EndIf
EndIf
EndIf
you need to have PUREBASIC_HOME set and PATH needs to have compilers set as the 1st entry so the fake gcc gets called before the real gcc (which may need changing if it's not /usr/bin/gcc)
Add this to the end of your .bashrc or launch.sh if you want it available form the ide
For windowsexport PUREBASIC_HOME=/home/andrew/purebasic
export PATH=$PUREBASIC_HOME/compilers:$PATH
Code: Select all
;fake gcc to add compiler flags and make c header includes available with inline c
;windows 11, windows xp
;in the pbcompilers folder rename gcc to gccreal and compile this as gcc.exe
;Useage
;you can specify additional compiler flags on the command line as below
;use an inline c comment !// and keyword gccflags followed by the 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 are availableto use from the header in c
;if you want to compile with clang rather than gcc
; !//UseClang";
;
; mingw64-clang13 build can be downloaded here
; https://github.com/mstorsjo/llvm-mingw/releases/download/20211002/llvm-mingw-20211002-msvcrt-x86_64.zip
EnableExplicit
OpenConsole()
Global Flags.s,Fn,a,Command.s,Param.s,ParamCount,Find.s,CompilerHome.s,Pos,Pos1
Global Output.s,Gcc,Len,tCommand.s,error.s,usellvm
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)
usellvm = FindString(Flags,"//UseClang",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
Until Eof(Fn)
CloseFile(Fn)
EndIf
EndIf
MessageRequester("fake gcc",command + " " + GetCurrentDirectory())
If usellvm
Gcc = RunProgram("C:\llvm-mingw-20211002-msvcrt-x86_64\bin\clang.exe",command,GetCurrentDirectory(),#PB_Program_Open);
Else
Gcc = RunProgram(CompilerHome + "Compilers\gccreal.exe",command,GetCurrentDirectory(),#PB_Program_Open);
EndIf
If WaitProgram(Gcc)
End ProgramExitCode(Gcc)
EndIf
EndIf
EndIf
EndIf
examples
Code: Select all
;Alsa example for joris
ImportC "-lasound" : EndImport
!//#include /usr/include/alsa/asoundlib.h;
Procedure Send_to_midi_port()
Protected *midiinp, *midiout, status.l
Protected *name = UTF8("hw:1,0,0")
Protected mode.l = 0
Protected noteon.l = RGB(90, 60, 100) ; 90 is note on in the alsa headers
Protected noteof.l = RGB(90, 60, 0) ;
!v_mode = SND_RAWMIDI_SYNC;
!v_status=snd_rawmidi_open(0, &p_midiout, p_name, v_mode);
If status=0
!snd_rawmidi_write(p_midiout, v_noteon, 3);
Delay(25)
!snd_rawmidi_write(p_midiout, v_noteof, 3);
!snd_rawmidi_drain(p_midiout);
!snd_rawmidi_close(p_midiout);
Else
MessageRequester("No Card.", "Problem opening MIDI output: " + PeekS(*name,-1,#PB_UTF8), #PB_MessageRequester_Ok)
EndIf
FreeMemory(*name)
EndProcedure
Send_to_midi_port()
Code: Select all
CompilerIf Not Defined(PB_Compiler_Backend,#PB_Constant)
CompilerError "Please use PureBasic Version 6.00 Beta1"
CompilerElseIf #PB_Compiler_Backend<>#PB_Backend_C
CompilerError "Please use Compiler-Option C Backend."
CompilerEndIf
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
ImportC "-llua5.2" : EndImport
!//#include /usr/include/lua5.2/lua.h
CompilerElseIf #PB_Compiler_OS = #PB_OS_Windows
CompilerError "Please add windows imports"
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
CompilerError "Please add osx imports"
CompilerEndIf
Global lua
!v_lua = luaL_newstate();
!luaL_openlibs(v_lua);
*code = UTF8("print('Hello, World from Lua')")
!if (luaL_loadstring(v_lua, p_code) == LUA_OK) {
! if (lua_pcall(v_lua, 0, 0, 0) == LUA_OK) {
! lua_pop(v_lua, lua_gettop(v_lua));
! }
! }
FreeMemory(*code)
!lua_close(v_lua);
test of optimized asm output
Code: Select all
;pbcompiler -z testasm.pb
!//gccflags -fno-schedule-insns -fno-schedule-insns2;
; With optimizer optimize ?Code_Start and ?Code_End away.
DisableDebugger
Code_Start:
a = (Random(100) * 5) + 2000
Code_End:
Text$ ="?Code_Start:"+Hex(?Code_Start)+ Chr(13)+"?Code_End:"+Hex(?Code_End)+ Chr(13)
Text$ + "Disassemblierter Code:" + Chr(13)
If ExamineAssembly(?Code_Start, ?Code_End)
While NextInstruction()
Text$ + RSet(Hex(InstructionAddress()), SizeOf(Integer)*2, "0")
Text$ + " " + InstructionString() + Chr(13)
Wend
EndIf
SetClipboardText(Text$)
MessageRequester("", Text$)
CompilerIf Not Defined(PB_Compiler_Backend,#PB_Constant)
CompilerError "Please use PB Version>=6.00"
CompilerElseIf #PB_Compiler_Backend<>#PB_Backend_C
CompilerWarning "C Backend test."
CompilerEndIf
;?Code_Start:402364
;?Code_End:40237D
;Disassemblierter Code:
;0000000000402364 mov edi, 0x64
;0000000000402369 call dword 0x407910
;000000000040236E lea rax, [rax+rax*4+0x7d0]
;0000000000402376 mov [rip+0x15293], rax