Guide to create static C lib for PB in Windows/Visual Studio

Share your advanced PureBasic knowledge/code with the community.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Guide to create static C lib for PB in Windows/Visual Studio

Post by Keya »

ive never created a .lib myself before other than existing projects so i was keen to learn myself how to create a simple one, and it turns out really easy! I've probably made it look harder than it is by breaking it down into 12 steps lol

See also using MASM assembler instead of C - http://www.purebasic.fr/english/viewtop ... 4&p=489136
For OSX & Linux it's even easier - see http://www.purebasic.fr/english/viewtop ... 2&p=488178

For Windows ...
1. Start Visual Studio (im using VS2013 for this), and click File menu -> New Project
2. In \Installed\Templates\Visual C++\Win32, select Win32 Project
3. Type "testlib" in Name, and click Ok
Image

4. Select Static Library, and uncheck the other boxes
Image

5. Right-click "Source Files", click Add -> New Item, and type "testlib.c" (ensuring the extension is .c and not the default .cpp - I dont know if you can make C++ libs for PB, but C is obviously fine and thats what we'll make here)
Image

6. Add the C source code of the library:

Code: Select all

	double addnums(double num1, double num2) {
		return num1 + num2;
	}
7. Build menu -> Configuration Manager, and ensure it's Release and not Debug
8. Project menu -> Properties. Go to Configuration Properties\C/C++\Optimization, and ensure that "Whole Program Optimization" is turned OFF. You can still set the main Optimization to "Maximize Speed" or "Minimize Size" etc if you want and play around with all the other settings too it seems, but you can NOT use Whole Program Optimization with libs! (it's On by default!)
Image
8b. Also, make sure to select "Disable C++ Security Checks"

9. Build menu -> Build Solution.
10. Copy the .lib file to wherever your .pb source file will be
11. Create the .pb source file:

Code: Select all

	ImportC "testlib.lib"  ;ensure ImportC and not Import
	  addnums.d (value1.d, value2.d) As "_addnums"
	EndImport

	MessageRequester("Result", StrD(addnums(5,2)))
12. Run and profit!

See also your \Purebasic\SDK\ directory!

Anyone have any guides for Linux and OSX?!? :)

[minor edit] I changed the function name from "add" to "addnums", as while it was ok on Windows it was clashing on Linux due to "add" being an asm instruction - poor form on my behalf there!
Last edited by Keya on Fri Feb 03, 2017 11:04 am, edited 7 times in total.
User avatar
idle
Always Here
Always Here
Posts: 5095
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Easy guide to creating static C .lib in Visual Studio fo

Post by idle »

good tip
Windows 11, Manjaro, Raspberry Pi OS
Image
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Easy guide to creating static C .lib in Visual Studio fo

Post by jack »

hi Keya
just one note, in step 7 you can also select x64 platform
click on Active solution platform->new , then select x64 as the new platform
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Easy guide to creating static C .lib in Visual Studio fo

Post by Keya »

jack yes good tip although to clarify i think you can only compile for both from the one Visual Studio if your Windows is also 64bit (64bit can compile 32+64, 32bit OS can only compile 32)? not 100% sure though but not really an issue for me though as i'm only using the 64bit one which definitely does both :)
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Easy guide to creating static C .lib in Visual Studio fo

Post by jack »

Keya, I just compiled a 64-bit lib with VS2010 in Windows 7 x86 and it works as expected with PB x64 in Windows x64 of course.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Easy guide to creating static C .lib in Visual Studio fo

Post by Keya »

far out, cool! appreciate the clarifying :)
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Easy guide to creating static C .lib in Visual Studio fo

Post by Lunasole »

Nice to remember.

Also there is a small trick to open VS 2013 projects in VS 2010 (for those who won't use 2013/2015).

Need to open .sln file in text editor and change the following line

Code: Select all

Microsoft Visual Studio Solution File, Format Version 12.00
to

Code: Select all

Microsoft Visual Studio Solution File, Format Version 11.00
Already compiled few projects such way and it had no problems for now.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
normeus
Enthusiast
Enthusiast
Posts: 415
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Guide to create static C lib for PB in Windows/Visual St

Post by normeus »

Keya,

Just tried your tutorial with Visual Studio 2017 and it worked great!!!

I hope you keep adding tips and sharing your knowledge with us.


Thank you again,

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Post Reply