Sorry for the boring topic. I'm writing a side project for fun using Glew and Glfw. And I encounter an external compiler error.
A test file who reproduces the error.
const
GLFW_CONTEXT_VERSION_MAJOR = 0x00022002
GLFW_CONTEXT_VERSION_MINOR = 0x00022003
GLFW_OPENGL_PROFILE = 0x00022008
GLFW_OPENGL_CORE_PROFILE = 0x00032001
proc glfwInit*():cint {.header: "GLFW/glfw3.h".}
proc glfwCreateWindow*(width, height:int, title:cstring = "GLFW Window", monitor:pointer = nil,
share:pointer = nil):pointer {.header: "GLFW/glfw3.h".}
proc glfwMakeContextCurrent*(window:pointer) {.header: "GLFW/glfw3.h".}
proc glfwSwapInterval*(interval:int) {.header: "GLFW/glfw3.h".}
proc glfwSwapBuffers*(window:pointer) {.header: "GLFW/glfw3.h".}
proc glfwPollEvents*() {.header: "GLFW/glfw3.h".}
proc glfwWindowShouldClose*(window:pointer):cint {.header: "GLFW/glfw3.h".}
proc glfwDestroyWindow*(window:pointer) {.header: "GLFW/glfw3.h".}
proc glfwTerminate*() {.header: "GLFW/glfw3.h".}
proc glfwWindowHint(hint:int, value:int) {.header: "GLFW/glfw3.h".}
proc glewInit():char {.header:"GL/glew.h".}
proc glewGetErrorString(err:char):cstring {.header:"GL/glew.h".}
###############################################################################
var glfwErr = glfwInit()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
var winHandle = glfwCreateWindow(640, 480)
glfwMakeContextCurrent(winHandle)
var glewErr = glewInit()
echo glewGetErrorString(glewErr)
while bool(glfwWindowShouldClose(winHandle)) == false:
glfwSwapBuffers(winHandle)
glfwPollEvents()
glfwDestroyWindow(winHandle)
glfwTerminate()
The command
nim c --passL:'`pkg-config glew --static --cflags --libs`' --passL:'`pkg-config glfw3 --static --cflags --libs`' test.nim
The error
Error: execution of an external compiler program 'gcc -c -w -I ../Src/GlLoader/Glad -I/home/drit0/Programmes/Nim/nim-0.19.0/lib -o /home/drit0/.cache/nim/test_d/test.c.o /home/drit0/.cache/nim/test_d/test.c' failed with exit code: 1
In file included from /home/drit0/.cache/nim/test_d/test.c:12:
/usr/include/GL/glew.h:85:2: error: #error gl.h included before glew.h
#error gl.h included before glew.h
^~~~~
/usr/include/GL/glew.h:97:2: error: #error glext.h included before glew.h
#error glext.h included before glew.h
^~~~~
How the hell it is possible to include gl twice here ? In glew there is a flag called "glewExperimental" and I would try to set it to true but I don't know how to import that in Nim.
Thanks for the answer. I found a workaround by creating this c file who does the includes.
#ifndef C_INCLUDES
# define C_INCLUDES
# include "GL/glew.h"
# include "GLFW/glfw3.h" //glfw3 must be the last include
#endif
I added this command before the nim command to create a library.
gcc -Wall -c c_includes.c
In the nim test file I added {.link: "c_includes.o".} and I replaced all the header pragmas by {.importc.} then I run the nim command as in my previous post.
The program works as expected but I didn't compare the executable size with this solution because I'm not able to compile the first version of my test file. Feel free to answer if you don't like this workaround.
Yeah that is the right idea. I think you did it very complicated. I would just put:
{.emit:"""
#include "GL/glew.h"
#include "GLFW/glfw3.h" //glfw3 must be the last include
""".}
At the top of your main file.