Using a freebasic dll in c++

New to FreeBASIC? Post your questions here.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Using a freebasic dll in c++

Post by dodicat »

Yea, that's fixed it.
Thanks.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Using a freebasic dll in c++

Post by BasicCoder2 »

dodicat wrote:I use the DevC++ ide.
But earlier you wrote that you didn't like it?
Something also about code completion which I think you can turn off?
Out of curiosity I downloaded the DevCPP resurrected in 2015 by Orwell.
When I first used DevCPP it was from a disc that came with a teach yourself C++ book.
As DevC++ 5.11 was being installed I saw a lot of files ended with .py which I assume are Python files.
Is this the one you are using?
I see it includes DevPak thingy which means maybe I can automate the setting up of libraries for use by the IDE.
The forum is rather inactive as is posts about using code::blocks.

Have yet to test if I can use the FB graphics with a C program.
.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Using a freebasic dll in c++

Post by BasicCoder2 »

Boromir wrote:
BasicCoder2 wrote:What is it you want to do with C that you aren't able to do with FreeBASIC?.
Nothing specifically but if I ever became more than a simple hobbyist programmer (which I can see coming) I would probably be forced into the C/C++ arena. So I'm just attempting to get a jump start on that.
If you mean a paid commercial programmer I think that would depend on what sort of programs and what language the company is using.
A few times over the years I dabbled again with C but kept coming back to FreeBasic as it was just that much easier to use.
Remember it is not the language but what the program does that counts. If it is your Eric the Viking game then all algorithms should be easy to translate to C++. My view is you have it in your head as a sort of meta program and translate down to whatever computer language you are familiar with.
.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Using a freebasic dll in c++

Post by BasicCoder2 »

dodicat wrote: If you have some spare time you could try this out.
Three files.
32 bit.
file 1 -- one.bas. to be compiled -dll to provide the library
file 2 A C++ file. It is quite easy to include the above library with Dev C++
file 3 -- a freebasic file including the same library.
Looks like this is going to take a while to figure out.
Ran and compiled one.bas to a .dll file
then saved file2 as fileTwo.cpp
Saved file3 as ball.bas
then double clicked fileTwo.cpp and Dev-C fired up.
fileTwo.cpp failed to compile with warnings and errors,

fileTwo.cpp:(.text+0x268):undefined reference to 'circlefill1(int,int,int,unsigned int)'
fileTwo.cpp:(.text+0x284):undefined reference to 'rgbcolor(int,int,int)'
and so on ...

All files are in the same folder.
.
Boromir
Posts: 463
Joined: Apr 30, 2015 19:28
Location: Oklahoma,U.S., Earth,Solar System
Contact:

Re: Using a freebasic dll in c++

Post by Boromir »

You need to link to one.dll with -lone
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Using a freebasic dll in c++

Post by dodicat »

In the DevC++ ide.
Tools
compiler options
general
then add -lone into the top box , which says (Add the following commands when calling the compiler.)

(I have a portable version of DevC++ for windows 10)
There might be some differences in our ides!
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Using a freebasic dll in c++

Post by BasicCoder2 »

C:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe cannot find -lone

I am using Windows 7 as my window10 has code::blocks and I didn't want a conflict.
Other ordinary C++ programs compile and run ok.
.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Using a freebasic dll in c++

Post by dodicat »

I get that error message if I use the 64 bit compiler.
I have a choice here:

TDM-GCC 4.8.1 32 bit release
or
TDM-GCC 4.8.1 64 bit release

(choose from the menu bar)
Boromir
Posts: 463
Joined: Apr 30, 2015 19:28
Location: Oklahoma,U.S., Earth,Solar System
Contact:

Re: Using a freebasic dll in c++

Post by Boromir »

BasicCoder2 wrote:C:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\bin\ld.exe cannot find -lone
Mr. Swiss mentioned earlier that a FreeBasic and C++ compiler must both have the same bitness to work together. If you compiled your dll in 32bit FreeBasic you have to use a 32 bit c++ compiler.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Using a freebasic dll in c++

Post by BasicCoder2 »

The compiler options is and was set to configure
TDM-GCC 4.9.2 32-bit Release

I have inserted -lone in the compiler options in the top box (with a tick in the check box) as a command when calling the compiler but it can't find it.
.

.
Last edited by BasicCoder2 on May 23, 2017 18:12, edited 1 time in total.
Boromir
Posts: 463
Joined: Apr 30, 2015 19:28
Location: Oklahoma,U.S., Earth,Solar System
Contact:

Re: Using a freebasic dll in c++

Post by Boromir »

BasicCoder2 wrote:The compiler options is and was set to configure
TDM-GCC 4.9.2 32-bit Release
.
Did you compile the dll with 32 bit FreeBasic?

Note:
In Linux I use -L"path to dll folder" to tell the linker to search that directory for the dll but I'm not sure if that's needed in Windows. You could try it and see if it helps.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Using a freebasic dll in c++

Post by BasicCoder2 »

I think I am using a 32 bit version of FB . Not sure how to check. Will a 64 version work on a 32 bit cpu?
Looking in the download folder I see ext-0-3-2-dev-release-03-10-2013-win32.zip
You talk of the linker but dodicat talks of adding -lone to the compiler commands and the compiler reports it cannot find -lone

.Ok.
Changed -lone to -Lone and it seems fine with that but now I get the old errors again.
fileTwo.cpp:(text+0x268):undefined reference to 'circlefill1(int,int,int,unsigned int)'
This is why I like FreeBASIC it just works and so does Java or Python with a simple import statement without all this filling out of forms.

By the way the FreeBasic program that is using one.dll compiles and runs ok.
The FB version just works no forms to fill out.
.
.
Boromir
Posts: 463
Joined: Apr 30, 2015 19:28
Location: Oklahoma,U.S., Earth,Solar System
Contact:

Re: Using a freebasic dll in c++

Post by Boromir »

BasicCoder2 wrote:I think I am using a 32 bit version of FB . Not sure how to check. Will a 64 version work on a 32 bit cpu?
No, 64 bit won't work on a 32 bit pc so you're using the 32 bit version.
You talk of the linker but dodicat talks of adding -lone to the compiler commands and the compiler reports it cannot find -lone
when -l is passed to the compiler it tells it that the following text is going to be a lib for the linker to link against.
.Ok.
Changed -lone to -Lone and it seems fine with that but now I get the old errors again.
fileTwo.cpp:(text+0x268):undefined reference to 'circlefill1(int,int,int,unsigned int)'
This is why I like FreeBASIC it just works and so does Java or Python with a simple import statement without all this filling out of forms.

By the way the FreeBasic program that is using one.dll compiles and runs ok.
The FB version just works no forms to fill out.
-L and -l are different
-l tells to link the program against whatever library specified.
-L takes in a directory to search for libraries.
Caseih mentioned that freebasic takes care of the linker stuff through code i.e. #inclib which is why you were able to compile it right off.

Let's say the folder C:\test\ contains one.dll.

Code: Select all

-lone -L"C:\test\"
Adding that to your Dev C++ ide compiler commands will both
-lone :: links against one.dll
-L"C:\test\" :: searches in specified directory for any libraries you are linking against, in our case it will look for one.dll
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Using a freebasic dll in c++

Post by BasicCoder2 »

-L"C:\FreeBasic\BasicToC\" ::-lone::
or just
-L"C:\FreeBasic\BasicToC\" ::
in the top box gives the same "undefined reference to" errors
.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Using a freebasic dll in c++

Post by dodicat »

Hi basiccoder2
I have found a way to load dll's into c++ via code (no need to alter anything in the dev c++ ide settings)

From scratch
Put this .bas file and the c++ file in the same folder.

graphics.bas
compile with -dll to get graphics.dll

Code: Select all

'graphics.bas --- a dll for C++ or FreeBASIC
'compile with -dll
extern  "C++"
Sub screen1 (Byval X As Integer, Byval Y As Integer,xres as integer,yres as integer,b as integer) EXPORT
  Screenres xres,yres,b
  SCREENCONTROL(100, X, Y)
End Sub

sub clearcolor(c as uinteger) export
    color ,c
    cls
    end sub

function rgbcolor(r as integer,g as integer,b as integer) as uinteger export
    return rgb(r,g,b)
end function

Sub drawstring (x as integer,y as integer, text As zstring ptr,c as uinteger) EXPORT
 draw string (x,y),*text,c
End Sub

sub printdbl(n as double,flag as integer=0) export
    select case as const flag
    case 0:print n
    case 1:print n,
    case 2:print n;
    case else
        print n
        end select
    end sub

sub line1(x1 as integer,y1 as integer,x2 as integer,y2 as integer,c as uinteger) EXPORT
    line (x1,y1)-(x2,y2),c
end sub

sub sleep1(t as integer) export
    sleep t 
end sub

sub waitkey export
    sleep
end sub

