newbie confusion with arrays and structs

Justin Whear justin at economicmodeling.com
Thu Jan 26 14:10:23 PST 2012


On Thu, 26 Jan 2012 21:53:39 +0000, Robert Bernecky wrote:

> Awesome! I feel much better now; thank you both for your instant help!
> 
> dmd prd.d Array.d -unittest
> 
> rbe at rattler:~/plural$ prd
> [0]
> [0, 1, 2, 3, 4]
> Created int
>  iota shape is %d
> [0]
>  iota value is %d
> [0, 1, 2, 3, 4]
> got to unittest for Array.d
> [0]
> [0, 1, 2, 3, 4]
> Created int
> [0]
> [0, 1, 2, 3, 4]
> rbe at rattler:~/plural$ dmd prd.d Array.d rbe at rattler:~/plural$ prd
> [0]
> [0, 1, 2, 3, 4]
> Created int
> [0]
> [0, 1, 2, 3, 4]
> 
> I found it odd that the import Array.d in prd.d was not adequate to
> bring in Array.d, though... In the interest of science, I tried this:
> 
> ls
> Array.d  crud2.d  crud3.d  crud.o   plusSA.d      prddmd   
> prd.original.d UnitTestArray.d
> Array.o  crud2.o  crud3.o  hello.d  prd.backup.d  prddmd.d  prd.works.d
> crud2    crud3    crud.d   plusSA   prd.d         prd.o    
> UnitTestArray rbe at rattler:~/plural$ dmd prd.d -I.
> prd.o:(.data+0x218): undefined reference to `_D5Array12__ModuleInfoZ'
> prd.o:(.data._D23TypeInfo_S5Array6Parray6__initZ+0x28): undefined
> reference to `_D5Array6Parray6__initZ'
> prd.o: In function `_D3prd4iotaFiZPS5Array6Parray':
> prd.d:(.text._D3prd4iotaFiZPS5Array6Parray+0x101): undefined reference
> to `_D5Array6Parray6__ctorMFNcAiAiZS5Array6Parray' collect2: ld returned
> 1 exit status
> --- errorlevel 1
> 
> At any rate, I am back on my training wheels again, and thank both Teoh
> and Phillips!
> 
> ps: Hereafter, I promise to name my modules in lower case.
>     Mea culpa and all that!


In the interest of saving you some trouble later on, I thought I'd 
explain why the import statement doesn't automatically "bring in" the 
referenced file.
When you compile, you need to give DMD the names of all the files which 
need to be turned into object code (thus you must specify "Array.d" on 
the command line). This is in contrast to linking your code against 
already-compiled stuff (the std library is a good example), which you do 
not need to provide for compilation. DMD is quietly passing the std 
library object file to the linker which is why you don't get linker 
errors.

If you want your Array code to be a separate library/project, you can 
compile it like so:
dmd Array.d -lib

Then when you compile your other project:
dmd prd.d -I/location/of/Array.d -L/location/of/Array.so

Note that some automated build tools can parse your import statements and 
figure out what files need to be included in the dmd command.

Justin


More information about the Digitalmars-d-learn mailing list