ReDim problems

New to FreeBASIC? Post your questions here.
Post Reply
SotSvart
Posts: 262
Joined: May 27, 2005 9:03
Location: Norway
Contact:

ReDim problems

Post by SotSvart »

Im trying to get some FB code from around 2007 to compile again, but I can't get past this problem. Seems something about variable size arrays has changed since I made the code back in 2007.

Can someone explain why this code gives an compiler error? It works if I keep all the code in one file.

Main Modul:

Code: Select all

Declare Sub TestSub

Extern As Integer Test(), TestUbound

Dim As Integer Test(), TestUbound
ReDim Test(40)


Print "Ubound Test: " & UBound(Test)
TestSub
Print "Ubound Test: " & UBound(Test)

Sleep
Second modul:

Code: Select all

Extern As Integer Test(), TestUbound

Sub TestSub
	TestUbound = 50
	ReDim Preserve Test(TestUbound)
End Sub
This is what FBedit returns:

Code: Select all

D:\Dev\FreeBASIC\fbc -c "test.bas"

Make done

D:\Dev\FreeBASIC\fbc -s console "Dim test.bas" "test.o"
test.o:fake:(.data+0x0): multiple definition of `TEST'
Dim test.o:fake:(.data+0x0): first defined here

Make done
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: ReDim problems

Post by Gonzo »

it looks like you are only compiling one module, from the build log
im assuming you have a second .bas file with the sub test()
it's probably fake because it never was compiled

edit: nevermind, its called "dim test.bas" im assuming
and youre using the .o with the extern

remove extern from test.o
SotSvart
Posts: 262
Joined: May 27, 2005 9:03
Location: Norway
Contact:

Re: ReDim problems

Post by SotSvart »

"test.bas" is compiled to "test.o", which is added to the compile of the main modul "dim test.bas". See the output from FBedit.
I think this is the correct way when you have more then one modul?
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: ReDim problems

Post by Gonzo »

no, actually you can
fbc -s console "dim test.bas" "test.bas" -x "test.exe"

the correct way is to make a .bi (.h equiv) file, and:

test.bi
extern as integer test(), testubound

and then in dim test.bas
#include test.bi

however, your example should work... it could just be that you arent compiling the modules simultaneously
i dont know
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ReDim problems

Post by fxm »

A symbol declared with 'Extern' cannot be (re)defined in several modules.
I think it has never worked with any version of fbc whatsoever.

A solution it to use 'Common' keyword:

Code: Select all

Declare Sub TestSub

Extern As Integer TestUbound
Common As Integer Test()

Dim As Integer TestUbound
ReDim As Integer Test(40)


Print "Ubound Test: " & UBound(Test)
TestSub
Print "Ubound Test: " & UBound(Test)

Sleep

Code: Select all

Extern As Integer TestUbound
Common Shared As Integer Test()

Sub TestSub
   TestUbound = 50
   ReDim Preserve Test(TestUbound)
End Sub
WARNING: There was a bug from fbc 0.18.2 version about 'Common' and 'Redim' for arrays:
Multi-'Redim' forbidden for 'Common' array (compiler error)
This bug is now solved if we use fbc 0.24 version with date > 2012-05-16:
Daily build at FreeBASIC-Portal.de
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Re: ReDim problems

Post by pestery »

If you remove the ReDim Test(40) and change Dim As Integer Test(), TestUbound to Dim As Integer TestUbound then it works. It's a bit of a nuisance, but as fxm said, you can't defined (using Dim or ReDim) the same value or array in difference modules. The problem is similar to if you created a version of your Sub TestSub in both test.bas and "dim test.bas". The compiler would compile both the modules separately without problem and than pass them to the linker to create the exe. But the linker would see two versions of TestSub and give an error regarding multiple definition of ...

Although there is one problem with my solution. There seems to be a bug in the GCC emitter at the moment that that affects Extern when using arrays. I made a comment about it here. It works fine with GAS though :-)

Regarding compiling multiple modules, you can compile them separately using:

Code: Select all

fbc -c mod1.bas
fbc -c mod2.bas
fbc main.bas mod1.o mod2.o
Or all in one command

Code: Select all

fbc main.bas mod1.bas mod2.bas
It makes little difference. You could probably use a mix of the two methods, but I haven't tried it. The only problem I've had when compiling separately is that when using libraries in the non-main modules I also had to add them to the main module just so that fbc knew to add them to the linker list.
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ReDim problems

Post by fxm »

pestery wrote:... you can't defined (using Dim or ReDim) the same value or array in difference modules
only declaring the array with 'Extern', unless you define the variable as a variable shared between multiple modules using the declaration/definition with 'Common' (see my previous post):
With 'Common', the array can be resized in each module ('Common Shared' in the second module to get the array visible inside the procedure, otherwise a local array is created).
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Re: ReDim problems

Post by pestery »

My mistake, it should have read: When using Extern you can't defined (using Dim or ReDim) the same value or array in difference modules. Thanks pointing that out fxm :-)
SotSvart
Posts: 262
Joined: May 27, 2005 9:03
Location: Norway
Contact:

Re: ReDim problems

Post by SotSvart »

Thanks for the answers.

I'm not sure if the code worked back in 2007, because I did at some time change the code from just "Include"ing the modules to using "Extern" and compiling them separately. It could be that I never finished the conversion, hard to remember what I did 5 years ago =)
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ReDim problems

Post by fxm »

I'm sure it did not work, and in addition I checked it on previous versions of fbc.

Documentation updated to make this clearer:
KeyPgExtern
Post Reply