Page 1 of 1

C backend #include

Posted: Fri Jun 03, 2022 3:40 pm
by jack
this may already be common knowledge to everyone but me, today I was experimenting in including C includes, all that you need for PB to find your include-file is to make the folders PureBasic\x86_64-w64-mingw32\include after that any include file inside the include folder can be included without full path

Re: C backend #include

Posted: Fri Jun 03, 2022 5:45 pm
by skywalk
This is not clear to me?
I thought idle and others were working on environment settings to allow direct includes of C libs.
This is the goal eventually, but I did not think Fred supported it yet.

Re: C backend #include

Posted: Fri Jun 03, 2022 7:06 pm
by jack
hello skywalk
the only use that I see for this is if you want to include pure C code into PB, by pure C I mean that the C code doesn't need any libraries, unless you know of a simple way to trick PB to link against the needed lib
here's a very simple example
fac.h
place this file in PureBasic\x86_64-w64-mingw32\include

Code: Select all

typedef unsigned long long ulong;

ulong fact(ulong n) {
  if(n>=1)
    return n*fact(n-1);
  else
    return 1;
}
test.pb

Code: Select all

; printf under PB_C
CompilerIf #PB_Compiler_ExecutableFormat<>#PB_Compiler_Console 
  CompilerError "Exec-Format must be Console in Compiler-Option"
CompilerEndIf  

CompilerIf #PB_Compiler_Backend<>#PB_Backend_C
  CompilerError "Compiler_Backend must be C in Compiler-Option"
CompilerEndIf

OpenConsole()
! #include <fac.h>
! ulong number;
! printf("Enter a positive integer: ");
! scanf("%d",&number);
! printf("Factorial of %llu = %llu\n", number, fact(number));
Input()
CloseConsole()

Re: C backend #include

Posted: Fri Jun 03, 2022 7:20 pm
by skywalk
Ok,
My goal is to include my custom built sqlite3.lib and others.
Including independent C code is nice, but we can already do it within pb files using "! C code here;".
Why can't you prepare your pure C code with some '!''s and label them mypurec.pb and include the normal way?
This way, you can manage the changes with a git or fossil SCM for your project.
Including files in an external path is a pain.
I prefer all project files within subdirectories from the top level myapp.
myapp\
myapp\lib
myapp\img
myapp\doc
etc.
Then source control manages changes to any of the files.

Re: C backend #include

Posted: Fri Jun 03, 2022 7:38 pm
by jack
my goal was to include almost any C header from the mingw distribution, there are some problems with those headers, PB doesn't like inlined so some editing of them would be needed

Re: C backend #include

Posted: Fri Jun 03, 2022 9:09 pm
by idle
The problem is when a c header includes a system file like <bits.h> otherwise it should be ok. If you include a c header with inline c it gets added into your main function. So currently the command line tool is the best option at the moment if you want to access and use c libs inline.

See tool in 1st post of thread
https://www.purebasic.fr/english/viewtopic.php?t=78558