Page 1 of 2

Static Linking of Library (no DLL dist.)

Posted: Sat Sep 19, 2015 6:57 pm
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?

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

Posted: Sat Sep 19, 2015 7:08 pm
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.

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

Posted: Sat Sep 19, 2015 7:13 pm
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 ?

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

Posted: Sat Sep 19, 2015 7:18 pm
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.

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

Posted: Sat Sep 19, 2015 7:34 pm
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!

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

Posted: Sat Sep 19, 2015 7:45 pm
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

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

Posted: Sat Sep 19, 2015 7:51 pm
by ts-soft
I think, this is a old lib. Better use: http://www.purebasic.fr/english/viewtop ... 91&start=0

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

Posted: Sun Sep 20, 2015 12:18 pm
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?

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

Posted: Sun Sep 20, 2015 12:38 pm
by Keya
make sure you specify #PB_Ascii as the flag for PeekS?

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

Posted: Sun Sep 20, 2015 1:18 pm
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. :(

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

Posted: Sun Sep 20, 2015 3:17 pm
by chi
Go to Project\Properties\Configuration Properties\C/C++\All Options

Security Check -> Disable Security Check (/GS-)
Whole Program Optimization -> No

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

Posted: Sun Sep 20, 2015 4:11 pm
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! :)

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

Posted: Sun Sep 20, 2015 5:08 pm
by chi
Hard to tell without the complete project ;)

might be useful
http://www.microsofttranslator.com/bv.a ... 752627.htm

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

Posted: Sun Sep 20, 2015 7:32 pm
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:

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

Posted: Mon Sep 21, 2015 11:40 am
by Danilo
Could you try to make 'char* returnStr = "Hello PureBasic!";' static or global?