www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Noob ImportC questions

reply John C. <example example.com> writes:
Hello everyone. Recently I get interested in some GUI-related 
stuff and I want to use Raylib in my project. I know about 
raylib-d, dray, etc. bindings, but I would like to see what can 
ImportC do for me in this situation.
I have following project hierarchy:
```
project/
   src/
     main.d
     raylib.c
   lib/
     raylib/
       include/
         raylib.h
       libraylib.a
```
main.d contents:
```
import raylib;

void main() {
     InitWindow(800, 600, "Hello, Raylib!");
     SetTargetFPS(60);
     while (!WindowShouldClose()) {
         BeginDrawing();
             ClearBackground(Color(245, 245, 245, 255));
             DrawText("Hello, Raylib!", 400, 300, 28, Color(200, 
200, 200, 255));
         EndDrawing();
     }
     CloseWindow();
}
```
raylib.c contents:
```
#include "../lib/raylib/include/raylib.h"
```
Raylib header file and library files were downloaded from Raylib 
GitHub releases page.
Here some questions about it and object file layout arised (it's 
important to mention that I'm newbie in D and programming at all):

1. Why both "ldmd2 main.d raylib.c -L../lib/raylib/libraylib.a" 
and "ldmd2 main.d -L../lib/raylib/libraylib.a" produce working 
executable? main.d code in second example does not have access to 
header inclusions, or this data is already available in library 
file?
2. Why main.d version with "Color(...)" replaced by "RAYWHITE" or 
"LIGHTGREY" (defined in header) will not compile with "undefined 
identifier" error?

Modified main.d:
```
import raylib;

void main() {
     InitWindow(800, 600, "Hello, Raylib!");
     SetTargetFPS(60);
     while (!WindowShouldClose()) {
         BeginDrawing();
             ClearBackground(RAYWHITE);
             DrawText("Hello, Raylib!", 400, 300, 28, LIGHTGRAY);
         EndDrawing();
     }
     CloseWindow();
}
```
Error (with both commands above, same result):
```
main.d(8): Error: undefined identifier `RAYWHITE`
main.d(9): Error: undefined identifier `LIGHTGRAY`
```
Feb 26
next sibling parent reply Kapendev <alexandroskapretsos gmail.com> writes:
On Wednesday, 26 February 2025 at 17:00:48 UTC, John C. wrote:
 Error (with both commands above, same result):
 ```
 main.d(8): Error: undefined identifier `RAYWHITE`
 main.d(9): Error: undefined identifier `LIGHTGRAY`
 ```
Both RAYWHITE and LIGHTGRAY are defines and ImportC was not able to parse them, so they don't exist. No idea about the first question.
Feb 26
parent reply John C. <example example.com> writes:
On Wednesday, 26 February 2025 at 18:53:40 UTC, Kapendev wrote:
 On Wednesday, 26 February 2025 at 17:00:48 UTC, John C. wrote:
 Error (with both commands above, same result):
 ```
 main.d(8): Error: undefined identifier `RAYWHITE`
 main.d(9): Error: undefined identifier `LIGHTGRAY`
 ```
Both RAYWHITE and LIGHTGRAY are defines and ImportC was not able to parse them, so they don't exist. No idea about the first question.
Yes, when I added new test simple value macro ("#define TESTM 123"), main.d was able to use it. So, this problem is connected to ImportC parsing capabilities? If it is, would removing some "hard to parse" constructs in header file help this situation?
Feb 27
parent reply John C. <example example.com> writes:
On Thursday, 27 February 2025 at 08:10:04 UTC, John C. wrote:
 would removing some "hard to parse" constructs in header file 
 help this situation?
I tried to change ``` #define LIGHTGRAY CLITERAL(Color){ 200, 200, 200, 255 } // Light Gray ``` To more simpler definition which does not contain other previously defined macros (such as CLITERAL above) ``` #define LIGHTGRAY (Color){ 200, 200, 200, 255 } // Light Gray ``` But this change was not helpful, same error occured. Does this mean that ImportC cannot parse object literals?
Feb 27
next sibling parent reply Kapendev <alexandroskapretsos gmail.com> writes:
On Thursday, 27 February 2025 at 08:16:33 UTC, John C. wrote:
 On Thursday, 27 February 2025 at 08:10:04 UTC, John C. wrote:
 But this change was not helpful, same error occured. Does this 
 mean that ImportC cannot parse object literals?
Seems like it has a hard time understanding structs. I tried something like `#define MY_COLOR struct Color {...}`, but got the same error.
Feb 27
next sibling parent Kapendev <alexandroskapretsos gmail.com> writes:
On Thursday, 27 February 2025 at 12:48:19 UTC, Kapendev wrote:
 On Thursday, 27 February 2025 at 08:16:33 UTC, John C. wrote:
 On Thursday, 27 February 2025 at 08:10:04 UTC, John C. wrote:
 But this change was not helpful, same error occured. Does this 
 mean that ImportC cannot parse object literals?
Seems like it has a hard time understanding structs. I tried something like `#define MY_COLOR struct Color {...}`, but got the same error.
Ignore my define lol.
Feb 27
prev sibling parent reply John C. <example example.com> writes:
On Thursday, 27 February 2025 at 12:48:19 UTC, Kapendev wrote:
 On Thursday, 27 February 2025 at 08:16:33 UTC, John C. wrote:
 On Thursday, 27 February 2025 at 08:10:04 UTC, John C. wrote:
 But this change was not helpful, same error occured. Does this 
 mean that ImportC cannot parse object literals?
Seems like it has a hard time understanding structs. I tried something like `#define MY_COLOR struct Color {...}`, but got the same error.
I have found YouTube video about ImportC and Raylib, author writes many constants to make ImportC parse colors successfully. His raylib.c looked like following: ``` #include "../lib/raylib/include/raylib.h" const Color lightgray = LIGHTGRAY; const Color gray = GRAY; const Color darkgray = DARKGRAY; const Color yellow = YELLOW; ... ``` After applying this modification to raylib.c, all code I have tested worked perfectly.
Feb 27
parent John C. <example example.com> writes:
On Thursday, 27 February 2025 at 13:33:55 UTC, John C. wrote:
 all code I have tested worked perfectly.
Of course, I had to rewrite all "RAYWHITE" to "raywhite", etc., but I'm happy to have working GUI library now!
Feb 27
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On Thursday, 27 February 2025 at 08:16:33 UTC, John C. wrote:
 On Thursday, 27 February 2025 at 08:10:04 UTC, John C. wrote:
 would removing some "hard to parse" constructs in header file 
 help this situation?
I tried to change ``` #define LIGHTGRAY CLITERAL(Color){ 200, 200, 200, 255 } // Light Gray ``` To more simpler definition which does not contain other previously defined macros (such as CLITERAL above) ``` #define LIGHTGRAY (Color){ 200, 200, 200, 255 } // Light Gray ``` But this change was not helpful, same error occured. Does this mean that ImportC cannot parse object literals?
#define constants translated to enums are very limited in importC. Numbers and strings work. These color defines do not work. In raylib-d the color defines are all translated by hand. https://dlang.org/spec/importc.html#defines -Steve
Feb 28
prev sibling next sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Wednesday, 26 February 2025 at 17:00:48 UTC, John C. wrote:
Why both "ldmd2 main.d raylib.c -L../lib/raylib/libraylib.a" and 
"ldmd2 main.d -L../lib/raylib/libraylib.a" produce working 
executable?
I think `import raylib` finds raylib.c
Feb 26
parent John C. <example example.com> writes:
On Wednesday, 26 February 2025 at 21:08:19 UTC, monkyyy wrote:
 I think `import raylib` finds raylib.c
Thank you, it were actually using raylib.c in both cases. When I renamed this file, import was made impossible (I think it should have happened?)
Feb 26
prev sibling parent John C. <example example.com> writes:
On Wednesday, 26 February 2025 at 17:00:48 UTC, John C. wrote:
 2. Why main.d version with "Color(...)" replaced by "RAYWHITE" 
 or "LIGHTGREY" (defined in header) will not compile with 
 "undefined identifier" error?
I have tested some basic Raylib functionality from examples on their website and seems that only colors are not available.
Feb 27