nimraylib_now icon indicating copy to clipboard operation
nimraylib_now copied to clipboard

physac `redefinition of 'struct Vector2'`

Open enthus1ast opened this issue 2 years ago • 6 comments

Another flavour of: #12

module.nim

import nimraylib_now

var body*: PhysicsBody

main.nim

import nimraylib_now
import module # <- this one contains a PhysicsBody
initWindow(20, 20,
           "raylib [texture] example - texture rectangle")
closeWindow()

# c:\Users\david\projects\nimPlayground\raylibBug\main.nim(2, 8) Warning: imported and not used: 'module' [UnusedImport]
# CC: module.nim
# CC: main.nim
# In file included from C:\Users\david\nimcache\main_d\@mmodule.nim.c:9:
# C:\Users\david\projects\nimraylib_now\src\nimraylib_now\mangled\raylib.h:188:16: error: redefinition of 'struct Vector2'
#   188 | typedef struct Vector2 {
#       |                ^~~~~~~
# In file included from C:\Users\david\nimcache\main_d\@mmodule.nim.c:8:
# C:\Users\david\projects\nimraylib_now\src\nimraylib_now\mangled\physac.h:119:16: note: originally defined here
#   119 | typedef struct Vector2 {
#       |                ^~~~~~~
# In file included from C:\Users\david\nimcache\main_d\@mmodule.nim.c:9:
# C:\Users\david\projects\nimraylib_now\src\nimraylib_now\mangled\raylib.h:191:3: error: conflicting types for 'Vector2'
#   191 | } Vector2;
#       |   ^~~~~~~
# In file included from C:\Users\david\nimcache\main_d\@mmodule.nim.c:8:
# C:\Users\david\projects\nimraylib_now\src\nimraylib_now\mangled\physac.h:122:3: note: previous declaration of 'Vector2' was here
#   122 | } Vector2;
#       |   ^~~~~~~
# Error: execution of an external compiler program 'gcc.exe -c  -w -fmax-errors=3 -mno-ms-bitfields -DWIN32_LEAN_AND_MEAN -Wall -D_DEFAULT_SOURCE -Wno-missing-braces -Werror=pointer-arith -fno-strict-aliasing -std=c99 -s -O1 -Werror=implicit-function-declaration -IC:\Users\david\projects\nimraylib_now\src\csources\raylib_mangled -IC:\Users\david\projects\nimraylib_now\src\csources\raylib_mangled\external\glfw\include -IC:\Users\david\projects\nimraylib_now\src\csources\raylib_mangled\external\glfw\deps\mingw -DPLATFORM_DESKTOP -DGRAPHICS_API_OPENGL_33 -DPHYSAC_IMPLEMENTATION -DPHYSAC_NO_THREADS -DRAYGUI_IMPLEMENTATION   -IC:\Users\david\.choosenim\toolchains\nim-1.6.0\lib -Ic:\Users\david\projects\nimPlayground\raylibBug -o C:\Users\david\nimcache\main_d\@mmodule.nim.c.o C:\Users\david\nimcache\main_d\@mmodule.nim.c' failed with exit code: 1

enthus1ast avatar Dec 02 '21 15:12 enthus1ast

When i look into the module c file i also see this include oder:

@mmodule.nim.c

/* Generated by Nim Compiler v1.6.0 */
/* Compiled for: Windows, amd64, gcc */
/* Command for C compiler:
   gcc.exe -c  -w -fmax-errors=3 -mno-ms-bitfields -DWIN32_LEAN_AND_MEAN -Wall -D_DEFAULT_SOURCE -Wno-missing-braces -Werror=pointer-arith -fno-strict-aliasing -std=c99 -s -O1 -Werror=implicit-function-declaration -IC:\Users\david\projects\nimraylib_now\src\csources\raylib_mangled -IC:\Users\david\projects\nimraylib_now\src\csources\raylib_mangled\external\glfw\include -IC:\Users\david\projects\nimraylib_now\src\csources\raylib_mangled\external\glfw\deps\mingw -DPLATFORM_DESKTOP -DGRAPHICS_API_OPENGL_33 -DPHYSAC_IMPLEMENTATION -DPHYSAC_NO_THREADS -DRAYGUI_IMPLEMENTATION   -IC:\Users\david\.choosenim\toolchains\nim-1.6.0\lib -Ic:\Users\david\projects\nimPlayground\raylibBug -o C:\Users\david\nimcache\main_d\@mmodule.nim.c.o C:\Users\david\nimcache\main_d\@mmodule.nim.c */
