www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - how to "include" the module file like C ?

reply "lzzll" <ownrepos gmail.com> writes:
Hi, I can auto index the module file use gcc,
but in D I must append the module file path to dmd args,
if I don't, dmd will give me an error like:
main.o: In function `_Dmain':
main.d:(.text._Dmain+0x15): undefined reference to `_D3lib1ai'
main.d:(.text._Dmain+0x2d): undefined reference to `_D3lib1ai'
collect2: ld returned 1 exit status
--- errorlevel 1

Here is the example:
//main.c
#include <stdio.h>
#include "lib.c"
int main(int argc, char** argv){
	printf("%d\n", a);
	a = 10;
	return 0;
}

//lib.c
int a = 1;

[pass] gcc main.c

//main.d
import std.stdio;
import lib;
int main(string[] args){
	writeln(lib.a);
	lib.a = 10;
	return 0;
}

//lib.d
int a = 1;

[pass] dmd main.d lib.d
[failed] dmd main.d
[failed] dmd main.d -inline
[failed] dmd main.d -I./
[failed] dmd main.d -I/full/path/

That mean I can't use geany to do the one key build when import 
the modules from file,
anybody known how to "include" the module file in D ?
Please teach me, thanks very much.
Apr 06 2012
parent reply simendsjo <simendsjo gmail.com> writes:
On Fri, 06 Apr 2012 11:36:48 +0200, lzzll <ownrepos gmail.com> wrote:

 Hi, I can auto index the module file use gcc,
 but in D I must append the module file path to dmd args,
 if I don't, dmd will give me an error like:
 main.o: In function `_Dmain':
 main.d:(.text._Dmain+0x15): undefined reference to `_D3lib1ai'
 main.d:(.text._Dmain+0x2d): undefined reference to `_D3lib1ai'
 collect2: ld returned 1 exit status
 --- errorlevel 1

 Here is the example:
 //main.c
 #include <stdio.h>
 #include "lib.c"
 int main(int argc, char** argv){
 	printf("%d\n", a);
 	a = 10;
 	return 0;
 }

 //lib.c
 int a = 1;

 [pass] gcc main.c

 //main.d
 import std.stdio;
 import lib;
 int main(string[] args){
 	writeln(lib.a);
 	lib.a = 10;
 	return 0;
 }

 //lib.d
 int a = 1;

 [pass] dmd main.d lib.d
 [failed] dmd main.d
 [failed] dmd main.d -inline
 [failed] dmd main.d -I./
 [failed] dmd main.d -I/full/path/

 That mean I can't use geany to do the one key build when import the  
 modules from file,
 anybody known how to "include" the module file in D ?
 Please teach me, thanks very much.
You can include a file as text using import("filename"), and you can convert it to code using mixin(string) mixin(import("lib.d")); will work for you example. *BUT*... You are completely breaking the module system and working against the language rather than with it. I recommend you use the tool rdmd: "rdmd main" will pick up dependencies and add them to dmds command line.
Apr 06 2012
parent "lzzll" <ownrepos gmail.com> writes:
On Friday, 6 April 2012 at 09:56:19 UTC, simendsjo wrote:
 I recommend you use the tool rdmd: "rdmd main" will pick up 
 dependencies and add them to dmds command line.
Rdmd works perfect, I just need replace linker=dmd -w -of"%e" "%f" to linker=rdmd --build-only -w -of"%e" "%f" in filetypes.d for geany then all things will be good. Thank you very much !
Apr 06 2012