Preventing multiple #define when including headers

后端 未结 3 994
暖寄归人
暖寄归人 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 09:07

    You should have the #define STB_IMAGE_IMPLEMENTATION macro definition in exactly one source file that includes (a .cpp file, not in a header).

    Since you can also only have one source file that defines main(), it is simple to put the #define in the same file as main() (as long as it also includes ), but it can be used in any other source file if you prefer. You could even create a source file stb_image_imp.cpp that contains just the two lines shown, and link that into your program (or library) too.

    All other source files in the project should only include without also defining the macro.

提交回复
热议问题