May 01, 2024, 03:57:43 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


How to reference secondary source file

Started by Steve, December 01, 2009, 11:43:23 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Steve

Hope someone can help.    Using two files.   File-1 being the main source and File-2 containing a function.   I understand that in File-1 (or the main source file) in order to refer to a function in another file it must be declared like so,  declare EXTERN otherfilefunction(string str),INT
My question is, what is the command to tell the compiler where to find File-2?   I've been going around in circles.     In File-2 the function is prefixed with the Global keyword.   In File-1, I've used the $INCLUDE command, containing the full path but that doesn't seem to work.

The following is where I'm currently at and when compiling the compiler is throwing a duplicate declaration error.

File-1

$INCLUDE "C:\Users\Steve\Documents\Learning\EmergenceBasic\MultiExample-01\multifile-example-02.inc"

OPENCONSOLE
declare EXTERN printname(string str),INT

string name
int fig

name = "Me and you"
fig = printname(name)
print "out of sub, fig = ", fig

WAITCON
CLOSECONSOLE


File-2

global sub printname(string str), int
print "In Sub, printing str, which is ", STR
return 10
ENDSUB

Steve
Melbourne, Australia.

LarryMc

to use multiple files you have to create a new project from the menu
Then open each of the source files you want in the project
As you do, right click on the source file code and select add-to-project for each one

As you do this to each source file you will see the filename appear in the lower right window of the IDE

Your main source file has to have a $MAIN at the top so the compiler knows where to start execution.

In the EB help file in IDE TOPICS there is a section devoted to USING PROJECTS
It should answer your questions in more detail if you'll read it. ;)

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Steve

December 02, 2009, 02:48:12 AM #2 Last Edit: December 02, 2009, 03:54:53 AM by Steve
Thanks Larry for pointing me in the right direction.   As you can imagine this has led to more questions, for me at least.   Below are three short console code files which do work as intended but I'm now confused as to when and under what conditions the keywords, GLOBAL, EXTERN & DECLARE are used as regards subroutines in secondary or external files.

' File-1 (main source file)
$MAIN
$INCLUDE "C:\Users\Steve\Documents\Learning\EmergenceBasic\MultiExample-01\file-02.inc"
$INCLUDE "C:\Users\Steve\Documents\Learning\EmergenceBasic\MultiExample-01\file-03.inc"
OPENCONSOLE
'declare EXTERN printname(string str),INT

def name as STRING
def fig as int
name = "You and me"
fig = displayname(name)
print "Back in File-1 (main source) and returned value from displayname() is : ", fig

def r as INT
r = addnum(4,7)
print "Back in File-1 (main source) and returned number is ", r

WAITCON
CLOSECONSOLE


' File-2
sub displayname(string str), int
print "In File-2, Subroutine - displayname() passed in value (str) is : ", STR
return 10
ENDSUB


' File 3
sub addnum(int n1, int n2), int
int ret
ret = n1 + n2
print "In File-3 (addnum) =  ", ret
return ret
ENDSUB


Whilst working with the above code, initially I had prefixed a sub with GLOBAL and the compiler was returning this error, "Error: Duplicate public symbol displayname".   Eventually I removed the GLOBAL prefix and presto, it worked.

As you can see in the main source file I commented out the "DECLARE EXTERN" line and the program/code still works and without prefixing the subs in the external files with GLOBAL.

I'm now a little confused as to when to use EXTERN & GLOBAL.   I've read the help section, Language/Subroutines , a number of times but it's not sinking in.   I'm wondering if anyone could shed some light.
Steve
Melbourne, Australia.

LarryMc

December 02, 2009, 06:03:44 AM #3 Last Edit: December 02, 2009, 06:09:37 AM by Larry McCaughn
As best I can figure you are trying to "include" the 2nd and 3rd file.

You don't put the kind of code that is in #2 and #3 in include files.

Include files should only be used for declares, constants and global variables, and such.

With the example code I see you don't even need the include files.

When you are building a project you need  a declare extern statement in every .eba file where you want to call a function in another .eba file.

The function has to be declared global if the function is to be called from a .eba file other than the one in which the function resides.


attached is your corrected project.
Unzip in a directory of your choice.
From the IDE select open project and find the steve_project.ebp file and open it.
When it ask if you want to adjust for the new directory location answer yes.

Compile it and run.

Hope this helps.



Larry

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

QuoteAs you can see in the main source file I commented out the "DECLARE EXTERN" line and the program/code still works and without prefixing the subs in the external files with GLOBAL.
That's because by "including" file #2 and #3 they reside in the #1 file when your program is compiled.

It works for your specific example.
But I promise you that if you needed to call the function in #3 from #2 and used your include scheme it will never compile.
You'll get duplicate definition errors.

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Steve

Thanks Larry, I'll check this out later today (after work).
Steve
Melbourne, Australia.

Steve

Once again Larry, thanks for that, it clears things up somewhat.   Just had a play around with your modified version and it makes sense.
Steve
Melbourne, Australia.