Preventing multiple #define when including headers

后端 未结 3 1006
暖寄归人
暖寄归人 2021-01-27 08:07

coming from python and am a bit tripped up on what the proper approach to this is.

I am trying to include this library in my project: https://github.com/nothings/stb/blob

3条回答
  •  醉酒成梦
    2021-01-27 08:42

    Create a cpp file (or whatever extension you are using for your source files) whose sole purpose is to have

    #define STB_IMAGE_IMPLEMENTATION
    
    #include  
    

    and don't forget to include this cpp file into your project so that it is compiled and the result is linked into your program. In all other places where you need something from this library, just include the stb_image.h header as usual.

    These "implementation" macros are a "trick" used by some library authors to make "installing" their library easy. The idea is that when a specific macro (chosen by the library authors) is defined before the header of that library is included, some code with the actual implementation will be added. That is why this must be in a source file instead of a header and only in one source file (otherwise you get multiple definitions for the same functions).

提交回复
热议问题