function randoms(n as integer) as double export
    return rnd*n
    end function

sub circlefill1(x1 as integer,_
    y1 as integer,_
    rad as integer,_
    c as uinteger) export
    circle (x1,y1),rad,c,,,,f
end sub

sub circle1(x1 as integer,_
    y1 as integer,_
    rad as integer,_
    c as uinteger) export
    circle (x1,y1),rad,c
end sub

sub pset1(x1 as integer,y1 as integer,c as uinteger) EXPORT
    pset (x1,y1),c
end sub

sub clearscreen1() export
    cls
end sub

sub endkey1(n as integer) export
    if inkey=chr(n) then end
end sub

sub screenlock1(dummy as integer) export
    screenlock
end sub

sub screenunlock1(dummy as integer) export
    screenunlock
end sub

sub getmouse1( mx as integer ptr, my as integer ptr, mw as integer ptr, mb as integer ptr) export
    getmouse(*mx,*my,*mw,*mb)
end sub
sub locate1(x as integer,y as integer) export
    locate x,y
end sub

Function framecounter() As Integer export
    dim as double t2=timer
    Static As Double t3,frames,answer
    frames=frames+1
    If (t2-t3)>=1 Then
        t3=t2
        answer=frames
        frames=0
    End If
    Return answer
End Function

