Import and C++

Everything else that doesn't fall into one of the other PB categories.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Maybe it is because std things and c++ is used I dont know, well I've gotta give up as I think I tried everything :?
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Post by milan1612 »

Ok, got it:
Create a Win32 Project, select Static Library and unselect Precompiled Headers

Paste this code into a new .cpp file:

Code: Select all

extern "C" {
	int answer() {
		return 42;
	}
};
Select Release mode

Go to the project settings and:
Set General -> Whole Program Optimization to "No Whole Program Optimization"
Set C/C++ -> Optimization -> Optimization to "Disabled"

Compile the project

Import the library in Purebasic using ImportC

Use it :P
I have no clue what the problem with your library is Polo, sorry.
I'll try a bigger example and use std::string, if I succeed I'll post it here...
Windows 7 & PureBasic 4.4
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Well just to tell it, the code has bo error when using VC++ 6 :)

Seems the problem lies with C++ and VC++8, PellesC linker maybe is not compatible.
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

Post by HotteHC »

Hi Polo.
Please try to compile the following Sourcecode in VC2008 express.
Turn off the Optimation like Fred said and build as Static Lib in Release Mode.

Code: Select all

#ifdef  __cplusplus
extern "C"
{
#endif//__cplusplus

int __stdcall GetInt()
{
 return 132;
}

#ifdef  __cplusplus
}
#endif//__cplusplus
In PB :

Code: Select all

Import "LibTest.lib"
 GetInt.l()
EndImport

Debug GetInt()
End
I can run this code without any Problems.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Yes but you're not actually using any C++ code, which is the problem :)

Here you just have C code in a CPP file ;)
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

Post by HotteHC »

But wasn't that nearly the same function witch causes Errors when you importet the Lib to PB?
What other codesnippet makes Problems in youre" C++" Code?
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

Post by HotteHC »

OK.
I tried the following:

Code: Select all

class CTest
{

private :
 int tInt;

 public:

 CTest(){tInt = 132;}
 ~CTest();

 int __stdcall GetInt(){return tInt;}


};

#ifdef  __cplusplus
extern "C"
{
#endif//__cplusplus

CTest* __stdcall createTest()
{
 return new CTest();
}


int __stdcall GetInt(CTest* test)
{
 if (test)
 {
  return test->GetInt();
 }
 return 0;
}


/*
void __stdcall deleteTest(CTest* test)
{
 if(test)
 {
	 delete test;
 }
 test = 0;
}
*/

#ifdef  __cplusplus
}
#endif//__cplusplus

It sees like there is a Problem with the Destructor!
Without the "deleteTest"-Function everything works fine !?!?

Lets test a littlebit more .
Post Reply