executable do not run after static linking with library - makefile

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?

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

Related

Mac os makefile command to link dynamic library to an executable that are in different folders

my makefile has these entries:
build_with_dynamic: main_executable_module.o decision.o data_io.o data_stat.o data_process.so
gcc -Wall -Wextra -Werror main_executable_module.o decision.o data_io.o data_stat.o -L. data_process.so -o ../../build/program
data_process.so: data_process_fPIC.o data_stat_fPIC.o
gcc -Wall -Wextra -Werror data_process_fPIC.o data_stat_fPIC.o -shared -o data_process.so
My makefile and my shared library are located in project/src/main_module, while my executable is located in project/build
I want to create an executable in the specified folder and link it to the shared library. Currently running the executable gives me this message:
dyld: Library not loaded: data_process.so
Referenced from: /Users/user/project/build/./program
Reason: image not found
I want to become able to run the program correctly without moving anything from their current positions and only via makefile commands. I have tried this, but nothing changed:
build_with_dynamic: main_executable_module.o decision.o data_io.o data_stat.o data_process.so
gcc -Wall -Wextra -Werror main_executable_module.o decision.o data_io.o data_stat.o -L. data_process.so -Wl,-rpath,. -o ../../build/program
The program works fine if i create the executable in the same folder as the library. How should i correctly write the makefile gcc line so that i can run the program? I run the make command from the same folder the makefile is located in.

Error when cross compiling shared so which depends on another so

I am trying to cross compile my application for a arm based system.
I have 2 libraries compiled in the following way:
$ gcc -shared --sysroot=$DIR_PATH -o $LIBPATH/libfoo.so foo.o
$ gcc -shared --sysroot=$DIR_PATH -o $LIBPATH/libbar.so bar.o
A third library is compiled:
gcc -shared -o $LIBPATH/libfoobar.so --sysroot=$DIR_PATH -L$LIBPATH -Wl,rpath=$RUN_TIME_PATH foobar.o -lfoo -lbar
Then finally I compile a binary:
gcc -o app --sysroot=$DIR_PATH -L$LIBPATH -Wl,rpath=$RUN_TIME_PATH app.o -lfoobar
However when compiling app I get
warning: libfoo.so, needed by libfoobar.so, not found (try using -rpath or -rpath-link)
I believe you need to use -Wl,-rpath-link=$LIBPATH to tell the linker where to look to resolve runtime library references during the link operation.
More info can be found in the ld documentation: https://sourceware.org/binutils/docs-2.37/ld/Options.html

Boehm-Demers-Weiser garbage collector not finding lgc

I'm trying to compile a C file and I get this error:
gcc -g -ansi -pedantic -Wall -D_C_SOURCE=1 -DGC_BDW -static interp.o
scan.o utils.o main.o -L gc/.libs -lgc -lm -o joy
/usr/bin/x86_64-linux-gnu-ld: cannot find -lgc
I've already installed the Boehm-Demers-Weiser garbage collector.
Any ideas?
This is what I'm trying to compile: https://github.com/xieyuheng/joy
In joy-master/src I cloned and installed the repo of gc following this: https://github.com/ivmai/bdwgc

Impossible to link SDL/GLUT/GLM Ubuntu [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I actualy work on a openGL projet on my VM but i need openGL 4.5 so i have install ubuntu on my laptop with (GTX 870M) who is compatible 4.5 (i check with glxinfo). But my problem is after install gcc, build-essential, libglew-dev, freeglut3-dev, freeglut3 and SDL2. I can't make my projet i have error like undefined reference on « SDL_WasInit » , « glBegin », .... for all library installed...
i try with makefile like :
ifeq "$(shell uname)" "Darwin"
LIBGL= -framework OpenGL
else
LIBGL= -lGLU -lGL
endif
CXXFLAGS += `pkg-config glew --cflags` `sdl2-config --cflags` -g -W -Wall -Wno-unused-parameter -Wno-deprecated-declarations
LDFLAGS += `pkg-config glew --libs` `sdl2-config --libs` $(LIBGL)
all : main.exe
run : main.exe
./main.exe
main.exe : main.cpp *.h
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o$# main.cpp
sol : main_solution.exe
runs : main_solution.exe
./main_solution.exe
main_solution.exe : main_solution.cpp *.h
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $# main_solution.cpp
clean :
rm -f *.o *.exe
and with cmakefile like :
cmake_minimum_required(VERSION 3.3)
project(src)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lglut -lGLU -lGL -lGLEW -lm -lSDL2 -lSDL2main -Wall -g")
set(SOURCE_FILES
"""ALL SOURCE FILES"""")
add_executable(src ${SOURCE_FILES})
This projet (cmakefile and makefile) work fine on my virtual machine ...
I hope you can help me thx.
In CMake you use target_link_libraries to specify which libraries to use. In general you want to use find_package(…) to locate the configuration for a specific library, then use the variables introduced by it to link.
find_package(OpenGL)
add_executable(foo …)
target_link_libraries(foo ${OPENGL_gl_LIBRARY} …)
You should look into the cmake modules that are used by find_package to see what the names of the variables they configure are.

how to link static library into dynamic library in gcc

Under gcc (g++), I have compiled a static .a (call it some_static_lib.a) library. I want to link (is that the right phrase?) this .a file into another dynamic library (call it libsomeDyn.so) that I'm building. Though the .so compiles, I don't see content of .a under .so using nm command:
/usr/bin/g++ -fPIC -g -O2 -Wall -Werror -pipe -march=pentium3
-mtune=prescott -MD -D_FILE_OFFSET_BITS=64 -DLINUX -D_GNU_SOURCE -D_THREAD_SAFE -I../../../../../../../../ -I../../../../../../../..//libraries -Wl,-rpath,/usr/lib -o libsomeDyn.so some.o another.o some_static_lib.a -shared -Wl -x
-Wl,-soname,libsomeDyn.so
I do not see functions under some_static_lib.a under libsomeDyn.so. What am I doing wrong?
Static libraries have special rules when it comes to linking. An object from the static library will only be added to the binary if the object provides an unresolved symbol.
On Linux, you can change that behavior with the --whole-archive linker option:
g++ -Wl,--whole-archive some_static_lib.a -Wl,--no-whole-archive
For every one that comes across that problem like me (and has not understand the answer properly): here is a short howto generate a dynamic library (libmylib.so) from a static one (mylib.a):
1.) create a mylib.c file that only imports the mylib.h file
2.) compile this mylib.c to mylib.o with
gcc -c -fPIC mylib.c -o mylib.o
3.) generate a dynamic library with the following command:
gcc --whole-archive -shared -Wl,-soname,libmylib.so -o libmylib.so mylib.o mylib.a
That worked at least for me, turning a static library (compiled with -fPIC) to
a dynamic library. I'm not sure wether this will work for other libraries too.

Resources