modules and mains

Brian Tiffin btiffin at gnu.org
Sun Aug 22 05:59:36 UTC 2021


On Sunday, 22 August 2021 at 04:38:58 UTC, Ali Çehreli wrote:
> On 8/21/21 8:46 PM, Brian Tiffin wrote:
>
> > prompt$ cat B.d
> > module B;
> > version = boss;
>
> A cannot know about 'version' condition inside B.
>
> The solution is to provide version on the command line. This is 
> how I do it with dmd:
>
> $ dmd -version=boss [...]
>
> (So, remove 'version = boss;' line in B.d)
>
> > But I'm getting a link error from gdc
> >
> > ```
> > prompt$ gdc -o B B.d
> > /tmp/ccWg1BrF.o: In function `_Dmain':
> > B.d:(.text+0x52): undefined reference to `_D1A7commandFAAyaZi'
> > collect2: error: ld returned 1 exit status
> > ```
> >
> > Is it just wrong thinking and *try again, ya noob*?   ;-)
>
> Never! :)
>
> D modules work both like C's .h files and .c files. (Well, more 
> like C++'s .h files because templates don't need .cpp part.) We 
> import for declarations but we must also include for linking. 
> Again, this is how it works with dmd:
>
> $ dmd B.d A.d -version=boss
>
> Another option is to use dmd's -i switch, which automatically 
> includes modules for linking but I don't have experience with 
> it other than it works:
>
> $ dmd B.d -version=boss -i
>
> (-i can take a pattern as well.)
>
> Ali

Got it working with `gdc`. It makes some sense to list all the 
modules when compiling Boss, and separately when getting the 
sub-commands to each define a main, so I may explore this path 
some more.  But I have the memory of a ~~5~~8 year old, so I'll 
be creating a Makefile as an aid.  Was wistfully thinking I might 
be able to avoid Makefiles with D projects.

```make
# Trying gdc with multiple modules
.RECIPEPREFIX = >

A: A.d
> gdc-11 -o A A.d

B: A.d B.d
> gdc-11 -fversion=boss B.d A.d
```

which gives

```
prompt$ make A
gdc-11 -o A A.d
prompt$ make B
gdc-11 -fversion=boss B.d A.d
```

and

```
prompt$ ./A one
["./A", "one"]
prompt$ ./B
["Boss calling A"]
```
*A gets a car, and B gets a car, everybody gets a car.*

This will get more fun as the real sub-commands get built up now. 
  The plan is a support tool for COBOL development.  Pump out 
named source fragments and project setups, keep Time and Task 
logs, automate Fossil commits, and the like.  Each component 
usable on its own, with the main boss app, hopper.

A little backfill, Grace Hopper was the defacto grand-ma of 
COBOL, one of the first humans to realize that computers could be 
used to compile "programs" and not just math formulas. She was 
also known as the oldest serving member in the U.S. Navy at the 
time she retired as a Rear admiral.  As Zach Weinersmith once 
cartooned in SMBC, "You may not know me, but you should fear me". 
  ;-)

Thanks, Ali.

Have good, make well


More information about the Digitalmars-d-learn mailing list