Author Topic: Static Library Creation using BCX  (Read 277 times)

jcfuller

  • Full Member
  • ***
  • Posts: 167
    • View Profile
Static Library Creation using BCX
« on: August 11, 2020, 09:23:48 AM »
Does anyone have a (MSVC) example of creating and using a static library with just a couple of simple functions?

Thanks,
  James

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 538
    • View Profile
Re: static library creation and use
« Reply #1 on: August 12, 2020, 11:20:42 AM »
Here's a complete PELLES C roadmap for you.  I would think MSVC would not be too different.

When working with batch files instead of inside the VS IDE, MSVC imposes upon you the need
to launch one of its special command prompts, so that it has all the environmental variables
it needs.  ( A stupid PIA, imo )

Okay ... first the BCX files.   

AddTwo.bas that we'll create a static lib from.

Code: [Select]
$NOMAIN
$NOWIN
$ZAP

FUNCTION AddTwo (a AS INT, b AS INT) AS INTEGER
  FUNCTION = a+b
END FUNCTION

Test.bas that will link Test.obj and AddTwo.lib

Code: [Select]
$CPROTO ! int AddTwo (int,int);  // Copy this from AddTwo.c

CLS

FOR long i = 1 TO 10
  PRINT AddTwo(i,i)
NEXT

PAUSE

A batch file that clearly explains the steps involved.

Code: [Select]
@ECHO OFF
@SETLOCAL

Echo 1) Translate AddTwo.bas and Test.bas with BCX

BC addtwo
BC Test

Echo 2) Setup your Pelles Environment

SET  PellesCDir=C:\PellesC
SET  PATH=%PellesCDir%\Bin
SET  INCLUDE=%PellesCDir%\Include;%PellesCDir%\Include\Win;
SET  LIB=%PellesCDir%\Lib;%PellesCDir%\Lib\Win;
SET  PoccOPTS=/Go /Gn /W2 /Gd /Ze /Zx /Tx86-coff  /D_WIN32_WINNT=0x501
SET  PolinkOPTS= -release -machine:x86 /subsystem:console,5.01 /STACK:8192
SET  PolinkLIBS=kernel32.lib user32.lib 


ECHO 3) Create an object file of our AddTwo function
c:\pellesc\bin\pocc   %PoccOPTS%    addtwo.c


ECHO 4) Create a STATIC lib from our AddTwo object (.obj) file
c:\pellesc\bin\polib.exe -out:addtwo.lib  addtwo.obj


ECHO 5) Compile our Test program
c:\pellesc\bin\pocc   %PoccOPTS%    Test.c


ECHO 5) Now link everything together
c:\pellesc\bin\polink %PolinkLIBS%  %PolinkOPTS%  Test.obj  AddTwo.lib


@ENDLOCAL
   


The output from the batch file
Code: [Select]
1) Translate AddTwo.bas and Test.bas with BCX

BCX BASIC to C/C++ Translator (c) 1999-2020 by Kevin Diggins
Version 7.5.3 (2020/08/12)  Compiled for 64-bit Windows using MS Visual C++
[Lines In: 7] [Lines Out: 107] [Statements: 6] [Time: 0.01 sec's]
BCX translated [Addtwo.Bas] to [Addtwo.C] for a C Compiler


BCX BASIC to C/C++ Translator (c) 1999-2020 by Kevin Diggins
Version 7.5.3 (2020/08/12)  Compiled for 64-bit Windows using MS Visual C++
[Lines In: 15] [Lines Out: 238] [Statements: 7] [Time: 0.02 sec's]
BCX translated [Test.Bas] to [Test.C] for a C Compiler

2) Setup your Pelles Environment
3) Create an object file of our AddTwo function
4) Create a STATIC lib from our AddTwo object (.obj) file
5) Compile our Test program
5) Now link everything together
Press any key to continue . . .



And the output from Test.exe
Code: [Select]
2
 4
 6
 8
 10
 12
 14
 16
 18
 20

Press any key to continue . . .

« Last Edit: August 12, 2020, 11:55:49 AM by MrBcx »

jcfuller

  • Full Member
  • ***
  • Posts: 167
    • View Profile
Re: static library creation and use
« Reply #2 on: August 12, 2020, 12:24:35 PM »
Kevin,

Thank You.

I appreciate the code but...

I did want MSVC. I did not specify but was looking for an example I might use with c++.
I expect the problems I have are with name mangling, but it is now moot!

I was looking for an alternative to the $USE method for Josť's Afx Library but the more I think about it $USE is  the best way. It can be used with g++, c++, and clang compilers; 32/64 ANSI or Unicode.

Thanks again

James

« Last Edit: August 12, 2020, 03:07:52 PM by MrBcx »

jcfuller

  • Full Member
  • ***
  • Posts: 167
    • View Profile
Re: static library creation and use
« Reply #3 on: August 12, 2020, 12:37:53 PM »
Kevin,
  You can use any command prompt and run one of these:

SET XTYPE=x64

"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" %XTYPE%

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" %XTYPE%

James

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 538
    • View Profile
Re: static library creation and use
« Reply #4 on: August 12, 2020, 01:18:15 PM »
JC- I'm using VS2015

The only changes that I needed to compile for MSVC are:

1) Remove $NOWIN and $ZAP from AddTwo.bas

2)  Include the -C switch when translating the BASIC files

3) Use this batch file which will require updating where your \VC\ folder is.

Code: [Select]
@ECHO OFF
 
Echo 1) Translate AddTwo.bas and Test.bas with BCX

BC addtwo    -c
BC Test      -c

