Running directly C code with v6.00 [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Running directly C code with v6.00 [Resolved]

Post by Kwai chang caine »

First ..........thanks a lot to the team for this great new generation of PB so waiting 8)

I have try to compile directly a simple C code, that works but PB not find the header file "stdio.h" and "stdlib.h"

Code: Select all

!//essai du code c sous gcc et windows
!#include <stdio.h>
!#include<stdlib.h>
 
!int main()
!{
!    printf("Test d'un bout de code en  C\n");
!    getchar;
!    Return 0;
! }
Where i must adding "stdio.h" and "stdlib.h" in PB folder

Have a good day 8)
Last edited by Kwai chang caine on Mon May 31, 2021 10:40 am, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Running directly C code with v6.00

Post by Kwai chang caine »

Nobody know how compile C code, like ASM code for < v6.00 ? :cry:
ImageThe happiness is a road...
Not a destination
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Running directly C code with v6.00

Post by infratec »

If you want to program in C, why you don't use a C compiler directly :wink:
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: Running directly C code with v6.00

Post by juergenkulow »

There is a way to printf("hello"); with Alpha 6.00:

Code: Select all

F:\PureBasic6Alpha\Compilers>pbcompilerc h:\pb2_21\hallo.pb /REASM /CONSOLE /LINKER f:\PureBasic6Alpha\Compilers\hallo.po

******************************************
PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
******************************************

Loading 'C' subsystem
Compiling h:\pb2_21\hallo.pb
Loading external libraries...
ReAssembling source code...
Creating and launching executable.

F:\PureBasic6Alpha\Compilers>PureBasic
Hello visible universe.
Modify purebasic.c pb_main:

Code: Select all

extern integer printf(void *,...); //<--
int pb_main(int argc, char* argv[]) {
PB_Instance = GetModuleHandleW(0);
PB_MemoryBase = HeapCreate(0,4096,0);
SYS_InitString();
printf("Hello visible universe.");  //<--
// 
// OpenConsole()
//integer rr0=PB_OpenConsole();  // <--
// Print("Hallo")
//integer rr1=PB_Print(_S1);  // <--
SYS_Quit();
}
Linker hallo.po :

Code: Select all

F:\PellesC\Lib\Win64\kernel32.lib
F:\PellesC\Lib\Win64\advapi32.lib
F:\PellesC\Lib\delayimp64.lib
F:\PellesC\Lib\crt64.lib
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Running directly C code with v6.00

Post by Keya »

infratec wrote: Fri May 28, 2021 9:41 pm If you want to program in C, why you don't use a C compiler directly :wink:
I actually find it quite incredible - being able to use Purebasic + assembly + C in the same source code. Talk about POWER and FLEXIBILITY!!!!! :)
I don't know of any other language which has this capability and flexibility. This really is awesome

And to any coders who are timid of assembly or C ... it's ok! Relax! You don't have to use it - you can still keep using 100% Purebasic syntax, as if nothing has changed :) (though you might find your apps run faster due to decades worth of C compiler optimisations!)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Running directly C code with v6.00

Post by Kwai chang caine »

infratec wrote: Fri May 28, 2021 9:41 pm If you want to program in C, why you don't use a C compiler directly :wink:
Hello INFRATEC :D
For several reason :wink:

1/ Because i love PB and C, but C is hard for me to learn, i have try several time without succes :oops:
It's the reason why, thanks to the team, i hope PB can help me to use C in part of my codes
At the begining just litlte parts, (just one or two procedure) and with the time more and more
In fact like the ASM in the <6.00, but ASM is also always too hard for me, stronger again than the C, so i never can do my dream since decennary :cry:

In first time
PB manage memory (String, Array string, etc.. all what C not can do)
And C the others things :idea:

2/Use only one configuration for coding C and PB together, furthermore USB PORTABLE
Because C is not really USB PORTABLE, you must adding path in windows environnement and often reboot :?
I love PB, because you plug usb key on a new machine, you run "pb.exe" and you can full works immediately, without installing something :shock: 8)

3/ Using easilly external DLL C or "C code" found in all the WEB (CodeProject, etc ...)
Keya wrote: being able to use Purebasic + assembly + C in the same source code. Talk about POWER and FLEXIBILITY!!!!!
Like KEYA say :wink:
After, ASM surely disappears for the same reason of processor portability FRED create a "BackEnd C" :|
But what a power coding PB and C hand in hand, nothing can stop it 8)

And surely others reasons that i have not really thinking for the moment :wink:

@JUERGENKULOW
Hello juergenkulow and thanks for your example 8)
I have not really all understand :oops:

I have create a file.txt in the PB compiler folder with this code

Code: Select all

//essai du code c sous gcc et windows
#include <stdio.h>
#include<stdlib.h>
 
int main()
{
    printf("Test d'un bout de code en  C\n");
    getchar;
    Return 0;
}
I have rename it "Hallo.po"
I have run this sentence in CMD admin windows

Code: Select all

X:\PB\v6.00A_x64\Compilers>pbcompilerc hallo.po /REASM /CONSOLE /LINKER hallo.po
And i have this answer of "Compilerc" :|
PB wrote:******************************************
PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
******************************************

Loading 'C' subsystem
Compiling hallo.po
Loading external libraries...
The file 'PureBasic.asm' isn't found or can't be opened !
ImageThe happiness is a road...
Not a destination
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: Running directly C code with v6.00

Post by juergenkulow »

Hello Kwai chang caine,

hallo.pb :

Code: Select all

; Program lines to commented out in the purebasic.c file. 
OpenConsole()
Print("hallo")

Code: Select all

F:\PureBasic6Alpha\Compilers>pbcompilerc h:\pb2_21\hallo.pb /COMMENTED /CONSOLE

******************************************
PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
******************************************

Loading 'C' subsystem
Compiling h:\pb2_21\hallo.pb
Loading external libraries...
Starting compilation...
7 lines processed.
Creating and launching executable.

- Feel the ..PuRe.. Power -

F:\PureBasic6Alpha\Compilers>dir purebasic.c
 Datenträger in Laufwerk F: ist RAMDISK
 Volumeseriennummer: 1755-A000

 Verzeichnis von F:\PureBasic6Alpha\Compilers

29.05.2021  11:40             3.268 purebasic.c
               1 Datei(en),          3.268 Bytes
               0 Verzeichnis(se),    690.012.160 Bytes frei

F:\PureBasic6Alpha\Compilers>type purebasic.c :

Code: Select all

//
// PureBasic 6.00 Alpha 1 - C Backend (Windows - x64) generated code
//
// (c) 2021 Fantaisie Software
//
// The header must remain intact for Re-Assembly
//
// Console
// :System
//
#pragma warning(disable: 4024)
//
typedef long long quad;
typedef quad integer;
typedef struct pb_array { void *a; } pb_array;
typedef struct pb_array2 { void *a; integer b[2]; } pb_array2;
typedef struct pb_array3 { void *a; integer b[3]; } pb_array3;
typedef struct pb_array4 { void *a; integer b[4]; } pb_array4;
typedef struct pb_array5 { void *a; integer b[5]; } pb_array5;
typedef struct pb_array6 { void *a; integer b[6]; } pb_array6;
typedef struct pb_array7 { void *a; integer b[7]; } pb_array7;
typedef struct pb_array8 { void *a; integer b[8]; } pb_array8;
typedef struct pb_array9 { void *a; integer b[9]; } pb_array9;
typedef struct pb_listitem { void *a; void *b; void *c;} pb_listitem;
typedef struct pb_list { void *a; pb_listitem *b; } pb_list;
typedef struct pb_mapitem { void *a; void *b; void *c;} pb_mapitem;
typedef struct pb_pbmap { pb_mapitem *a; } pb_pbmap;
typedef struct pb_map { pb_pbmap *a; } pb_map;
static integer s_s[]={0, -1};
#define M_SYSFUNCTION(a) a __stdcall
#define M_PBFUNCTION(a) a __stdcall
#define M_CDECL
typedef void TCHAR;
static integer SYS_BankerRound(double i) { return i >= 0 ? i+0.5 : i-0.5; }
integer __stdcall ExitProcess(integer);
integer __stdcall HeapCreate(integer,integer,integer);
integer __stdcall HeapDestroy(integer);
integer __stdcall GetModuleHandleW(integer);
//
integer PB_OpenConsole();
typedef integer (*pbv_Print)(void*);
extern pbv_Print PB_Print;
static char *tls;
//
//
//
//
void SYS_Quit();
extern void *PB_StringBase;
extern integer PB_StringBasePosition;
M_SYSFUNCTION(void) SYS_InitString(void);
M_SYSFUNCTION(void) SYS_FreeStrings(void);
//
M_SYSFUNCTION(void) SYS_PushStringBasePosition(void);
M_SYSFUNCTION(integer) SYS_PopStringBasePosition(void);
M_SYSFUNCTION(integer) SYS_PopStringBasePositionUpdate(void);
M_SYSFUNCTION(void *) SYS_PopStringBasePositionValue(void);
M_SYSFUNCTION(void *) SYS_PopStringBasePositionValueNoUpdate(void);
M_SYSFUNCTION(integer) SYS_GetStringBasePosition(void);
M_SYSFUNCTION(void) SYS_SetStringBasePosition(integer Position);
//
static unsigned short _S1[]={104,97,108,108,111,0};
void *PB_NullString=0;

