Page 1 of 2

Can I use PureBasic with a static c++ library?

Posted: Mon Dec 24, 2012 6:28 am
by karmacomposer
There is a 3D engine I am using that can be scripted with either c++, c#, BlitzMax or Lua. According to the creator of the engine, it can be used with ANY language that can open static c++ libraries. Can PureBasic do that?

I'd rather program in PureBasic than BlitzMax.

Mike

Re: Can I use PureBasic with a static c++ library?

Posted: Mon Dec 24, 2012 8:05 am
by jassing
you could use

Code: Select all

Import "MyStatic.lib"
  fnFunction()
endimport

Re: Can I use PureBasic with a static c++ library?

Posted: Mon Dec 24, 2012 12:52 pm
by Thade
karmacomposer wrote:There is a 3D engine I am using that can be scripted with either c++, c#, BlitzMax or Lua. According to the creator of the engine, it can be used with ANY language that can open static c++ libraries. Can PureBasic do that?

I'd rather program in PureBasic than BlitzMax.

Mike
Hi

May I ask which 3D Engine you want to use?

Btw.: Working with PureBasic is according to my own experience much better than with any other program.
It has by far the best editor for any solution. I have used several 3D Engines with PB.

To answer your question. Yes you can.

Re: Can I use PureBasic with a static c++ library?

Posted: Mon Dec 24, 2012 3:32 pm
by luis
Depends on the library, probably no or not easily if it's a C++ library (object oriented, with name mangling, etc.).
See what is "exported" and how and you will know.

Re: Can I use PureBasic with a static c++ library?

Posted: Tue Dec 25, 2012 4:49 am
by karmacomposer
How can one person adamantly say "Yes" and the other emphatically say "No"?

The engine is Leadwerks 2.5 (www.leadwerks.com).

Josh, the creator of the engine, says it works with c++, c#, Lua and BlitzMax. I've also been told it will work
with Nuclear Basic. I assume it will work with PureBasic, but i'd love to know if it can and how to do it.

Mike

Re: Can I use PureBasic with a static c++ library?

Posted: Tue Dec 25, 2012 4:56 am
by sec
karmacomposer wrote:How can one person adamantly say "Yes" and the other emphatically say "No"?

The engine is Leadwerks 2.5 (http://www.leadwerks.com).

Josh, the creator of the engine, says it works with c++, c#, Lua and BlitzMax. I've also been told it will work
with Nuclear Basic. I assume it will work with PureBasic, but i'd love to know if it can and how to do it.

Mike
Yes, you can call function from static c++ library with Purebasic.

And i want to ask you:
How Josh say it works with C#, Lua .. you know? because he is tried with them. If you can't trying with purebasic by yourself, why you don't ask him to try with Purebasic? it is more nature way!!

Re: Can I use PureBasic with a static c++ library?

Posted: Tue Dec 25, 2012 1:04 pm
by luis
karmacomposer wrote:How can one person adamantly say "Yes" and the other emphatically say "No"?
I really hate when someone misrepresent what another person said:
luis wrote:Depends on the library, probably no or not easily if it's a C++ library (object oriented, with name mangling, etc.).
See what is "exported" and how and you will know.
So I didn't say emphatically "No". To access a function inside a C++ library and to be able to use the library as intended are two different things.
Knowing the name of the engine would have helped in giving you an answer.
If it does make available a flat C api, like the engine you now mentioned seems to do, a PB programmer should be able to use it without the need of a wrapper.

Re: Can I use PureBasic with a static c++ library?

Posted: Thu Dec 27, 2012 5:15 pm
by karmacomposer
luis wrote:
karmacomposer wrote:How can one person adamantly say "Yes" and the other emphatically say "No"?
I really hate when someone misrepresent what another person said:
luis wrote:Depends on the library, probably no or not easily if it's a C++ library (object oriented, with name mangling, etc.).
See what is "exported" and how and you will know.
So I didn't say emphatically "No". To access a function inside a C++ library and to be able to use the library as intended are two different things.
Knowing the name of the engine would have helped in giving you an answer.
If it does make available a flat C api, like the engine you now mentioned seems to do, a PB programmer should be able to use it without the need of a wrapper.
Fair enough. I apologize if I so misrepresented you.

I will try to do this myself, but I may be asking questions if I get stuck. Asking Josh will more than likely not happen since he does not code in PureBasic, but I can always ask.

Mike

Re: Can I use PureBasic with a static c++ library?

Posted: Thu Dec 27, 2012 5:22 pm
by karmacomposer
Is there an example anywhere of another static c++ library being loaded into a PureBasic project? I learn by example and ANY example would help me here.

Thanks in advance.

Mike

Re: Can I use PureBasic with a static c++ library?

Posted: Thu Dec 27, 2012 5:44 pm
by luis
Try to search for "wrapper" in the forum and look at those.
There are many, and most of them are not really "wrappers" but the same thing you need to do.
Basically you have to:

1) take the c include files, translate them to pb (constants, structures, etc)
2) make another pb file with a lot of imports, one for each c function needed from the library.

