简体   繁体   中英

G++: Linking a dynamic (dylib) library

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.

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.

When I compile in g++ using the following command from the directory of the main.cpp file:

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

Everything compiles correctly and the program runs.

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 $(CURDIR) to link to my current directory:

g++ -arch x86_64 -framework OpenGL -I/usr/local/include -L'$(CURDIR)'  -o Main main.cpp -lglfw.3.2 -lGLEW.2.0.0

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.

I'm looking for a way to link my dylibs so that I can move my executable to any computer and have it work.

Any help would be appreciated.

The path given with -L is just for the linker to find extra libraries. Where these libraries are located at runtime is not controlled with -L . Instead you should look at something called rpath . There's an article on how to do it properly on MacOS X: https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/RunpathDependentLibraries.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM