1

In my project directory, i have:

  • ./external/glew, which has glew compiled from source (ran make)
  • ./external/glfw, which has glfw also compiled from source (ran make x11)

in my .c source code:

#include <stdio.h>
#include <stdlib.h>

#include "external/glew/include/GL/glew.h"
#include "external/glfw/include/GL/glfw.h"

i tried to compile using GCC:

gcc test1.c -o test1 -DGLEW_STATIC -L./external/glew/lib -lGLEW -lGLU -lGL \
-L./external/glfw/lib/x11 -lglfw

./external/glew/lib is where the libGLEW.a is and ./external/glfw/lib/x11 is where the libglfw.a is.

and it compiles without error. but then i try to run ./test1 it gives me:

./test1: error while loading shared libraries: libGLEW.so.1.6: cannot
open shared object file: No such file or directory

how do i compile glew & glfw statically?

EDIT 1 Thanks for the help guys. After some help from people in stackoverflow and old nabble I manage to write it down what needs to be done to statically linked GLFW and GLEW and put it on http://www.phacks.net/static-compile-glfw-and-glew/

pixelblender
  • 181
  • 3
  • 4
  • 8

1 Answers1

1

Static libraries are not linked with -l… but just added to the linker source files. However please double check you really want to link those statically. The problem you have here is that the dynamic linker on *nix systems will by default only look into the system library directories and the path specified in the LD_LIBARY_PATH environment variable.

However it is possible to add relative linker paths to the executable, where libraries are located, too (--rpath linker option). That way you can ship the libraries in a directory relative to your executable, independently from the system libraries. If you do this, you also should look into binreloc

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • thanks for the info datenwolf! if i just added the .a files, it will throws a bunch of unresolved symbols / "undefined references". how do i fix those? yes i guess using rpath with relative path to the executable gives me the flexibility to link to shared libs. – pixelblender Aug 21 '11 at 15:18
  • @pixelblender: Which symbols exactly? One main difference between static and dynamic libraries is, that dynamic libraries may dynamically link other dynamically libraries, so that all dependencies are silently resolved. Static libraries however require you to manually add any libraries (static or dynamic) to your program linkage, that the static library requires. Technically static libraries (`.a` files) are just a bunch of object files (`.o`) in a common archive (that's what the *a* stands for), so you must treat them as if they were results of your program's compile process. – datenwolf Aug 21 '11 at 15:43
  • 1
    @AmigableClarkKant: If the only variant of a library present is in the form of an archive (.a), then `ld` will use that one. However most, if not all libraries are installed as shared libraries. To enforce static linkage of only a particular library, that's also present dynamically, one passes the filename directly, as the `-static` option to `ld` will link *all* libraries statically. – datenwolf Oct 08 '12 at 22:14