#define NIM_INTBITS 64

#include "nimbase.h"
#include "C:\Users\david\projects\nimraylib_now\src\nimraylib_now\mangled\physac.h"
#include "C:\Users\david\projects\nimraylib_now\src\nimraylib_now\mangled\raylib.h"
#undef LANGUAGE_C
#undef MIPSEB
#undef MIPSEL
#undef PPC
#undef R3000
#undef R4000
#undef i386
#undef linux
#undef mips
#undef near
#undef far
#undef powerpc
#undef unix
#define nimfr_(x, y)
#define nimln_(x, y)
typedef NI32 tyEnum_PhysicsShapeType__4XnIM9c0KtgiqP83K0bfZ2w;
typedef Vector2 tyArray__DNYpGxjnxL84RUmckj7N0w[24];
N_LIB_PRIVATE PhysicsBodyData* body_module_2;

enthus1ast avatar Dec 02 '21 15:12 enthus1ast

Now when I think of it, maybe there's a solution to do several #define statements that would disable every repeating import in other header files. For example your case is because of this in physac.h:

#if !defined(RL_VECTOR2_TYPE)
// Vector2 type
typedef struct Vector2 {
    float x;
    float y;
} Vector2;
#endif

and passing --passC:"-DRL_VECTOR2_TYPE" would fix the issue. I will do some experimentation later tonight or at the weekend. Thanks for bringing up this issue.

greenfork avatar Dec 03 '21 02:12 greenfork

Let's see if someone on the forum can help us https://forum.nim-lang.org/t/8704

greenfork avatar Dec 12 '21 14:12 greenfork

This is still an issue, please look into it. Using include instead of import is the only way to get around it. If module has an import nimraylib_now and uses physac functions, and main also has import nimraylib_now, then adding import module to main will cause a redefinition of ‘struct Vector2' error. Adding the --passC:"-DRL_VECTOR2_TYPE" flag causes it to never be defined at all: unknown type name ‘Vector2’.

the-argus avatar Feb 20 '23 17:02 the-argus

@greenfork I doubt this helps, because I don't really understand what's going on, but I tried wrapping all the declarations of struct Vector2 with an RL_VECTOR2_TYPE define check. This solved my original issue and produced a new one: every nim module in my project that includes nimraylib_now gives "multiple definition" errors for all of the physics functions. They look like this:

@mprogrammer_art.nim.c:(.text+0x0): multiple definition of `InitPhysics'; /home/argus/.cache/nim/dark_as_a_dungeon_d/@m..@s..@s..@[email protected]@spkgs@snimraylib_now-0.15.0@snimraylib_now@[email protected]:@m..@s..@s..@[email protected]@spkgs@snimraylib_now-0.15.0@snimraylib_now@[email protected]:(.text+0x0): first defined here

where programmer_art.nim is my file. All of the conflicts are with the produced physac.nim.c file.

Just to be clear: I am using raylib as a shared library because I'm on nixOS and the static one fetched via nimble doesn't work here.

the-argus avatar Feb 23 '23 20:02 the-argus

What goes on -- is the incorrect import order of C files https://github.com/greenfork/nimraylib_now/issues/12#issuecomment-835053185

If you already use a dynamic system library, I think the best solution would be to use a third-party physics library. Unfortunately I will need a major rewrite to workaround the issue of incorrect order of imports, it is unlikely to be fixed on the Nim side because when I raised this question, there was little help and no plans to fix it. I plan to do this re-write some time soon but so far it's better to use a third-party library.

You can use Nim's "converters" to convert Raylib types to the chosen library types automatically to make it seamless. Or you can make simple wrapping functions for better experience.

greenfork avatar Feb 24 '23 02:02 greenfork