nanovg icon indicating copy to clipboard operation
nanovg copied to clipboard

Separation of header and c files ?

Open ejahandar opened this issue 8 years ago • 10 comments

Hi, i can't figure out why nanovg_gl.h contains both definitions and declarations together, if i want to use nvgCreateGLES2 on multiple files so i have to include nanovg_gl.h (with -DNANOVG_GLES2_IMPLEMENTATION) in each files, now the problem occurs, there are so many files which have nvgCreateGLES2 compiled in and they're conflicting with each other in link time. For the moment, i have separated definition and declarations in two separate file, am i correct?

ejahandar avatar Dec 14 '16 12:12 ejahandar

i think the idea is that you only have the implementation in one file, ie get rid of the -DNANOVG_GLES2_IMPLEMENTATION, then pick one cpp file where you do:

#define NANOVG_GLES2_IMPLEMENTATION
#include "nanovg_gl.h"

every else you just include the header. (or... something like that :) )

kritzikratzi avatar Dec 15 '16 19:12 kritzikratzi

I agree with one file for implementation, but combining both header and source file will make problems if you need using nvgCreateGLES2 on multiple files say multiple surfaces.

ejahandar avatar Dec 20 '16 04:12 ejahandar

The one file idea is taken from stb libraries [1]. You should have only one file in your project where you define the implementation as @kritzikratzi mentioned.

[1] https://github.com/nothings/stb

memononen avatar Dec 20 '16 13:12 memononen

Ran into the same problem. Perhaps this should be mentioned in the README?

I am guessing the reason the stb approach is used is so GL lib wrangling is left up to user.

holocronweaver avatar Jan 19 '17 20:01 holocronweaver

what about using nvgluCreateFramebuffer (in nanovg_gl_utils.h) in multiple files, its impossible with this approach.

ejahandar avatar Mar 09 '17 11:03 ejahandar

Hello,

Sorry to reopen that old issue but I struggle a lot with the same problem.

Has anything changed regarding the inclusion of nanovg_gl.h in several .cpp ? I tried to write in a first .cpp :

#include <nanovg.h> #define NANOVG_GL3_IMPLEMENTATION #include <nanovg_gl.h>

There is only one .cpp file which define that as @kritzikratzi suggests and then in my other .cpp file, I only put :

#include <nanovg.h> #include <nanovg_gl.h>

And this leads to the compile-time error C3861 : ncgCreateGL3 identifier not found from that second .cpp file.

i already tried to fix that by declaring the ncgCreateGL3 function in my file without the definition, but the linker send a LNK2019 : unresolved external symbol for the definition.

Any idea of what is happening ?

KrankerApfel avatar Jun 22 '22 08:06 KrankerApfel

Dude, there is no ncgCreateGL3 symbol in nanovg, so this is probably just a typo. Also maybe check #634.

mulle-nat avatar Jun 22 '22 11:06 mulle-nat

@mulle-nat I mean nvgCreateGL3 it's not a copy-past and I make a mistake when I wrote it here, sorry.

KrankerApfel avatar Jun 22 '22 15:06 KrankerApfel

Try putting

#include <nanovg.h> 
#define NANOVG_GL3
#include <nanovg_gl.h>

into the second .cpp file, which doesn't define NANOVG_GL3_IMPLEMENTATION but does use nvgCreateGL3. This would solve the C3861 problem. The linker problem is some kind of goof in the setup of your project: e.g. the first .cpp that defines NANOVG_GL3_IMPLEMENTATION isn't really linked.

mulle-nat avatar Jun 22 '22 18:06 mulle-nat

#define NANOVG_GL3

Thanks for your help! I don't really understand what happened or why adding that definition worked, but it fixed my problem.

KrankerApfel avatar Jun 27 '22 13:06 KrankerApfel