Echo 2)  Setup MSVC environment
CALL C:\VS2015\vc\bin\vcvars32.bat


ECHO 3) Create an object file of our AddTwo function
C:\VS2015\vc\bin\cl.exe /O2 /c /Gd /W3 /MT /D_WIN32_IE=0x0501  addtwo.cpp


ECHO 4) Create a STATIC lib from our AddTwo object (.obj) file
C:\VS2015\vc\bin\lib.exe -out:addtwo.lib  addtwo.obj


ECHO 5) Compile our Test program
C:\VS2015\vc\bin\cl.exe test.cpp addtwo.lib


ECHO 6) Now link everything together
C:\VS2015\vc\bin\link.exe Test.obj AddTwo.lib /RELEASE /MACHINE:X86 /SUBSYSTEM:CONSOLE /OUT:Test.exe

PAUSE
« Last Edit: August 12, 2020, 03:10:18 PM by MrBcx »

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 538
    • View Profile
Re: static library creation and use
« Reply #5 on: August 12, 2020, 03:03:01 PM »
Let's give MINGW some love:

1)  Use the two BCX Basic files from the Pelles C project (No. 2 post in this thread).

2) Compile with MINGW (g++) using this batch file:

Code: [Select]
@ECHO OFF
@SETLOCAL

Echo 1) Translate AddTwo.bas and Test.bas with BCX

BC addtwo
BC Test

Echo 2) Setup your MINGW Environment
SET PATH=C:\MinGW\bin;
SET INCLUDE=C:\MinGW\include;C:\MinGW\include\sys;
SET LIB=C:\MinGW\lib;


ECHO 3) Create an object file of our AddTwo function
c:\mingw\bin\g++ -c  AddTwo.c


ECHO 4) Create a STATIC lib from our AddTwo object (.o) file
c:\mingw\bin\ar  -rc  libAddTwo.a  AddTwo.o


ECHO 5) Create an object file from TEST.c
c:\mingw\bin\g++  -c  Test.c


ECHO 6) Link our Test (object) and AddTwo(library) files together
c:\mingw\bin\g++  -o Test.exe  Test.o  -L.  -lAddTwo

@ENDLOCAL
 
PAUSE
« Last Edit: August 13, 2020, 10:24:48 AM by MrBcx »

jcfuller

  • Full Member
  • ***
  • Posts: 167
    • View Profile
Re: Static Library Creation using BCX
« Reply #6 on: August 13, 2020, 09:40:32 AM »
Kevin,
  Thank you for all the code.
I was able to figure out how to compile a basic source file with many functions to an obj file using option /Gy. This gives function granularity.
It is not very practical if you want to provide libraries for a number of different 32/64 ansi/unicode compilers but I only use MSVC so I may pursue it further.
I'll post an example shortly

James

jcfuller

  • Full Member
  • ***
  • Posts: 167
    • View Profile
Re: Static Library Creation using BCX
« Reply #7 on: August 13, 2020, 01:11:30 PM »
Ok here we go.

I first commented all the $USE $ENDUSE items in the AfxTime.bas file.(61 functions)
'------------------------------------------------------------------------------
get a vsc command prompt from a normal terminal.

"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x64
'------------------------------------------------------------------------------
translate AfxTime.bas to c++

bc AfxTime -c
'------------------------------------------------------------------------------
I then copied all the user prototypes from afxtime.cpp to afxtime.h which is referenced in the afxlibtest_02.bas file.(#include "afxtime.h") It is in the attached zip.
'------------------------------------------------------------------------------

Create AfxTime.obj object file. This is the same as Kevin's call from above with the added /Gy to include function level granularity.

cl /O2 /c /Gd /Gy /W3 /MT /D_WIN32_IE=0x0501 afxtime.cpp
'------------------------------------------------------------------------------
create the library file which I named Afx_Time.lib

lib -out:Afx_Time.lib afxtime.obj
'------------------------------------------------------------------------------
translate the test file afxlibtest_02.bas

bc afxlibtest_02 -c
'------------------------------------------------------------------------------
compile it
  cl afxlibtest_02.cpp
 
link is automatically called to produce the exe. no link line needed
'------------------------------------------------------------------------------

run it
afxlibtest_02
'------------------------------------------------------------------------------

James



Robert

  • Sr. Member
  • ****
  • Posts: 449
    • View Profile
Re: Static Library Creation using BCX
« Reply #8 on: August 13, 2020, 06:00:34 PM »
Hi JCF and MrBCX:

Thank you for these examples.

I found James' reference to the -Gy flag very interesting.

James, does the _Gy flag restrict loading only to referenced modules in the same manner as your $USE $ENDUSE directives?

jcfuller

  • Full Member
  • ***
  • Posts: 167
    • View Profile
Re: Static Library Creation using BCX
« Reply #9 on: August 13, 2020, 07:00:44 PM »
Robert,
  If you comment all but one of the example lines you will notice the size of the exe shrinks even though you are linking to a single obj file in the lib that has 61 functions.
It does perform the same function but with libraries you have to create lib's for each compiler 32/64 ansi/unicode.
With the $IFUSE $ENDUSE you can create which ever you want just a few lines of code. ULEX, -u, x32,x64 and of course the $USE USE_xxxx in your source.


/Gy
https://docs.microsoft.com/en-us/cpp/build/reference/gy-enable-function-level-linking?view=vs-2019



From here:
https://docs.microsoft.com/en-us/cpp/build/reference/opt-optimizations?view=vs-2019

/OPT:REF eliminates functions and data that are never referenced;

By default, /OPT:REF is enabled by the linker

James