Skip to content
Advertisement

Is there a way to automatically link all libraries an OpenGL program needs, without explicitly writing their flags on compile?

For an entry level program, I’m currently compiling with

g++ manip.c -o manip -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -ldl -lXinerama -lXcursor -lGLEW

It was a very time-consuming process of trial and error figuring out which flags were necessary, googling the ‘undefined references to’, copy pasting every flag I came across and then deleting flags one by one until I had removed those I didn’t need.

For a beginner who is learning the ropes, compiling example programs in tutorials (that don’t have instructions on how to compile), is there a way to automatically link everything the program might need? Perhaps a way to move the libraries so they don’t have to be explicitly linked?

If not, is there a way to simplify the flag adding process? (I can envisage the command becoming incredibly long from all the flags as the program grows in complexity.)

Advertisement

Answer

I’m writing this answer for someone at the level I’m at.

You apparently can write makefiles that will automatically detect what libraries should be linked. The syntax for doing so is way out of my depths, but I’ll edit this in future if I advance to that.

To simplify the compiling option, you could write a makefile that links every single library you might need. The example I’ve included below has the libraries I’ve found out I require so far and may need to be extended significantly as I develop the OpenGL program further.

Call the file ‘Makefile’. Run it using the command make.

So vim Makefile

Inside the file, I have this:

manip: manip.c
        g++ manip.c -o manip -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -ldl -lXinerama -lXcursor -lGLEW -lglfw3 -lGL -lm -lXi

The term before the : on the first line will be the output executable. The term after the : on the first line is the C/C++ file the executable depends on. The next line is what the Makefile will execute if it detects the source code (after : on the first line) has changed.

If you have a more extensive set of files that depend on each other, you can add them below with the same syntax (google this if relevant to you).

So I now edit the manip.c file, run the command make, and manip is generated for me.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement