Search code examples
makefilestatic-librariesstatic-linking

executable do not run after static linking with library


In my Makefile, I have tried build a code static linking with a library from another project I have in my computer. I have this instruction:

game: Input.o Image.o Renderer.o Surface.o Main.o
    g++ -g -L ${sdl_library} -L ${lib_netpbm_library} -o release/game2d build/Input.o build/Image.o build/Renderer.o build/Surface.o build/Main.o -l:libnetpbm.a -l:libSDL2.a -l:libSDL2main.a -lGL -lGLEW -lm

It builds with no error or warning. But when I try run the generated executable, I got this error:

./game2d: error while loading shared libraries: libnetpbm.a: cannot open shared object file: No such file or directory

What I am missing here?


Solution

  • The issue was in fact in the way I was building the library. I change the Makefile from this:

    libnetpbm: ${obj_dir}/netpbm.o ${obj_dir}/bitmap.o ${obj_dir}/graymap.o ${obj_dir}/pixmap.o
        g++ -g -shared -o ${release_dir}/libnetpbm.so ${obj_dir}/netpbm.o ${obj_dir}/bitmap.o ${obj_dir}/graymap.o ${obj_dir}/pixmap.o -Wl,--out-implib,${release_dir}/libnetpbm.a
    

    to that:

    libnetpbm: ${obj_dir}/netpbm.o ${obj_dir}/bitmap.o ${obj_dir}/graymap.o ${obj_dir}/pixmap.o
        ar rcs ${release_dir}/libnetpbm.a ${obj_dir}/netpbm.o ${obj_dir}/bitmap.o ${obj_dir}/graymap.o ${obj_dir}/pixmap.o
        g++ -g -shared -o ${release_dir}/libnetpbm.so ${obj_dir}/netpbm.o ${obj_dir}/bitmap.o ${obj_dir}/graymap.o ${obj_dir}/pixmap.o