Function regulate( MyFps As integer, fps As integer ptr) As integer export
    Static As Double timervalue,lastsleeptime,t3,frames
    frames+=1
    If (Timer-t3)>=1 Then t3=Timer:*fps=frames:frames=0
    Var sleeptime=lastsleeptime+((1/myfps)-Timer+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function

end extern
 
and the c++ file

Code: Select all



#include <windows.h>
#include <math.h>
#include<new>
 
 typedef double ( * thernd)(int);
 typedef void (* thescreen)(int,int,int,int,int);
 typedef void  (* thedrawstring)(int x,int y,char* text,unsigned int c);
 typedef void ( * theprintdbl)(double n,int flag);
 typedef void ( * thecirclefill1)(int x1,int y1,int rad,unsigned int c);
 typedef void ( * thecircle1)(int x1,int y1,int rad,unsigned int c);
 typedef void ( * theline1)(int x1,int y1,int x2,int y2,unsigned int c);
 typedef unsigned int ( * thergbcolor)(int r,int g,int b);
 typedef void ( * thescreenlock1)();
 typedef void ( * thescreenunlock1)();
 typedef void ( * thesleep1)(int n);
 typedef void ( * theclearscreen1)();
 typedef void ( * theendkey1)(int);
 typedef void ( * thelocate1)(int x,int y);
 typedef int ( * theframecounter)();
 typedef void ( * thegetmouse1)(int *mx,int *my,int *mw,int *mb);
 typedef int ( * theregulate)(int myfps,int *fps);
 typedef void ( * theclearcolor)(unsigned int c);
 typedef void ( * thepset1)(int,int,unsigned int);
 typedef void ( * thewaitkey)();
 
 HMODULE Module = LoadLibrary("graphics.dll");
 
 
     thernd  rnd = (thernd) GetProcAddress(Module, "_Z7randomsi");
     thescreen  screen = (thescreen) GetProcAddress(Module, "_Z7screen1iiiii");
     thedrawstring  drawstring = (thedrawstring) GetProcAddress(Module, "_Z10drawstringiiPcj");
     thergbcolor   rgb= (thergbcolor) GetProcAddress(Module, "_Z8rgbcoloriii");
     theprintdbl  print=  (theprintdbl) GetProcAddress(Module, "_Z8printdbldi");
     thecirclefill1 circlefill= (thecirclefill1) GetProcAddress(Module, "_Z11circlefill1iiij");
     thecircle1 circle = (thecircle1)  GetProcAddress(Module, "_Z7circle1iiij");
     theline1 line =(theline1) GetProcAddress(Module, "_Z5line1iiiij");
     thescreenlock1 screenlock=(thescreenlock1) GetProcAddress(Module, "_Z11screenlock1i");
      thescreenunlock1 screenunlock = ( thescreenunlock1) GetProcAddress(Module, "_Z13screenunlock1i");
      thesleep1 sleep = (thesleep1) GetProcAddress(Module, "_Z6sleep1i");
      theclearscreen1  clearscreen = (theclearscreen1)GetProcAddress(Module, "_Z12clearscreen1v");
      theendkey1 endkey = (theendkey1) GetProcAddress(Module, "_Z7endkey1i");
      thelocate1 locate = (thelocate1) GetProcAddress(Module, "_Z7locate1ii");
      theframecounter framerate = (theframecounter) GetProcAddress(Module, "_Z12framecounterv");
       thegetmouse1 getmouse = ( thegetmouse1)  GetProcAddress(Module, "_Z9getmouse1PiS_S_S_");
       theregulate regulate = (theregulate)  GetProcAddress(Module, "_Z8regulateiPi");
       theclearcolor clearcolor = (theclearcolor) GetProcAddress(Module, "_Z10clearcolorj");
       thepset1 putpixel = (thepset1) GetProcAddress(Module, "_Z5pset1iij");
       thewaitkey wait = (thewaitkey) GetProcAddress(Module, "_Z7waitkeyv");

  struct ball
{
	float x;
	float y;
	float dx;
	float dy;
	unsigned int col;
} ;

// globals
struct ball* b[3]={new ball,new ball,new ball};
 int fps,sleeptime;
 int mx,my,mw,mb;


// procedures
void move(struct ball* b[])
{
		for (int n=0;n<3;n++)
		{
b[n]->x+=b[n]->dx;
b[n]->y+=b[n]->dy;
}
}

void edges(struct ball* b[])
{
	int r=50;
	for (int n=0;n<3;n++)
	{

	if(b[n]->x<r) {
 		b[n]->dx=-b[n]->dx;
	 };
	 if(b[n]->x>1000-r){
 		b[n]->dx=-b[n]->dx;
	 };
	 if(b[n]->y<r){
 		b[n]->dy=-b[n]->dy;
	 };
	 if(b[n]->y>768-r){
 		b[n]->dy=-b[n]->dy;
	 };	
}

}

void draw(struct ball* b[])
{
	for (int n=0;n<3;n++)
	{	
circlefill(b[n]->x,b[n]->y,50,b[n]->col);
circle(b[n]->x,b[n]->y,50,rgb(255,255,255));

}
}


void BallCollisions(struct ball* b[])
{
float L,impulsex,impulsey,dot,impactx,impacty;           
            for(int n1=0;n1<3;n1++)
                {
                for(int n2=n1+1;n2<3;n2++)
                    {
            L=sqrt( (b[n1]->x-b[n2]->x)*(b[n1]->x-b[n2]->x) + (b[n1]->y-b[n2]->y)*(b[n1]->y-b[n2]->y));
          
            if (L<100)     // Then
                {
                 impulsex=(b[n1]->x-b[n2]->x)/L;
                 impulsey=(b[n1]->y-b[n2]->y)/L;
                //In case of overlap circles, reset to non overlap positions
                b[n1]->x=b[n2]->x+100*impulsex;
                b[n1]->y=b[n2]->y+100*impulsey;
                 impactx=b[n1]->dx-b[n2]->dx;
                 impacty=b[n1]->dy-b[n2]->dy;
                 dot=impactx*impulsex+impacty*impulsey;
                b[n1]->dx-=dot*impulsex;
                b[n1]->dy-=dot*impulsey;
                b[n2]->dx+=dot*impulsex;
                b[n2]->dy+=dot*impulsey;
                }
            }
        }
    
    }

 int main(void)
//set up balls
 {
 	b[0]->x=100;
 	b[0]->y=100;
 	b[0]->dx=5.2;
 	b[0]->dy=3.2;
 	b[0]->col=rgb(0,100,200);
 	
 	b[1]->x=600;
 	b[1]->y=200;
 	b[1]->dx=-5.2;
 	b[1]->dy=3.2;
 	b[1]->col=rgb(200,100,0);
 	
 	
 	b[2]->x=700;
 	b[2]->y=500;
 	b[2]->dx=-1.2;
 	b[2]->dy=3.2;
 	b[2]->col=rgb(0,200,0);
 	
 screen(50,20,1000,768,32);
 clearcolor(rgb(0,0,100));
 while (1)
 {
 
 	move(&b[0]);
 	edges(&b[0]);
 	BallCollisions(&b[0]);
	 
    screenlock();
 clearscreen();
 draw(&b[0]);
 
 
 drawstring(50,10," Press escape key to end",rgb (255,200,0));
 drawstring(0,55,"framerate",rgb(0,200,0));
 locate (8,11);
 print(fps,0);
 
screenunlock();
sleeptime=regulate(65,&fps);
sleep(sleeptime);
endkey(27);
}

 //delete [] b;
}
      
          
  
Note: you don't need the .dll.a file, you can delete it if you want.
Post Reply