integer ms_s[]={0,-1};
//
//
//
char PB_OpenGLSubsystem=0;
int PB_DEBUGGER_LineNumber=-1;
int PB_DEBUGGER_IncludedFiles=0;
char *PB_DEBUGGER_FileName=0;
int PB_Compiler_Unicode=1;
int PB_Compiler_Thread=0;
int PB_Compiler_Purifier=0;
int PB_Compiler_Debugger=0;
int PB_Compiler_DPIAware=0;
int PB_Compiler_XPSkins=0;
int PB_ExecutableType=1;
//
integer PB_MemoryBase=0;
integer PB_Instance=0;
int PB_ExitCode=0;
//
void PB_EndFunctions() {
}
//
void ___chkstk_ms(integer a) { return; }
int pb_main(int argc, char* argv[]) {
PB_Instance = GetModuleHandleW(0);
PB_MemoryBase = HeapCreate(0,4096,0);
SYS_InitString();
//
// OpenConsole()
integer rr0=PB_OpenConsole();
// Print("hallo")
integer rr1=PB_Print(_S1);
//
//
//
//
SYS_Quit();
}

void SYS_Quit() {
PB_EndFunctions();
HeapDestroy(PB_MemoryBase);
ExitProcess(PB_ExitCode);
}
//integer rr1=PB_Print(_S1); // <--out commented
//integer rr0=PB_OpenConsole(); // <--out commented

printf("Hello visible universe."); //<--insert line after SYS_InitString();
extern integer printf(void *,...); //<--insert line before int pb_main(int argc, char* argv[]) {
Save purebasic.c .

You need this 4 files from PellesC for the next step:
F:\PellesC\Lib\Win64\kernel32.lib
F:\PellesC\Lib\Win64\advapi32.lib
F:\PellesC\Lib\delayimp64.lib
F:\PellesC\Lib\crt64.lib

h:\pb2_21\hallo.pb is a PureBasic file.
f:\PureBasic6Alpha\Compilers\hallo.po is a linker command file with 4 filenames.

Code: Select all

F:\PureBasic6Alpha\Compilers>pbcompilerc h:\pb2_21\hallo.pb /REASM /CONSOLE /LINKER f:\PureBasic6Alpha\Compilers\hallo.po

******************************************
PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
******************************************

Loading 'C' subsystem
Compiling h:\pb2_21\hallo.pb
Loading external libraries...
ReAssembling source code...
Creating and launching executable.

F:\PureBasic6Alpha\Compilers>purebasic
Hello visible universe.
PureBasicC.exe cannot currently affect the include directory of GCC -IDirectory, so any #include will fail.
An example with "Import user32.lib MessageBoxW" would probably have been easier.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Running directly C code with v6.00

Post by Kwai chang caine »

Thanks a lot for your precious help 8)
All works, except the last step :|

When i write

Code: Select all

X:\PB\v6.00A_x64\Compilers>pbcompilerc D:\Temp\PBcompileC\hallo.pb /REASM /CONSOLE /LINKER "X:\PB\v6.00A_x64\Compilers\hallo.po"
PB answers
Compiler wrote:******************************************
PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
******************************************

Loading 'C' subsystem
Compiling D:\Temp\PBcompileC\hallo.pb
Loading external libraries...
The file 'PureBasic.asm' isn't found or can't be opened !
And when i not add the "/REASM" command, i have the "purebasic.exe"
******************************************
PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
******************************************

Loading 'C' subsystem
Compiling D:\Temp\PBcompileC\hallo.pb
Loading external libraries...
Starting compilation...
3 lines processed.
Creating and launching executable.

- Feel the ..PuRe.. Power -
but without your modification because when i write
X:\PB\v6.00A_x64\Compilers>purebasic
The answer is "hallo" and not "Hello visible universe." :cry:
KCC is really a wound :oops:
ImageThe happiness is a road...
Not a destination
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Running directly C code with v6.00

Post by davido »

@KCC,
Thank you for airing this important question.
I'm sure there are lots of users who do not want learn to code in C when they can simply use C-code in a PB program.
DE AA EB
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Running directly C code with v6.00

