Page 1 of 3

Import and C++

Posted: Sun Jun 22, 2008 9:21 pm
by Polo
Hi !

Can we import C++ functions with the "Import" feature of Purebasic (I use VC++ express) ?

What syntax should we use for exporting the functions ?

cheers,
Polo

Posted: Sun Jun 22, 2008 10:44 pm
by Fred
You will have to put 'extern "C"' before the function you want to export and it should be ok.

Posted: Tue Jun 24, 2008 8:12 pm
by Polo
This is what I'm doing :)
When I compile the code with import, the linker hang :oops:

I'm pretty sure this is just me doing something wrong with visual C++ express, but I can't figure out what :cry:

Here's a quick example to show you what the problem is :

http://dl.free.fr/nopKQCTV1/test.rar

thanks :)

Posted: Tue Jun 24, 2008 8:37 pm
by milan1612
I created a new project with VS2008 and compiled this code to a static library:

Code: Select all

extern "C" {
	int get()
	{
		return 1;
	}
};
The Purebasic Linker doesn't find the function, even when I import it like this

Code: Select all

Import "Release/testlib.lib"
  get.l() As "get"
EndImport
it won't work :cry:

Posted: Tue Jun 24, 2008 8:46 pm
by Polo
Yes this is exactly the problem I have :)
Is that a bug ?

Posted: Tue Jun 24, 2008 8:54 pm
by srod
Will you not need to use ImportC with that lib?

Posted: Tue Jun 24, 2008 8:56 pm
by Polo
srod wrote:Will you not need to use ImportC with that lib?
No I'm afraid it doesn't change anything, as I'm using _stdcall :cry:

Posted: Tue Jun 24, 2008 9:00 pm
by milan1612
srod wrote:Will you not need to use ImportC with that lib?
Doesn't change anything, I already tried that before :?

Posted: Tue Jun 24, 2008 9:02 pm
by srod
What about :

Code: Select all

ImportC "Release/testlib.lib" 
  get.l() As "_get" 
EndImport

Posted: Tue Jun 24, 2008 9:07 pm
by DoubleDutch
VS2008
Don't forget that VS2008 doesn't write code that will run on Win9x!
(I know that this isn't the cause of the problem, but its worth considering if your coding a lib and would like it to run on Win9x machines).

Posted: Tue Jun 24, 2008 9:08 pm
by Polo
srod wrote:What about :

Code: Select all

ImportC "Release/testlib.lib" 
  get.l() As "_get" 
EndImport
It unfortunately doesn't change anything, as the linker just crash :(

Posted: Tue Jun 24, 2008 9:10 pm
by milan1612
@srod
Sorry, no change, it simply doesn't find the function and outputs this:
PureBasic - Linker error
POLINK: fatal error: Corrupt library: 'C:\Dokumente und Einstellungen\Milan\Eigene Dateien\testlib\Release\testlib.lib'; unable to find symbol '_get' in member '.\Release\testlib.obj'.
@DoubleDutch
I'm aware of this, thanks anyway... :P

Posted: Tue Jun 24, 2008 9:17 pm
by Fred
May be you can try with the lastest polink version (found in the pellesc package).

Posted: Tue Jun 24, 2008 9:21 pm
by srod
Aye, my next suggestion was to perhaps look for a more up to date version of Polink. :)

Posted: Tue Jun 24, 2008 9:22 pm
by milan1612
OK, downloaded and replaced the new polink.exe, but the error persists :?