Jump to content


Photo

#include and library with another library linked


  • Please log in to reply
4 replies to this topic

#1 trismarck

trismarck
  • Members
  • 229 posts

Posted 07 February 2012 - 11:43 AM

Lets suppose we have a following situation:
project\
|-main.ahk     ; #include %A_ScriptDir%
               ; #include lib\lib.ahk
               ; #include lib\lib2.ahk
|-lib\
   |-[color=red]lib1.ahk[/color] ; this library needs to include an external library, i.e. #include ext_lib\extLib.ahk
             ; how to include an external library here 'without breaking' the A_ScriptDir #include folder?
             ; ('old' #include folder in main.ahk file)
   |-ext_lib\
      |- extLib.ahk ; library without #include directives
   |-lib2.ahk ; library without #include directives

What is the default action if one needs to link an 'external' library to an already existing library in a project, like in the above example?
I've thought about:
[*:34oaxyju] #include lib\ext_lib\extlib.ahk
in main.ahk file
Con: Each time I use lib1.ahk, I have to remember that lib1.ahk uses an external library
[*:34oaxyju] #include %A_ScriptDir%
#include ext_lib\extlib.ahk
in lib1.ahk file
Con: if lib1.ahk included in main.ahk, then in main.ahk directly after the #include lib\lib1.ahk there has to be a #include %A_ScriptDir% directive to restore the old main.ahk #include working directory - looks a little bit ugly:
#include %A_ScriptDir%
#include lib\lib1.ahk
#include %A_ScriptDir%
#include lib\lib2.ahk
#include %A_ScriptDir%
etc.


#2 Lexikos

Lexikos
  • Administrators
  • 8832 posts

Posted 07 February 2012 - 11:55 AM

For AutoHotkey_L, instead of
#include %A_ScriptDir%
#include lib\lib1.ahk
use
#include <lib1>
Additionally, the following applies to both versions of AutoHotkey:

If a library file uses #Include, the working directory for #Include is the library file's own directory. This can be used to create a redirect to a larger library file that contains that function and others related to it.

However, if you do #include lib\lib1.ahk, it is not considered a library file.

Lastly, for v2:

#Include is now relative to the directory containing the current file, by default.



#3 trismarck

trismarck
  • Members
  • 229 posts

Posted 08 February 2012 - 04:25 PM

Hi Lexikos, thanks for the response.

Additionally, the following applies to both versions of AutoHotkey:

If a library file uses #Include, the working directory for #Include is the library file's own directory. This can be used to create a redirect to a larger library file that contains that function and others related to it.

However, if you do #include lib\lib1.ahk, it is not considered a library file.

What is the definition of a library file?
I also have doubts about the sentence you quoted from the documentation - why would someone want to provide an #include ext_lib directive inside lib1.ahk library file if at the same time he'd know that in order to use any of the function inside the lib1.ahk library, he'd have to include that lib1.ahk library file in some other file (like main.ahk). My mind tells me that this (specifying #include ext_lib in lib1.ahk) looks like creating a redirection that will always fail (will fail bc the library will always be broken if linked to main.ahk) (unless I use sth like #include *i inside lib1.ahk)
From what I've understood so far, the sentence you quoted means sth like:
lib1.ahk ; #include dir\lib2.ahk ; works *
dir
|-lib2.ahk
project\
|-main.ahk ; #include lib.ahk ; fails **
|-lib1.ahk ; #include dir\lib2.ahk ; at this point
    |-dir
        |-lib2.ahk
* works because 'the file to be compiled directly' is a library (#include 's working directory is the directory the library file is in)
** fails because 'the file to be compiled directly' is 'a file' (#include 's working directory is the file's directory. File's directory != lib1 default working directory)
Do I understand this correctly? :)

Lastly, for v2:

#Include is now relative to the directory containing the current file, by default.

Ok, this is for Autohotkey v2 (I didn't even know such thing existed, thanks). I guess that new #include behaviour was implemented in response to cases similar to what I've described in the first part of my post (problems with #include working dir inheritance in a multiple files ahk project).

#4 Lexikos

Lexikos
  • Administrators
  • 8832 posts

Posted 09 February 2012 - 02:49 AM

What is the definition of a library file?

A file from a function library, included automatically because of a function-call, or explicitly with #include <>.

why would someone want to provide an #include ext_lib directive inside lib1.ahk library file ...

One would use #include in a library file for the same reasons one would use #include anywhere else.

... if at the same time he'd know that in order to use any of the function inside the lib1.ahk library, he'd have to include that lib1.ahk library file in some other file (like main.ahk).

I don't understand what you think the problem is. If lib1 depends on lib2, either the user must #include both libs in their script (even if they don't explicitly use lib2) or lib1 must #include lib2. If lib1 is split into multiple files, the user should not need to #include each individual file.

#5 trismarck

trismarck
  • Members
  • 229 posts

Posted 09 February 2012 - 08:29 AM

What is the definition of a library file?

A file from a function library, included automatically because of a function-call, or explicitly with #include <>.

Thanks, now I get it. I had doubts if by 'library file' you mean only a file included with #include <> as #include <> has some special meaning.

why would someone want to provide an #include ext_lib directive inside lib1.ahk library file ...

One would use #include in a library file for the same reasons one would use #include anywhere else.

... if at the same time he'd know that in order to use any of the function inside the lib1.ahk library, he'd have to include that lib1.ahk library file in some other file (like main.ahk).

I don't understand what you think the problem is. If lib1 depends on lib2, either the user must #include both libs in their script (even if they don't explicitly use lib2) or lib1 must #include lib2. If lib1 is split into multiple files, the user should not need to #include each individual file.

I've tested #includes further in ahkB and it appears that approach 2) from my first post will always fail. Here is an example:
File structure
project\
|-main.ahk ; #inlcude %A_ScriptDir% ; |if main.ahk compiled|
		     ; #include lib\chair.ahk
|-lib\ ; lib\ or any other folder really
    |-chair.ahk ; #include %A_ScriptDir% 
		          ; #include ext\cabinet.ahk ; |this fails|            ; if chair.ahk compiled, this works
    |-ext\
        |-cabinet.ahk
main.ahk
#include %A_ScriptDir%
#include lib\chair.ahk
chair_beat()

return
chair.ahk
#include %A_ScriptDir% ; fails if chair.ahk library included into main.ahk 
				; (main.ahk directory above chair.ahk directory)
#include ext\cabinet.ahk
chair_beat() {
	MsgBox, % "To beat the dust out of the chair, I need a carpet beater."
	cabinet_carpetBeater()
	MsgBox, % "Done. Dust removed from chair."
}
cabinet.ahk
cabinet_carpetBeater() {
	MsgBox, % "Carpet beater working."
}
 

A workaround would be a solution 1) from my first post:
main.ahk
#include %A_ScriptDir%
[color=red]#include lib\ext\cabinet.ahk[/color] ; required by chair.ahk
#include lib\chair.ahk
 
chair_beat()
return
 
chair.ahk
[color=red]#include *i ext\cabinet.ahk[/color] ; workaround so main.ahk would work
chair_beat() {
	MsgBox, % "To beat the dust out of the chair, I need a carpet beater."
	cabinet_carpetBeater()
	MsgBox, % "Done. Dust removed from chair."
}
In ahkL everything is much simpler because of #include <>.