library not found for -lpng, clang: error: linker command failed

I am trying to make a c++ program using fltk library and some additional files, but i am getting this error again and again,

i used the command shown in the picture and have checked that i have libpng file installed. I can't figure out how to solve this issue. Pls help, this is the first time i have installed a library for the 12th chapter in Bjarne's 2nd edition of "Programming: Principles and Practices using C++". I am stuck on this linking issue for days and can't get my program to run.

The program i am trying to run is this:

#include "Simple_window.h"
#include "Graph.h"

int main()
{
    using namespace Graph_lib;
   
    Point tl {100,100};
   
    Simple_window win {tl,600,400,"Canvas"};
   
   
    Polygon poly;
   
    poly.add(Point{300,200});
    poly.add(Point{350,100});
    poly.add(Point{400,200});
   
    poly.set_color(Color::red);
   
    win.attach (poly);
   
    win.wait_for_button();
}

I am getting the same issue in Xcode.

How did you install fltk?

Homebrew is probably the best option for you to play with. Assuming you used brew install fltk then dependencies like libpng will be automatically installed and configured for you.

g++ -w -Wall -std=c++11 Graph.cpp Window.cpp GUI.cpp MY_CPP_FILE.cpp fltk-config --ldflags --use-images -o MY_EXECUTABLE will compile with updated PPP2 code that fixes known issues.

eg check https://github.com/BjarneStroustrup/Programming-_Principles_and_Practice_Using_Cpp/pull/13/commits/ccb56864ac2a932bb85ef27699226b172ad796f9

i installed using homebrew to make the program on x code, but i faced the same linking issue there so i installed fltk 1.4 separately and tried to make the program using Make. I first tried to make my program using cmake and x code as described here : https://github.com/fltk/fltk/blob/master/README.macOS.md#build_cmake_make, but after completing all the steps up till building fltk, i couldn't figure out what to do with the CMakeLists.txt file or how to link my new project with fltk. After that i tried Homebrew and was able to successfully link my program with fltk but then this linking issue occured and i got stuck. Now, even with updating the code using the github page using you mentioned, i am getting the same issue. Can you tell me in which directory should i be in when i execute this command, if i installed fltk using Homebrew? What was the procedure you followed if you also did this exercise?

Historically I've built from source and maintained third party packages in each project (that's overkill for you) which allowed me to manage dependencies separately for each project and never install anything.

Didn't actually do the exercise just randomly picked this question to answer while reviewing past couple of weeks of mostly unanswered questions.

For this question my steps were:

You'll probably need to clean out what you have manually built from source and installed. Homebrew will manage fltk dependences for you.

brew deps fltk
jpeg-turbo
libpng

fltk-config provides information about the installed library:

fltk-config
Usage: fltk-config [OPTIONS]
Options:
	[--version]
	[--api-version]

Options telling what we are doing:
	[--use-gl]    use GL
	[--use-images]  use extra image formats (PNG, JPEG)
	[--use-glut]   use glut compatibility layer
	[--use-forms]   use forms compatibility layer
	[--use-cairo]   use cairo graphics lib

Options telling what information we request:
	[--cc]      return C compiler used to compile FLTK
	[--cxx]      return C++ compiler used to compile FLTK
	[--optim]     return compiler optimization used to compile FLTK
	[--cflags]    return flags to compile C using FLTK
	[--cxxflags]   return flags to compile C++ using FLTK
	[--ldflags]    return flags to link against FLTK
	[--ldstaticflags] return flags to link against static FLTK library
                     even if there are DSOs installed
	[--libs]     return FLTK libraries full path for dependencies
	[--prefix]    return FLTK install time --prefix directory
	[--includedir]  return FLTK install time include directory

Options to compile and link an application:
	[-g]       compile the program with debugging information
	[-Dname[=value]] compile the program with the given define
	[--compile program.cxx]
    [--post program] prepare the program for desktop use
fltk-config --ldflags --use-images 
-L/usr/local/Cellar/fltk/1.3.8_1/lib -lfltk_images -lpng -lz -ljpeg -lfltk -lpthread -framework Cocoa

Note that Homebrew installs into /usr/local on Intel and /opt/homebrew on Apple Silicon.

library not found for -lpng, clang: error: linker command failed
 
 
Q