linking static library instead of dynamic - c++

I have a directory named FOO which has X.a and X.so . I want to choose only X.a.
So in my makefile I am using the following to link it
EXE: OBJ.o
g++ -L/PATH/TO/FOO -o $# $< -static -lX
But when I do that, I get the following error
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
How should this be done properly. One way is to give abs path of X.a. Is there some other way?

Try this:
EXE: OBJ.o
g++ -L/PATH/TO/FOO -o $# $< -Wl,-Bstatic -lX -Wl,-Bdynamic

If I'm reading the documentation for ld correctly, you don't have to specify the absolute path, but you can specify a library using its full name as such:
EXE: OBJ.o
g++ -L/PATH/TO/FOO -o $# $< -l:X.a

Related

ld: unrecognized -a option `tic-libstdc++' when using -static-libstdc++ in Makefile?

I am trying to link the "fstream" library to my kernel in C++.
But linker has to have -static-libstdc++ in order for this to work (And G++). But when I go to compile, it says:
ld: unrecognized -a option `tic-libstdc++'
PS: I typed the whole thing (-static-libstdc++)
Does anybody know why this is happening?
Here is my linker command:
ld -z max-page-size=0x1000 -Ttext=0x01000000 -static -Bsymbolic -static-libstdc++ -o $(TARGET) $(OBJS) build/GDT/GDTASM.o
-static-libstdc++ is a compiler option, not a linker option. You already set the linker to link the object files with static libraries (-static). Use -lstdc++:
ld -z max-page-size=0x1000 -Ttext=0x01000000 -static -Bsymbolic -o $(TARGET) $(OBJS) build/GDT/GDTASM.o -lstdc++
Libraries must be listed after object files.
It is better if you do not use ld:
g++ -static-libstdc++ -Wl,-z -Wl,max-page-size=0x1000 -Wl,-Ttext=0x01000000 -Wl,-Bsymbolic -o $(TARGET) $(OBJS) build/GDT/GDTASM.o

Undefined reference to function when using shared library in Linux

