2

In my main.h file, I #include "skybox.h". In skybox.h, stb_image.h is included (the latest version from GitHub as of 29-1-2017). Unlike any other library I've encountered, before including stb_image.h the docs say to #define STB_IMAGE_IMPLEMENTATION. I have tried putting this before including stb_image.h (in skybox.h), before including skybox.h (in main.h), both, and none. None of them work, the linker outputs all the duplicates from between build/main.o and build/skybox.o.

ld: 33 duplicate symbols for architecture x86_64

is the error given when linking. Also, I am doing the #define from the header file stb_image.h, but have tried doing it from the source file, which does not help.

  • The notes in the header file says to define that preprocessor variable in exactly one source file to define the implementation. Is that what you did? How are you compiling and linking your project? Edit: it sounds like you're doing this in a header file, which would be included in multiple source files - that would be your problem. – Greg Kikola Jan 30 '17 at 01:09

1 Answers1

2

I finally figured out how to fix it. Instead putting #define STB_IMAGE_IMPLEMENTATION and #inlclude stb_image.h in the header file, you put them in the source file wherever they are used. If you put #define STB_IMAGE_IMPLEMENTATION in the header, it gets defined in all the files that include that header, causing the duplicate symbols error as when it is defined, stb_image gets reimplemented.

  • I don't understand. when compile, headers and cpps are all together.... Putting into source doesn't work at all. – Nicholas Jela Apr 30 '22 at 14:14
  • I think you should put a header guard at the beginning of your header files. It should be the absolute first thing on your header files like this: `#pragma once` or ifndef - define combination. If you don't have them you will re-define everything inside the header file each time. – Turgut Apr 27 '23 at 07:46