www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - LDC / BetterC / _d_run_main

reply Richard <garlicbready googlemail.com> writes:
Hi,
I've been trying to see if I can get an mbed project to work with 
Dlang
basically compiling D code for use on a Cortex-M Proccessor
So far I've been using the latest release of LDC with the 
-BetterC Flag
 From what I can gather, normally without -BetterC it works like 
this:

   * main() - C main generated by the compiler
   * _d_run_main - called by C main to setup the runtime
   * _Dmain - main function in D land, is called by _d_run_main

With -BetterC enabled, it instead works like this

   * main() - C main generated by the compiler
   * _d_run_main - needs to be written by the user since there's 
no runtime
   * _Dmain - main function in D land

so basically I need to write my own basic _d_run_main to call 
_Dmain.
Code in github does something like this typically
```
private alias extern(C) int function(char[][] args) MainFunc;
private extern (C) int _d_run_main(int argc, char** argv, 
MainFunc mainFunc)
{
     return mainFunc(null);
}
```

However the LDC compiler doesn't like this as it expects the 
mainFunc parameter to be a void pointer
so I tried this instead
```
extern (C) int _d_run_main(int argc, char **argv, void* mainFunc) 
{
     MainFuncType mFunc = cast(MainFuncType) mainFunc;
     return mFunc(null);
}
```

but nope that didn't seem to work ether, compiles okay but the 
code in main() (D space) isn't called
I'd imagine this should be a simple thing, anyone got any ideas?
Mar 09 2018
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
Eh, you can simplify it a lot by just writing your own C main 
(legal with or without betterC btw)

---
import core.stdc.stdio;

extern(C)
int main(int argc, char** argv) {
	printf("hello world, %s\n", argc > 1 ? argv[1] : "user");
	return 0;
}
---

that should work with any compiler, with or without -betterC.
Mar 09 2018
prev sibling parent reply Mike Franklin <slavo5150 yahoo.com> writes:
On Saturday, 10 March 2018 at 02:25:38 UTC, Richard wrote:
 Hi,
 I've been trying to see if I can get an mbed project to work 
 with Dlang
 basically compiling D code for use on a Cortex-M Proccessor
You might be interested in the following, if you're not already aware: * https://github.com/JinShil/stm32f42_discovery_demo * https://bitbucket.org/timosi/minlibd The STM32 demo only supports GDC right now, but I'll be updating it to support LDC when 2.079.0 lands there. 2.079.0 removes some coupling of the compiler to the runtime, so I should be able to avoid the following bugs: https://github.com/ldc-developers/ldc/issues/created_by/JinShil
 so I tried this instead
 ```
 extern (C) int _d_run_main(int argc, char **argv, void* 
 mainFunc) {
     MainFuncType mFunc = cast(MainFuncType) mainFunc;
     return mFunc(null);
 }
 ```

 but nope that didn't seem to work ether, compiles okay but the 
 code in main() (D space) isn't called
 I'd imagine this should be a simple thing, anyone got any ideas?
The following worked fine for me on my x64 Linux desktop with LDC version 1.8.0 (DMD v2.078.3, LLVM 5.0.1) ``` main.d import core.stdc.stdio; private alias extern(C) int function(char[][] args) MainFuncType; extern (C) int _d_run_main(int argc, char **argv, void* mainFunc) { MainFuncType mFunc = cast(MainFuncType) mainFunc; return mFunc(null); } void main() { printf("Hello, World!\n"); } ``` Compile with: ldc2 -defaultlib= -debuglib= -betterC main.d Mike
Mar 09 2018
next sibling parent reply Johan Engelen <j j.nl> writes:
On Saturday, 10 March 2018 at 07:54:33 UTC, Mike Franklin wrote:
 On Saturday, 10 March 2018 at 02:25:38 UTC, Richard wrote:
 Hi,
 I've been trying to see if I can get an mbed project to work 
 with Dlang
 basically compiling D code for use on a Cortex-M Proccessor
You might be interested in the following, if you're not already aware: * https://github.com/JinShil/stm32f42_discovery_demo * https://bitbucket.org/timosi/minlibd
There is also: https://github.com/kubo39/stm32f407discovery and its submodules.
 The STM32 demo only supports GDC right now, but I'll be 
 updating it to support LDC when 2.079.0 lands there.
Awesome. -Johan
Mar 10 2018
parent Richard <garlicbready googlemail.com> writes:
On Saturday, 10 March 2018 at 10:57:05 UTC, Johan Engelen wrote:
 On Saturday, 10 March 2018 at 07:54:33 UTC, Mike Franklin wrote:
 On Saturday, 10 March 2018 at 02:25:38 UTC, Richard wrote:
 Hi,
 I've been trying to see if I can get an mbed project to work 
 with Dlang
 basically compiling D code for use on a Cortex-M Proccessor
You might be interested in the following, if you're not already aware: * https://github.com/JinShil/stm32f42_discovery_demo * https://bitbucket.org/timosi/minlibd
There is also: https://github.com/kubo39/stm32f407discovery and its submodules.
 The STM32 demo only supports GDC right now, but I'll be 
 updating it to support LDC when 2.079.0 lands there.
Awesome. -Johan
That's interesting I've uploaded by own setup over here if anyone's interested * https://github.com/grbd/GBD.Dlang.MbedBlinkyTest
Mar 10 2018
prev sibling parent reply Richard <garlicbready googlemail.com> writes:
On Saturday, 10 March 2018 at 07:54:33 UTC, Mike Franklin wrote:
 On Saturday, 10 March 2018 at 02:25:38 UTC, Richard wrote:
 Hi,
 I've been trying to see if I can get an mbed project to work 
 with Dlang
 basically compiling D code for use on a Cortex-M Proccessor
You might be interested in the following, if you're not already aware: * https://github.com/JinShil/stm32f42_discovery_demo * https://bitbucket.org/timosi/minlibd The STM32 demo only supports GDC right now, but I'll be updating it to support LDC when 2.079.0 lands there. 2.079.0 removes some coupling of the compiler to the runtime, so I should be able to avoid the following bugs: https://github.com/ldc-developers/ldc/issues/created_by/JinShil
 so I tried this instead
 ```
 extern (C) int _d_run_main(int argc, char **argv, void* 
 mainFunc) {
     MainFuncType mFunc = cast(MainFuncType) mainFunc;
     return mFunc(null);
 }
 ```

 but nope that didn't seem to work ether, compiles okay but the 
 code in main() (D space) isn't called
 I'd imagine this should be a simple thing, anyone got any 
 ideas?
The following worked fine for me on my x64 Linux desktop with LDC version 1.8.0 (DMD v2.078.3, LLVM 5.0.1) ``` main.d import core.stdc.stdio; private alias extern(C) int function(char[][] args) MainFuncType; extern (C) int _d_run_main(int argc, char **argv, void* mainFunc) { MainFuncType mFunc = cast(MainFuncType) mainFunc; return mFunc(null); } void main() { printf("Hello, World!\n"); } ``` Compile with: ldc2 -defaultlib= -debuglib= -betterC main.d Mike
Based on the above this seems to work fine so I'll use this since it's the simplest option. ``` extern(C) int main(int argc, char** argv) { return d_main(); } int d_main() { // Do stuff } ``` This compiles but main() is never called btw ``` import core.stdc.stdio; private alias extern(C) int function(char[][] args) MainFuncType; extern (C) int _d_run_main(int argc, char **argv, void* mainFunc) { MainFuncType mFunc = cast(MainFuncType) mainFunc; return mFunc(null); } int main() { // Do stuff } ``` I tried compiling with ldc2 -defaultlib= -debuglib= -mtriple=thumb-none-linux-eabi -mcpu=cortex-m3 --od=. -c -betterC main.d
Mar 10 2018
parent Uknown <sireeshkodali1 gmail.com> writes:
On Saturday, 10 March 2018 at 12:00:12 UTC, Richard wrote:
 On Saturday, 10 March 2018 at 07:54:33 UTC, Mike Franklin wrote:
 On Saturday, 10 March 2018 at 02:25:38 UTC, Richard wrote:
[snip] Based on the above this seems to work fine so I'll use this since it's the simplest option. ``` extern(C) int main(int argc, char** argv) { return d_main(); } int d_main() { // Do stuff } ```
You can simplify it further: ``` extern(C) int main(int argc, char** argv { //Do stuff } ```
 This compiles but main() is never called btw
 ```
 import core.stdc.stdio;

 private alias extern(C) int function(char[][] args) 
 MainFuncType;
 extern (C) int _d_run_main(int argc, char **argv, void* 
 mainFunc)
 {
     MainFuncType mFunc = cast(MainFuncType) mainFunc;
     return mFunc(null);
 }

 int main() {
   // Do stuff
 }
 ```
 I tried compiling with
 ldc2 -defaultlib= -debuglib= -mtriple=thumb-none-linux-eabi 
 -mcpu=cortex-m3 --od=. -c -betterC main.d
I think you should not put `-betterC` since you are trying to use _d_run_main, which is only called when in regular mode.
Mar 10 2018