PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Class CWSTR in multi-module projects  (Read 308 times)

VANYA

  • Little Newbie
  • *
  • Posts: 13
Class CWSTR in multi-module projects
« on: January 27, 2021, 07:22:44 AM »

Hi all!

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

Here I am creating 2 modules:

1.bas:
Code: [Select]
#Include "header.bi"

sub one(s as CWSTR)

?  s

End Sub

2.bas:
Code: [Select]
#Include "header.bi"

sub two(s as CWSTR)

? s

End Sub

And also the header file

header.bi
Code: [Select]
#Include Once "AFX/CWSTR.INC"

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

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

Next, I run the example:
Code: [Select]
#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:
Code: [Select]
#Include "header.bi"

sub one(s as CWSTR)

? "One"

s.ws = "One String"

End Sub

2.bas:
Code: [Select]
#Include "header.bi"

sub two(s as CWSTR)

? "Two"

s.ws = "Two String"

End Sub

header.bi:
Code: [Select]
Type CWSTR extends WSTRING

ws as wstring*40

End Type


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

example:
Code: [Select]
#Include "header.bi"
#inclib "1"

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

sleep

Everything works here.

Logged

Josť Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3325
Re: Class CWSTR in multi-module projects
« Reply #1 on: January 27, 2021, 08:18:59 AM »

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

  • Little Newbie
  • *
  • Posts: 13
Re: Class CWSTR in multi-module projects
« Reply #2 on: January 27, 2021, 10:03:33 AM »

all the procedures are prefixed with the keywork PRIVATE

Not all. Here is an excerpt from the code:

Code: [Select]
' ========================================================================================
' // 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.
Logged

Josť Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3325
Re: Class CWSTR in multi-module projects
« Reply #3 on: January 27, 2021, 03:50:30 PM »

> 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.