I have some problems to use a custom shared library, since I get undefined reference errors for some functions from two of many other source files.
If I compile the whole project with main file (release:), everything works just fine. But if I create a shared library (lib:) and use this library with the main file (all:), those compile time errors occur.
Here is a snippet of my makefile
release:
$(CC) -Wall -s -w $(INCLUDES) $(LIBRARY) $(SRC) mainfile.cpp $(OCV) $(BOOST) $(GLOG) $(GFLAGS) -o test.exe
lib:
$(CC) -fPIC $(INCLUDES) $(LIBRARY) -c $(SRC) $(OCV) $(BOOST) $(GLOG) $(GFLAGS)
mv *.o obj/
$(CC) -shared -o libOutput.so obj/*.o
all:
$(CC) -Wall -s -w $(INCLUDES) $(LIBRARY) mainfile.cpp -L/path/to/lib/ -lOutput $(OCV) $(BOOST) $(GLOG) $(GFLAGS) -o project.exe
Since there is no error during compilation using the release-option, I'm assuming that there is a linker specific problem.
I inspected the specific object files using GNU Binary Utilities
nm -C obj/specific.o | grep functionName
with no results. I did the same for the shared library, but this time with the following result,
U functionName(std::vector<int>)
which means that the function is unknown.
Do you have any suggestions, how to fix this issue?

/usr/bin/ld: cannot find : No such file or directory

I'm following this SDL tutorial to try and make use of some SDL extension libraries. My code is identical to theirs but I am still unable to make the file which leads me to believe the problem is in my makefile which looks like this:
CXX = g++
# Update these paths to match your installation
# You may also need to update the linker option rpath, which sets where to look for
# the SDL2 libraries at runtime to match your install
SDL_LIB = -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib, -lSDL2_image
SDL_INCLUDE = -I/usr/local/include
# You may need to change -std=c++11 to -std=c++0x if your compiler is a bit older
CXXFLAGS = -Wall -c -std=c++11 $(SDL_INCLUDE)
LDFLAGS = $(SDL_LIB)
EXE = SDL_Lesson3
all: $(EXE)
$(EXE): main.o
$(CXX) $< $(LDFLAGS) -o $#
main.o: main.cpp
$(CXX) $(CXXFLAGS) $< -o $#
clean:
rm *.o && rm $(EXE)
That makefile worked fine for previous examples. The only thing that has changed in this example is line 5 where I added -lSDL2_image as per the tutorial. When I try make the file I get the following traceback:
rony#comet:~/Documents/cpp/helloworld/lesson3$ make
g++ main.o -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib, -lSDL2_image -o SDL_Lesson3
/usr/bin/ld: cannot find : No such file or directory
collect2: error: ld returned 1 exit status
make: *** [SDL_Lesson3] Error 1
Is there an error with my makefile? Have I not installed the library correctly?
The problem is this rogue comma:
SDL_LIB = -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib, -lSDL2_image
^
causing the linker to look for libraries in a non-existent directory with an empty name, as well as /usr/local/lib. Removing the comma should fix it.

How should I link to LLVM libraries?

When I use command "gcc .. ../../*.so", there are the following error messages:
/usr/bin/ld: /home/demonwnb/build/src/*.so: error: undefined reference to 'llvm::raw_ostream::operator<<(void const*)'
/usr/bin/ld: /home/demonwnb/build/src/*.so: error: undefined reference to 'clang::DeclarationName::printName(llvm::raw_ostream&) const'
I think that I do not link "llvm library" correctly, so how should I do?
You need to tell your compiler where to load the libraries from, which can be done using the llvm-config command.
You could set the following symbols in your makefile
CC = g++
LLVM_MODULES = core jit native
CPPFLAGS = `llvm-config --cppflags $(LLVM_MODULES)`
LDFLAGS = `llvm-config --ldflags $(LLVM_MODULES)`
LIBS = `llvm-config --libs $(LLVM_MODULES)`
all:
$(CC) *.o $(LDFLAGS) $(LIBS) -o MyOutput
main:
find -name '*.cpp' -print0 | xargs -0 $(CC) -c $(CPPFLAGS)
Did you try using g++ to do the link? Those are C++ libraries and gcc doesn't pass the C++ libraries to the linker.

GCC Shared Library Problems

I'm trying to create a shared library on ubuntu using gcc
I just have one simple class(shared.h and shared.cpp) and one client to use it (main.cpp)
This is my makefile and I'm still not able to to get the program to compile.
all:
#compile object(fPIC: creates position independent code)
gcc -fPIC -Wall -g -c shared.cpp
#compile shared library
gcc -shared -Wl,-soname,libshared.so.1 -o libshared.so.1.0.1 shared.o -lc
#link shared library
gcc -g -o main main.cpp -L. -lshared
I'm confident the first line is correct
I am unsure what "-lc" does. I think it passes something to the linker?
I don't want to install the library, I just want to be able to link it from the current directory. I have tried: export LD_LIBRARY_PATH=.
but it does not seem to make a difference. Everything is in the current directory.
ERROR: /usr/bin/ld: cannot find -lshared
how do I get the compiler to check the current directory for my library?
The problem is not that it's not looking in the directory, the problem is that you've named the library "libshared.so.1.0.1". When you use -lshared, it's looking for a file named 'libshared.so' or 'libshared.a' in the library search path.
Most of the time, when using versioned system libraries, you'll provide a link to the latest one as 'libshared.so', even if you have installed 'libshared.so.1' or 'libshared.so.1.0.1'.
In your case, if you continue to leave the file named 'libshared.so.1.0.1', you'll want to create 2 symbolic links:
libshared.so - So that the library can be found using ld
libshared.so.1 - Since you declared the SO name as libshared.so.1 when building it, you need to provide this link, otherwise, the executable will not be able to find the proper shared library at runtime.
You don't write any dependencies, which is the purpose of Makefile-s. And you probably need to force the run path Perhaps something like
.PHONY: all clean
CXX=g++
CXXFLAGS=-g -Wall
all: main
main: main.o libshared.so
$(LINK.cpp) -o $# $< -Wl,-rpath,. -L. -lshared
libshared.so: shared.pic.o
$(LINK.cpp) -shared -o $^ $<
main.o: main.cc shared.hh
%.pic.o: %.cc
$(CXX) $(CXXFLAGS) -fPIC -c -o $# $<
#
clean:
rm -f *.o *.so main *~