C backend #include

Everything else that doesn't fall into one of the other PB categories.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

C backend #include

Post 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
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: C backend #include

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: C backend #include

Post 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()
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: C backend #include

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: C backend #include

Post 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
User avatar
idle
Always Here
Always Here
Posts: 5089
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: C backend #include

Post 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
Post Reply