• Welcome to PlanetSquires Forums.
 

Class CWSTR in multi-module projects

Started by VANYA, January 27, 2021, 07:22:44 AM

Previous topic - Next topic

VANYA

Hi all!

How to use this class in a Static Library with multiple modules?

Here I am creating 2 modules:

1.bas:
#Include "header.bi"

sub one(s as CWSTR)

?  s

End Sub


2.bas:
#Include "header.bi"

sub two(s as CWSTR)

? s

End Sub


And also the header file

header.bi
#Include Once "AFX/CWSTR.INC"

declare sub one(s as CWSTR)
declare sub two(s as CWSTR)


I compile to a static library:
Quotefbc -lib 1.bas 2.bas

Next, I run the example:
#Include "header.bi"
#inclib "1"

one("one")
two("two")

sleep


And I get errors:
Quote./lib1.a(1.o):fake:(.text+0x0): multiple definition of `AFX::CBSTR_ISBSTR(void*)@4'
G:\test\FBTEMP.o:fake:(.text+0x0): first defined here
./lib1.a(2.o):fake:(.text+0x0): multiple definition of `AFX::CBSTR_ISBSTR(void*)@4'
G:\test\FBTEMP.o:fake:(.text+0x0): first defined here

As I understand it, a class is assigned to each module (including the executable), and the compiler does not like duplicate definition. I've already broken my whole head :)

-----------------------------

If I take create the same files, but simplified:

1.bas:
#Include "header.bi"

sub one(s as CWSTR)

? "One"

s.ws = "One String"

End Sub


2.bas:
#Include "header.bi"

sub two(s as CWSTR)

? "Two"

s.ws = "Two String"

End Sub


header.bi:
Type CWSTR extends WSTRING

ws as wstring*40

End Type


declare sub one(s as CWSTR)
declare sub two(s as CWSTR)


example:
#Include "header.bi"
#inclib "1"

dim as CWSTR sz
one(sz)
? Sz.ws
two(sz)
? Sz.ws

sleep


Everything works here.


José Roca

No idea. I never work with modules. My framework has been designed to work with source code and all the procedures are prefixed with the keywork PRIVATE, as it is the only way to have dead code removal with FreeBasic.

VANYA

Quote from: José Roca on January 27, 2021, 08:18:59 AM
all the procedures are prefixed with the keywork PRIVATE

Not all. Here is an excerpt from the code:

' ========================================================================================
' // Checks if the passed pointer is a BSTR.
' // Will return FALSE if it is a null pointer.
' // If it is an OLE string it must have a descriptor; otherwise, don't.
' // Get the length in bytes looking at the descriptor and divide by 2 to get the number of
' // unicode characters, that is the value returned by the FreeBASIC LEN operator.
' // If the retrieved length if the same that the returned by LEN, then it must be an OLE string.
' ========================================================================================
FUNCTION CBSTR_IsBstr (BYVAL pv aS ANY PTR) AS BOOLEAN
   IF pv = NULL THEN RETURN FALSE
   DIM res AS DWORD = PEEK(DWORD, pv - 4) \ 2
   IF res = LEN(*cast(WSTRING PTR, pv)) THEN RETURN TRUE
END FUNCTION
' ========================================================================================


José! Can I edit the original CWStr.inc file and then use it in my project?
1) I need to remove the code of the CBSTR class that I do not need and leave only CWSTR
2) Rename file extension inc-> bi

------------------------------------

Of course, I'll leave the authorship reference in the code unchanged.

José Roca

> Not all.

I will add to it.

> José! Can I edit the original CWStr.inc file and then use it in my project?
> 1) I need to remove the code of the CBSTR class that I do not need and leave only CWSTR
> 2) Rename file extension inc-> bi

Yes, as long as you don't make it incompatible with mine.