简体   繁体   中英

Header only library doesn't use header guards. How can I use the library in other header files?

I'm working with a header only library ( Nuklear ). I include this in my main.c file like so:

#define SDL_MAIN_HANDLED
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>

#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_SDL_GL3_IMPLEMENTATION
#include "nuklear.h"
#include "nuklear_sdl_gl3.h"

I want to create a file to house some components and break the code up into smaller chunks. I tried to do this by including nuklear.h into one of the component headers but got a re declaration error:

error: redefinition of 'nk_sdl_shutdown'

I don't know any other way to access the types from nuklear.h in other header files. Any help please?

You can wrap the #include for that library in another header.

#ifndef NUKLEAR_INCLUDED
#define NUKLEAR_INCLUDED

#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_SDL_GL3_IMPLEMENTATION
#include "nuklear.h"
#include "nuklear_sdl_gl3.h"

#endif

It looks like it does include header guards now. Try updating the library. If that doesn't work file a bug or, even better, a pull request.

It's open source software, if you find something wrong you can fix it instead of trying to work around the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM