Additional include directories

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
akamicah
User
User
Posts: 18
Joined: Fri May 06, 2022 7:11 pm
Contact:

Additional include directories

Post 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!
Last edited by akamicah on Thu May 12, 2022 11:59 am, edited 1 time in total.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Additional include directories

Post 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.
akamicah
User
User
Posts: 18
Joined: Fri May 06, 2022 7:11 pm
Contact:

Re: Additional include directories

Post 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
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re: Additional include directories

Post by BarryG »

What's wrong with using "IncludePath" in your code to point to an external folder of files? It does what you want.
akamicah
User
User
Posts: 18
Joined: Fri May 06, 2022 7:11 pm
Contact:

Re: Additional include directories

Post 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
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Additional include directories

Post 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
The Stone Age did not end due to a shortage of stones !
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Additional include directories

Post 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")
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Additional include directories

Post 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.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Additional include directories

Post 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.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Additional include directories

Post 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).
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Additional include directories

Post 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. ;)
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Additional include directories

Post 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.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Additional include directories

Post 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.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Additional include directories

Post 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?
Post Reply