Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work if target language is C++ #57

Open
NotoriousRebel opened this issue Sep 15, 2021 · 6 comments
Open

Does not work if target language is C++ #57

NotoriousRebel opened this issue Sep 15, 2021 · 6 comments

Comments

@NotoriousRebel
Copy link

Hello, I recently wanted to use this for a project I was working on. However, with the first example in the readme it works great when the target language is C but not when it's C++. I do not have a stack trace available as it does not provide one when compiling as C++.

@NotoriousRebel
Copy link
Author

For some more detail this is on Windows Version 20H2 and Nim 1.4.2. Also is x86 supported? Doesn't seem to compile if targeting x86.

@krux02
Copy link
Collaborator

krux02 commented Sep 15, 2021

I just tried the examples with C and C++. No problems on my machine. I however use Linux, not Windows. This library doesn't use ancy fancy trickery that would break x86, so that one should work out of the box.

Can you post the exact code that you used for testing and the generated C++ code? My first guess would be that it is a regression in the Nim compiler.

@krux02
Copy link
Collaborator

krux02 commented Sep 17, 2021

on my system, the generated c++ code for /tmp/scratch.nim, is stored under /home/arne/.cache/nim/scratch_d/@mscratch.nim.cpp. There should be an equivalent path on your system, too.

@krux02
Copy link
Collaborator

krux02 commented Sep 29, 2021

Since I don't have Windows, I can't reproduce the problem. Maybe somebody else can. Maybe you can reproduce the problem in an online compiler Nim compiler, so I might be able to address the problem there.

If I don't hear back from you I might close this issue as not reproduceable.

@NotoriousRebel
Copy link
Author

@krux02 Apologies for the late response here is the example code with error logs:

Here is the example code I am using:

The files math, vec, and quat are the nim files from the repo in nim-glm/glm folder.

import mat
import vec
import quat
import math

proc lookAtRH[T](eye,center,up:Vec3[T]): Mat4[T] =
    let
        f = normalize(center - eye)
        s = normalize(cross(f, up))
        u = cross(s,f)
    result = mat4[T](1.0)
    result.row0 = vec4( s,0)
    result.row1 = vec4( u,0)
    result.row2 = vec4(-f,0)
    result.arr[3] = vec4(-dot(s,eye), -dot(u,eye), dot(f,eye), 1)
    result[3,0] = -dot(s, eye)
    result[3,1] = -dot(u, eye)
    result[3,2] = dot(f, eye)

proc perspectiveRH[T]( fovy, aspect, zNear, zFar:T): Mat4[T] =
    let tanHalfFovy = tan(fovy / T(2))
    result = mat4[T](0.0)
    result[0,0] = T(1) / (aspect * tanHalfFovy)
    result[1,1] = T(1) / (tanHalfFovy)
    result[2,3] = T(-1)

    result[2,2] = -(zFar + zNear) / (zFar - zNear)
    result[3,2] = -(T(2) * zFar * zNear) / (zFar - zNear)

proc perspective*[T]( fovy, aspect, zNear, zFar:T):Mat4[T]=
    perspectiveRH(fovy, aspect, zNear, zFar)
proc lookAt*[T](eye,center,up:Vec3[T]):Mat4[T]=
    lookAtRH(eye, center, up)

proc Calculate(): int =
    var
        v = vec3(1.0, 5.0, 6.0)
        a = vec3(2.0, 2.0, 5.0)
        v4 = vec4(v, 1.0)
        c = cross(v,a)
        # m = rotate(mat4d(), 5.0, vec3(1.0, 0.0, 0.0))
        # r = v4 * c
    echo "a: "
    echo $a
    var
        eye = vec3(50.0, 50.0, 10.0)
        center = vec3(0.0)
        up = vec3(0.0, 1.0, 0.0)
        viewMatrix = lookAt(eye, center, up)
        # math.PI
        projectionMat = perspective(4/2, 1.0, 0.01, 100.0)

    echo viewMatrix * projectionMat
    # TODO replace result with matrix[1][1]
    result =  1

var res = Calculate()
echo $res

This is how I am compiling it:

nim c -r --d:release --opt:size --cpu:i386 .\calculate.nim

and the error logs:

In file included from C:\Users\john\nimcache\calculate_r\stdlib_dollars.nim.c:11:0:
C:\Users\john\.choosenim\toolchains\nim-1.4.2\lib/nimbase.h:271:35: error: static assertion failed: ""
 #define NIM_STATIC_ASSERT(x, msg) _Static_assert((x), msg)
                                   ^
C:\Users\john\.choosenim\toolchains\nim-1.4.2\lib/nimbase.h:542:1: note: in expansion of macro 'NIM_STATIC_ASSERT'
 NIM_STATIC_ASSERT(sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8, "");
 ^~~~~~~~~~~~~~~~~
In file included from C:\Users\john\nimcache\calculate_r\stdlib_assertions.nim.c:11:0:
C:\Users\john\.choosenim\toolchains\nim-1.4.2\lib/nimbase.h:271:35: error: static assertion failed: ""
 #define NIM_STATIC_ASSERT(x, msg) _Static_assert((x), msg)
                                   ^
C:\Users\john\.choosenim\toolchains\nim-1.4.2\lib/nimbase.h:542:1: note: in expansion of macro 'NIM_STATIC_ASSERT'
 NIM_STATIC_ASSERT(sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8, "");
 ^~~~~~~~~~~~~~~~~
Error: execution of an external compiler program 'gcc.exe -c  -w -fmax-errors=3 -mno-ms-bitfields -masm=intel -Os -fno-ident   -IC:\Users\john\.choosenim\toolchains\nim-1.4.2\lib -IC:\NimMathTest -o C:\Users\john\nimcache\calculate_r\stdlib_dollars.nim.c.o C:\Users\john\nimcache\calculate_r\stdlib_dollars.nim.c' failed with exit code: 1

When compiling for x64 and the target language is C the program works just fine.

nim cpp -r --d:release --opt:size .\calculate.nim

When the target language is C++ this is the error log (all I see):

Error: execution of an external program failed: 'C:\NimMathTest\calculate.exe'

I will test on Linux sometime this week.

@krux02
Copy link
Collaborator

krux02 commented Oct 1, 2021

Can you post the content of these three files from your system after you got the problem:

  • C:\Users\john\.choosenim\toolchains\nim-1.4.2\lib\nimbase.h
  • C:\Users\john\nimcache\calculate_r\stdlib_assertions.nim.c
  • C:\Users\john\nimcache\calculate_r\stdlib_dollars.nim.c

I think that should be enough for me to remote diagnose the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants