title: G++: Linking a dynamic (dylib) library
body: <p>I've been recently trying to write an opengl program on Mac and deploy it as a console application and distribute it to other Mac users.</p>
  
  <p>Since the program is written in C++, I decided to use g++. My program requires the use of the OpenGL framework, GLFW, and GLEW. I have the GLFW and GLEW libraries in my /usr/local/lib folder and the header files in my /usr/local/include folder. My main file is called main.cpp. The GLFW dynamic loading library is called libglfw.3.2.dylib and the GLEW dynamic loading library is called libGLEW.2.0.0.dylib.</p>
  
  <p>When I compile in g++ using the following command from the directory of the main.cpp file:</p>
  
  <pre><code>g++ -arch x86_64 -framework OpenGL -I/usr/local/include -L/usr/local/lib  -o Main main.cpp -lglfw.3.2 -lGLEW.2.0.0
  </code></pre>
  
  <p>Everything compiles correctly and the program runs.</p>
  
  <p>However, this links the libraries through an absolute path: /usr/local/lib. This means that when I move my executable to another computer, it won't run. I need the dylibs to load on every computer. So I tried moving them to the folder that has my main.cpp file and using <code>$(CURDIR)</code> to link to my current directory:</p>
  
  <pre><code>g++ -arch x86_64 -framework OpenGL -I/usr/local/include -L'$(CURDIR)'  -o Main main.cpp -lglfw.3.2 -lGLEW.2.0.0
  </code></pre>
  
  <p>This doesn't work since for some reason it doesn't seem to recognize '$(CURDIR)' as the current directory or maybe it isn't a valid variable to use.</p>
  
  <p>I'm looking for a way to link my dylibs so that I can move my executable to any computer and have it work.</p>
  
  <p>Any help would be appreciated.</p>