Static Linking of Library (no DLL dist.)

Just starting out? Need help? Post your questions and find answers here.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Static Linking of Library (no DLL dist.)

Post by coder14 »

Is it possible to static-link a library with a PureBasic exe?

I am compiling C DLLs with VS2013 and it generates both .lib and .dll files. Can I link my PureBasic exe with any of the outputs so that I do not have to distribute the DLL?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Static Linking of Library (no DLL dist.)

Post by ts-soft »

you can static-link a lib!
but if you use a lib that only included the functionprototypes for a dll, for early-binding, you have to use the dll with your programm.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Static Linking of Library (no DLL dist.)

Post by coder14 »

ts-soft wrote:you can static-link a lib!
but if you use a lib that only included the functionprototypes for a dll, you have to use the dll with your programm.
Hello ts-soft. Thank you. How can I know if the .lib contains only functionprototypes or full functions? Is there a setting in VS2013 do you know?

If it does I just have to import the functions to static-link ?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Static Linking of Library (no DLL dist.)

Post by ts-soft »

coder14 wrote: Hello ts-soft. Thank you. How can I know if the .lib contains only functionprototypes or full functions? Is there a setting in VS2013 do you know?
Normal, if the lib bigger as the dll, you can statically link. This is not the default behavior. The settings for static-linking in VS2013 (and others) is more difficult.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Static Linking of Library (no DLL dist.)

Post by coder14 »

ts-soft wrote:
coder14 wrote: Hello ts-soft. Thank you. How can I know if the .lib contains only functionprototypes or full functions? Is there a setting in VS2013 do you know?
Normal, if the lib bigger as the dll, you can statically link. This is not the default behavior. The settings for static-linking in VS2013 (and others) is more difficult.
OK. Thanks!
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Static Linking of Library (no DLL dist.)

Post by infratec »

Or you can use a trick:

add the dll in a DataSection and call the dll from memory.
Look here:
http://www.purebasic.fr/english/viewtop ... 13&t=42118

Bernd
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Static Linking of Library (no DLL dist.)

Post by ts-soft »

I think, this is a old lib. Better use: http://www.purebasic.fr/english/viewtop ... 91&start=0
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Static Linking of Library (no DLL dist.)

Post by coder14 »

@ts-soft & @infratec: Thank you for the suggestions and links.

I am now able to do it. From VS2013 I create a static library project which generates an object .lib file. Then I import the function from the library. After compiling it in PureBasic the exe runs standalone and is able to read the string by using PeekS and display it in PureBasic's message requester. :D

But why is the library not able to display its own message box after compiling to exe? This works in the debugger:

Code: Select all

extern "C" {
	char* go()
	{
			char* returnStr = "Hello PureBasic!";
			//MessageBox(NULL, LPTSTR(returnStr), TEXT("fromDLL"), MB_OK);
			MessageBox(HWND_DESKTOP, LPTSTR(returnStr), TEXT("fromDLL"), MB_OK);
			return returnStr;
	}
}
EDIT: Another issue is that regardless of the Unicode setting (one set and the other not or vice-versa or both the same) PeekS is always able to read and display the string correctly. But if the library uses Unicode its message box displays the string as Chinese characters. I have to leave it to NOT SET for it to display properly.

Whats the reason for this?
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Static Linking of Library (no DLL dist.)

Post by Keya »

make sure you specify #PB_Ascii as the flag for PeekS?
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Static Linking of Library (no DLL dist.)

Post by coder14 »

Keya wrote:make sure you specify #PB_Ascii as the flag for PeekS?
Thanks for the tip, but there is no issue with PeekS.

But when I try the linking with my bigger library I get this:

Image

The functions are not even in the library.

Can some please help. :(
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Static Linking of Library (no DLL dist.)

Post by chi »

Go to Project\Properties\Configuration Properties\C/C++\All Options

Security Check -> Disable Security Check (/GS-)
Whole Program Optimization -> No
Et cetera is my worst enemy
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Static Linking of Library (no DLL dist.)

Post by coder14 »

chi wrote:Go to Project\Properties\Configuration Properties\C/C++\All Options

Security Check -> Disable Security Check (/GS-)
Whole Program Optimization -> No
YES! Thanks a whole bunch chi! You have cleared up 4 of the 6 errors leaving only 2:

Code: Select all

unresolved external symbol '__imp__findfirst64i32'
unresolved external symbol '__imp__findnext64i32'
Does anyone know how to clear it? Googled it but nothing.

Almost there! :)
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Static Linking of Library (no DLL dist.)

Post by chi »

Hard to tell without the complete project ;)

might be useful
http://www.microsofttranslator.com/bv.a ... 752627.htm
Et cetera is my worst enemy
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Static Linking of Library (no DLL dist.)

Post by coder14 »

chi wrote:Hard to tell without the complete project ;)

might be useful
http://www.microsofttranslator.com/bv.a ... 752627.htm
Thanks again chi. I cannot overcome that issue.

After searching older posts on the forum I decided to try CodeBlocks. It works! A few warnings but no errors.

BUT - the library works perfectly only when the main function is called with debug. I mean this works:

Code: Select all

ImportC "statLib.a"
  go() 
EndImport

Debug go()
but this does not:

Code: Select all

ImportC "statLib.a"
  go() 
EndImport

result = go()
WHY!? :cry: :cry: :cry:
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Static Linking of Library (no DLL dist.)

Post by Danilo »

Could you try to make 'char* returnStr = "Hello PureBasic!";' static or global?
Post Reply