Post by Kwai chang caine »

Thanks DAVIDO for your kind advice :wink:
In fact, I had stupidly understood that we could simply insert C code in a PB code, as we can do for ASM with old versions, by just putting an exclamation point in front of each instruction. :idea:
I was so happy to read this great idea. 8)
Apparently FRED didn't really plan this, or maybe it just isn't possible. :cry:
ImageThe happiness is a road...
Not a destination
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Running directly C code with v6.00

Post by mk-soft »

Yes, you can insert C code into PB with "!" and it works.
If you need header files that are not defined. You have to insert them yourself.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: Running directly C code with v6.00

Post by juergenkulow »

Code: Select all

F:\PureBasic6Alpha\Compilers>pbcompiler H:\PB2_21\hallo.pb /COMMENTED /CONSOLE

******************************************
PureBasic 6.00 Alpha 1 (Windows - x64)
******************************************

Compiling H:\PB2_21\hallo.pb
Loading external libraries...
Starting compilation...
7 lines processed.
Creating And launching executable.

- Feel the ..PuRe.. Power -

F:\PureBasic6Alpha\Compilers>del PureBasic.asm

F:\PureBasic6Alpha\Compilers>pbcompilerc H:\PB2_21\hallo.pb /REASM /CONSOLE

******************************************
PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
******************************************

Loading 'C' subsystem
Compiling H:\PB2_21\hallo.pb
Loading external libraries...
The file 'PureBasic.asm' isn't found or can't be opened !
Show the Error?!

Do you also have the following error messages on your machine?

Code: Select all

F:\PureBasic6Alpha\Compilers>pbcompilerc h:\pb2_21\hallo.pb /COMMENTED /CONSOLE

******************************************
PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
******************************************

Loading 'C' subsystem
Compiling h:\pb2_21\hallo.pb
Loading external libraries...
Starting compilation...
7 lines processed.
Creating And launching executable.

- Feel the ..PuRe.. Power -

F:\PureBasic6Alpha\Compilers>pbcompilerc h:\pb2_21\hallo.pb /REASM /CONSOLE

******************************************
PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
******************************************

Loading 'C' subsystem
Compiling h:\pb2_21\hallo.pb
Loading external libraries...
ReAssembling source code...
Creating And launching executable.
Error: Linker
POLINK: error: Unresolved external symbol 'PB_OpenConsole'.
POLINK: error: Unresolved external symbol 'PB_Print'.
POLINK: fatal error: 2 unresolved external(s).

F:\PureBasic6Alpha\Compilers>

F:\PureBasic6Alpha\Compilers>Type H:\PB2_21\h.po
F:\PureBasic6Alpha\PureLibraries\Console.


F:\PureBasic6Alpha\Compilers>pbcompilerc h:\pb2_21\hallo.pb /REASM /CONSOLE /Linker H:\PB2_21\h.po

******************************************
PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
******************************************

Loading 'C' subsystem
Compiling h:\pb2_21\hallo.pb
Loading external libraries...
ReAssembling source code...
Creating And launching executable.
Error: Linker
POLINK: fatal error: Invalid machine type in object 'Console'.
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Running directly C code with v6.00

Post by firace »

@KCC
Why don't you simply try it this way (includes are not needed):

Code: Select all

! int main()
! {
!  printf("Hello world!");
!  getchar();
!  return 0;
! }
! main();
User avatar
useful
Enthusiast
Enthusiast
Posts: 367
Joined: Fri Jul 19, 2013 7:36 am

Re: Running directly C code with v6.00

Post by useful »

Code: Select all

!  printf("Hello world!\n");
!  getchar();
:D
Dawn will come inevitably.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Running directly C code with v6.00

Post by Marc56us »

The error message is the same regardless of the syntax error and there is many errors

Code: Select all

!//essai du code c sous gcc et windows

; <stdio.h> and <stdlib.h> are implicits
!//#include <stdio.h>
!//#include<stdlib.h>
 
!int main()
!{
!    printf("Test d'un bout de code en  C\n");

; getchar is a function getchar; => getchar();
!    getchar();

; C is case sensitive Return 0 => return 0
!    return 0;
! }

; in PB, main function need to be called explict
! main();

; Note:
; Compiler = PureBasic 6.00 Alpha 1 - C Backend (Windows - x64)
; Console
And it works.
and more simply, see the answers of fireace and useful

Code: Select all

!  printf("Hello world!\n");
!  getchar();
:wink:
Post Reply