Declares defined as public?

Hello

I’m working with Declares after including an external Lib. I include the Lib in the OPEN event of the MainWindow and also doing the Declares there. This is working fine as I call the declared functions in the named OPEN event.

On calling the declared functions from outside of the OPEN event the declared functions are not known. Can I define declared functions somehow as “public” or do I have to include the Lib AND the declared functions wherever I use them?

Optimum: Including Lib and Declare functions ONCE and then being able to use them where I call them (from Xojo methods). Is that possible? The documentation doesn’t describe that.

Usually you’ll wrap your declares in a function. If you need to access it multiple places in your application, you can put that function in a module.

Try Insert>Module then Insert>External Method ?

https://documentation.xojo.com/getting_started/using_the_xojo_language/advanced_language_features.html

[quote=486169:@]Try Insert>Module then Insert>External Method ?
[/quote]
as long as you never care about writing x-platform code they’re fine :slight_smile:

Thank you all! Sorry for not being precise enough…

I need to call those functions from a single place, depending on users choice. My Lib has like 30 functions and I need to react differently of the return of those functions. If I have to put that in one code fragment it would be huge.

I am writing x-plattform (Win, Mac, Linux) with 3 different Libs (with same functions/parameter). I tried to call them on all 3 platforms, works! So I’m still not sure if I have to do these 3 steps in the same code fragment:

  • Include Lib
  • Declare a function of that Lib
  • Call the declared function

-> So whenever I call a function from that Lib I first have to include that Lib, then Declare a function and then call the function (all at the same place (method)).

OR if I can declare the functions at once and use everywhere inside my App:

  • Include Lib ONCE (on startup fo App)
  • Declare all functions in that Lib ONCE
  • Call any declared functions from somewhere else (any Xojo method)

I’m fine with the first option. I just don’t want to produce an internal overhead if it’s not needed.

What do you mean by “Include Lib”?

Unfortunately there’s no way to limit the inclusion of external methods (insert>external method) by platform which is what Norman was talking about above but you should be able to use methods with common parameters and return types as external method’s of a global module so long as the library has the same name across the platforms and you omit the extension from the declare.

Any methods that use platform specific types will need to be wrapped as Anthony mentioned so depending on your library you may be able to cut down the use of wrapper methods by only writing them when you specifically need them because of those platform specific types and relying on external methods for the rest.

Or you could write wrappers for them all and have them all using the same functionality/looking similar then you can reference the full library name and ship everything to all platforms, 30 isn’t that many once your have your basic method layout written in an external editor its just a cut and paste away from including them straight into xojo :wink:

Also, I assume you’ve found ?If…?Endif

I’d write on module that has xojo methods in it that do the calls into the library
then those xojo functions can hide from you the fact that there IS a dylib
in those functions you can use

#if TargetMacOS
      // make the right declare to the macOS lib here
#elseIfTargetWindows
      // make the right declare to the Windows DLL here
#elseiF TargetLinux
      // make the right declare to the Linux .so here
#endif

and all the rest of your code just uses those Xojo functions from the module

Thank you all guys. I’m on the way as you propose @Norman Palardy

MyModule:

MySharedMethod1

[code]#If TargetWindows Then
Const D2XXLib = “ftd2xx.dll”
#EndIf

#If TargetMacOS Then
Const D2XXLib = “…/Resources/libftd2xx.1.4.16.dylib”
#EndIf

#If TargetLinux Then
Const D2XXLib = “Libs/libftd2xx.so.1.4.8”
#EndIf

Soft Declare Function MyLibFunction1 Lib D2XXLib Alias …

// Execute MyLibFunction1
MyLibFunction1(…)
…[/code]

MySharedMethod2

[code][code]#If TargetWindows Then
Const D2XXLib = “ftd2xx.dll”
#EndIf

#If TargetMacOS Then
Const D2XXLib = “…/Resources/libftd2xx.1.4.16.dylib”
#EndIf

#If TargetLinux Then
Const D2XXLib = “Libs/libftd2xx.so.1.4.8”
#EndIf

Soft Declare Function MyLibFunction2 Lib D2XXLib Alias …

// Execute MyLibFunction2
MyLibFunction1(…)
…[/code]

… and so on (for each Lib function I use)

That way I don’t have all Declares in one big code fragment. I’m aware the Lib functions must exist in all included OS Libs with the same parameter/types, otherwise it won’t work properly.