Page 1 of 1

Additional include directories

Posted: Wed May 11, 2022 2:43 pm
by akamicah
Hello!

Reusable code is great, but can be tedious when wanting to use the same reusable code across multiple projects, especially if changing something in that code library and needing the change to be available to all projects.

Of course there is using compiler constants like the PB home directory, and one could always use git submodules too, but could we possibly look to having the option to specify additional include directories in the compiler options (being able to resolve env vars would be a huge benefit)? that way we're not limited to using PB home directory, or even worse, hard-coded directories in-code that fall outside of the project's scope.

In terms of how it would work, upon an include/xinclude, if the file doesn't exist relative to the project/file directory, then the compiler could go through the list of additional included directories to find the file before declaring it missing.

Thanks!

Re: Additional include directories

Posted: Wed May 11, 2022 3:47 pm
by Little John
akamicah wrote:In terms of how it would work, upon an include/xinclude, if the file doesn't exist relative to the project/file directory, then the compiler could go through the list of additional included directories to find the file before declaring it missing.
I don't think that's a good idea, because then it remains nebulous which files were actually compiled in the end.
With "XIncludeFile" you can include arbitrary source code files and the effect of this command is self-documenting.

You can even use "IncludePath" if you want.

Re: Additional include directories

Posted: Thu May 12, 2022 10:06 am
by akamicah
Little John wrote: Wed May 11, 2022 3:47 pm
akamicah wrote:In terms of how it would work, upon an include/xinclude, if the file doesn't exist relative to the project/file directory, then the compiler could go through the list of additional included directories to find the file before declaring it missing.
I don't think that's a good idea, because then it remains nebulous which files were actually compiled in the end.
With "XIncludeFile" you can include arbitrary source code files and the effect of this command is self-documenting.

You can even use "IncludePath" if you want.
I don't think it'd be as much of an issue as you might think. Being able to reference external directories of reusable code is very common across compilers of other languages, and as for nebulously, providing the developer uses it wisely then there should be no issue.

An example could be a directory of reusable code where each module/library is in it's own subdirectory (a method adopted by a lot of frameworks)

Additional include directory:
.../myIncludes
.../myIncludes/maths/vector.pbi
.../myIncludes/crypt/rc4.pbi


If the project referenced ".../myIncludes" then the file to be included would be "maths/vector.pbi" or "crypt/rc4.pbi"

You could also create custom constants in the compiler option to point to a directory, but it would certainly be cleaner to separate the concerns IMO

Re: Additional include directories

Posted: Thu May 12, 2022 11:08 am
by BarryG
What's wrong with using "IncludePath" in your code to point to an external folder of files? It does what you want.

Re: Additional include directories

Posted: Thu May 12, 2022 11:49 am
by akamicah
BarryG wrote: Thu May 12, 2022 11:08 am What's wrong with using "IncludePath" in your code to point to an external folder of files? It does what you want.
Hard-coded paths in code that fall outside the project's folder/scope is a very ugly and bad practice, especially if you distribute your code.

Say your project is in /home/micah/Projects/myProject and a library of shared code is in /home/micah/Projects/myBigCodebase if you IncludePath to "../myBigCodebase" then it's outside of your project's scope, and the second you move your project, or the second you bring it to another machine where the paths are different, it breaks

Re: Additional include directories

Posted: Thu May 12, 2022 12:09 pm
by StarBootics
BarryG wrote: Thu May 12, 2022 11:08 am What's wrong with using "IncludePath" in your code to point to an external folder of files? It does what you want.
From my perspective NO! When you use IncludePath the problem is if you rearrange your folder you have to change the path pointed out in the IncludePath.

That's why I'm using ToolBoxManager You still need to specify the file you want but the path to it is not required at all. The drawback : work only with the Main file.

Best regards
StarBootics

Re: Additional include directories

Posted: Thu May 12, 2022 1:27 pm
by Danilo
The problem with

Code: Select all

IncludePath #PB_Compiler_Home + "includes/"
is, that it is different for every installation of PureBasic (current/old/beta version).

If we could set additional include search paths inside the IDE or specify locations
at the command line (-I "the/path/to/my/includes"), it could be really helpful for
specifying the personal folder where includes/libs are to be found.

Code: Select all

Macro Includes(file="")
    #HomeDir + "purebasic/includes/"+file
EndMacro

IncludePath Includes()
XIncludeFile Includes("my_lib_001.pbi")

Re: Additional include directories

Posted: Thu May 12, 2022 3:30 pm
by Little John
akamicah wrote:Hard-coded paths in code that fall outside the project's folder/scope is a very ugly and bad practice, especially if you distribute your code.
Any paths that fall outside the project's scope are very ugly in my opinion, regardless whether they are hard-coded or "soft-coded" by means of environment variables or something similar, especially if you distribute your code – because you do not distribute your environment variables together with your code.
Programming code should be as self documenting as possible, anything else is bad practice.
So just use "XIncludeFile" with paths inside your project's scope.

Re: Additional include directories

Posted: Thu May 12, 2022 3:39 pm
by Danilo
Little John wrote: Thu May 12, 2022 3:30 pm So just use "XIncludeFile" with paths inside your project's scope.
This means you have to copy a library/.pbi you downloaded from the forum or github
into every project, and on updates you have to replace it inside all projects!?

Of course we can do "XIncludeFile '/users/danilo/git/purebasic/the_lib.pbi'", but when
I share this project with a friend, he can't run/execute it.

That's where specifying a lib/include directory may make sense.

An alternative would be a package manager (https://lib.rs/ or https://pub.dev/) for libs/includes.

Re: Additional include directories

Posted: Thu May 12, 2022 4:03 pm
by Little John
Danilo wrote: Thu May 12, 2022 3:39 pm
Little John wrote: Thu May 12, 2022 3:30 pm So just use "XIncludeFile" with paths inside your project's scope.
This means you have to copy a library/.pbi you downloaded from the forum or github
into every project, and on updates you have to replace it inside all projects!?
Exactly.
That's not elegant, and I don't like it myself. However, I don't know a solution that is better (from my point of view).

Re: Additional include directories

Posted: Thu May 12, 2022 4:09 pm
by Danilo
Little John wrote: Thu May 12, 2022 4:03 pm I don't know a solution that is better (from my point of view).
The solution could be this feature request: Specify additional Include Directories
at command-line or from within the IDE. ;)

Re: Additional include directories

Posted: Thu May 12, 2022 4:22 pm
by Little John
Danilo wrote:The solution could be this feature request: Specify additional Include Directories
at command-line or from within the IDE. ;)
Not in my opinion, as I wrote above.

Re: Additional include directories

Posted: Thu May 12, 2022 4:29 pm
by Danilo
Little John wrote: Thu May 12, 2022 4:22 pm
Danilo wrote:The solution could be this feature request: Specify additional Include Directories
at command-line or from within the IDE. ;)
Not in my opinion, as I wrote above.
Okay.

Re: Additional include directories

Posted: Thu May 12, 2022 5:45 pm
by #NULL
Danilo wrote: Thu May 12, 2022 4:09 pm The solution could be this feature request: Specify additional Include Directories
at command-line or from within the IDE. ;)
IIRC you can define constants at the compilation commandline and in the IDE ..and then use them in include statements?