Linking different #include statement to library?

  apple-m1, c++, libpng, macos

Previously I had been working with the intel chip MacBooks, and I’m not sure if that’s related to the problem I have now, but now I’m using a silicone chip, and that might change some libraries.

I’m trying to run some C++ code using a PNG package I learned about this summer. The dependencies are having a gcc compiler and having this png library, called libpng. I installed homebrew on my new MacBook (I had some issues with this, but now I am fairly sure I have the home folder /opt/homebrew in the right place.

ok. So that’s my computer’s context. As to the file.

What I am trying to do is compile the following test program, save-test.cpp into save-test.exe, which will then when I compile, spit out a PNG image, which then lets me know the set up was done correctly.

save-test.cpp #include’s PNGIMG.cpp. This is OK. The problem is when PNGImg.cpp tries to include png.h.

On my previous laptop, this was fine as is. I had the include statement in PNGImg as just “#include <png.h>”. Then when I used the following compile statement (I dont know the details of every instruction in that compile statement) everything went as expected.

g++ -std=c++11 -Wall --pedantic-errors save-test.cpp -lpng -o save-test.exe

First when I used that compile statement, terminal told me the program could not find png.h. I do not know where the C++ compiler was looking, this may be a problem with where homebrew installed stuff, or something related to the hardware im compiling on.

Anyway, I went and found in my dependencies where png.h literally is, and changed by include statement in PNGImg.cpp to “#include </opt/homebrew/include/png.h>”

Now, the compiler can find the header file, but it has linking problems to the rest of the library. I’m also reaching the limits of my computer troubleshooting knowledge, as if I compile without the -lpng flag (as screen capped below) then I get an error message about not being able to link to the library but this time with undefined symbols. And if I do compile with the -lpng flag, it gives me an error message it still can’t find the library: "library not found for -lpng"

I wonder if this is due to the fact that since now I changed where the compiler looks for png.h, it can’t find the rest of the library?

When I hit -v for further details, I got:

ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.

but I dont really know what this means. Should I have those directories that are empty be non-empty?

Source: Windows Questions C++