So this is more an import and structure/constants conversion job than a wrapper. A wrapper generally means you need to write a thin layer of code to bridge from pb to the target library. This is needed for example if the library is a c++ lib using objects and so on. You need to write a wrapper in c++ calling the target library and exposing flat c functions to you. I suppose this is what fred originally did with the ogre library, for example. But it seems in your case this is not needed (from what I read on wikipedia, I never downloaded the library).

In all of this the most difficult thing to do is to pay attention to the correct conversion of data types, the most boring is the conversion of constants/structures.

My suggestion is to try initially only with the initialization function, if any, and to convert from the .h file only what's needed for that one. One that one works, you know the approach is correct and you can continue.

Re: Can I use PureBasic with a static c++ library?

Posted: Thu Dec 27, 2012 5:52 pm
by karmacomposer
luis wrote:Try to search for "wrapper" in the forum and look at those.
There are many, and most of them are not really "wrappers" but the same thing you need to do.
Basically you have to:

1) take the c include files, translate them to pb (constants, structures, etc)
2) make another pb file with a lot of imports, one for each c function needed from the library.

So this is more an import and structure/constants conversion job than a wrapper. A wrapper generally means you need to write a thin layer of code to bridge from pb to the target library. This is needed for example if the library is a c++ lib using objects and so on. You need to write a wrapper in c++ calling the target library and exposing flat c functions to you. I suppose this is what fred originally did with the ogre library, for example. But it seems in your case this is not needed (from what I read on wikipedia, I never downloaded the library).

In all of this the most difficult thing to do is to pay attention to the correct conversion of data types, the most boring is the conversion of constants/structures.

My suggestion is to try initially only with the initialization function, if any, and to convert from the .h file only what's needed for that one. One that one works, you know the approach is correct and you can continue.
This can already be programmed in Lua and BlitzMax, and according to Josh, it's creator, it can be easily ported to any programming language - so, is the wrapper/conversion really necessary?

Mike

Re: Can I use PureBasic with a static c++ library?

Posted: Thu Dec 27, 2012 5:58 pm
by luis
I don't know. You are already using the lib, you said, so you should know.

Are we talking about a library in a foreign language here ?

Yes, right ?

So at the bare minimum (C flat API) you need to import the functions using the correct calling convention (ImportC probably, I don't know).

If there is a .h with supporting structures / constants you need to convert them.

If the library only expose C++ functions you need a real wrapper.

Re: Can I use PureBasic with a static c++ library?

Posted: Thu Dec 27, 2012 6:19 pm
by karmacomposer
Actually, no. Josh is American (not a foreign language to me - I live in Florida, he lives in California).

The only thing that needs to be imported is 'engine.dll'.

The rest is declaring functions.

It has c++ header files which contains all the functions - would I be able to just load that in, like an include?

Mike

Re: Can I use PureBasic with a static c++ library?

Posted: Thu Dec 27, 2012 6:20 pm
by luis
written in a foreign language = not written in PB

Re: Can I use PureBasic with a static c++ library?

Posted: Thu Dec 27, 2012 6:31 pm
by karmacomposer
luis wrote:written in a foreign language = not written in PB
LOL. Yeah, it was written in c++ I believe. It was also originally written in BlitzMax and I know they are using Lua for scripting (because they ported it to both BlitzMax and Lua).

Mike