Linking dynamic library to my project - c++

After reading lots of post I am really confused.
I want to link a dynamic library to my cpp code.
The library is in /usr/local/include/sbml
and the libsbml.so file can be found in /usr/local/lib
I have a makefile that looks like this
SHELL = /bin/sh
VERSION = 5.04.02
CC = /usr/bin/g++
CFLAGS = -Wall -g -D_REENTRANT -DVERSION=\"$(VERSION)\"
LDFLAGS = -lsbml
OBJ = main.o SBML.o
prog: $(OBJ)
$(CC) $(CFLAGS) -o prog $(OBJ) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $<
.PHONY : clean
clean :
-rm edit $(OBJ)
If I run the makefile I get the following error: (undefined reference to SBMLReader::readSBMLFromFile())
g++ -c -o SBML.o SBML.cpp
/usr/bin/g++ -Wall -g -D_REENTRANT -DVERSION=\"5.04.02\" -o prog main.o SBML.o -lsbml
sbml.o: In Funktion `SBML::readSBML()':
sbml.cpp:(.text+0x129): Nicht definierter Verweis auf `SBMLReader::readSBMLFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status

The library was not linked because you need to specify the folder containing the so-file as well:
LDFLAGS = -L/usr/local/lib -lsbml
Now it should link and your unresolved symbol should be gone as well.

Related

Using pthread in a MPI program has a Compilation error

I'm trying to run an MPI program that uses pthreads. I am able to compile and run in my local machine but couldn't do that on a server.
This is the command I used to compile.
target1: TARGET=main
target2: TARGET=kmer_finaliser
CC = mpic++
# CPPFLAGS = -pg
# CPPFLAGS = -lm -g -Wall -pthread
CPPFLAGS = -std=c++11 -pthread
USER_LIBS = -I /home/ruchin/sparsehash-sparsehash-2.0.4/src
main: main.o extractor.o com.o kmer_dump.o thread_pool.o
$(CC) $(CPPFLAGS) -o kmer_counter.out main.o extractor.o com.o kmer_dump.o thread_pool.o $(USER_LIBS)
main.o: main.cpp extractor.h com.h kmer_dump.h thread_pool.h
$(CC) $(CPPFLAGS) -c main.cpp $(USER_LIBS)
kmer_finaliser: kmer_finaliser.o kmer_dump.o
$(CC) $(CPPFLAGS) -o kmer_finaliser.out kmer_finaliser.o kmer_dump.o $(USER_LIBS)
kmer_finaliser.o: kmer_finaliser.cpp kmer_dump.h
$(CC) $(CPPFLAGS) -c kmer_finaliser.cpp $(USER_LIBS)
extractor.o: extractor.cpp extractor.h
$(CC) $(CPPFLAGS) -c extractor.cpp $(USER_LIBS)
com.o: com.cpp com.h
$(CC) $(CPPFLAGS) -c com.cpp $(USER_LIBS)
kmer_dump.o: kmer_dump.cpp kmer_dump.h
$(CC) $(CPPFLAGS) -c kmer_dump.cpp $(USER_LIBS)
thread_pool.o: thread_pool.cpp kmer_dump.h
$(CC) $(CPPFLAGS) -c thread_pool.cpp $(USER_LIBS)
This is the error I get.
/usr/bin/ld: thread_pool.o: undefined reference to symbol 'pthread_create##GLIBC_2.2.5'
/lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
According to the man page of g++, you should include also in the compilation the flag -pthread:
-pthread
Define additional macros required for using the POSIX threads
library. You should use this option consistently for both
compilation and linking. This option is supported on
GNU/Linux targets, most other Unix derivatives, and also on
x86 Cygwin and MinGW targets.

recompiling with -fPIE flag could not resolve error

I am trying to compile this app using its makefile on WSL. But I get the following error:
g++ -g -Wall -ansi pendulumSystem.o TimeStepper.o particleSystem.o ClothSystem.o simpleSystem.o camera.o main.o vecmath/src/Vector3f.o vecmath/src/Vector2f.o vecmath/src/Matrix3f.o vecmath/src/Matrix4f.o vecmath/src/Vector4f.o vecmath/src/Quat4f.o vecmath/src/Matrix2f.o -o a3 -L. -lRK4 -lglut -lGL -lGLU
/usr/bin/ld: ./libRK4.a(RK4.o): relocation R_X86_64_32 against symbol `__gxx_personality_v0##CXXABI_1.3' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: ./libRK4.a(RK4.o): relocation R_X86_64_PC32 against symbol `_Znwm##GLIBCXX_3.4' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make: *** [Makefile:15: a3] Error 1
I tried editing my makefile to include the flag like below, but it still gives me the same error message.
INCFLAGS = -I vecmath/include
INCFLAGS += -I /usr/include/GL
LINKFLAGS = -L. -lRK4 -lglut -lGL -lGLU
CFLAGS = -g -Wall -ansi -fPIE
CC = g++
SRCS = $(wildcard *.cpp)
SRCS += $(wildcard vecmath/src/*.cpp)
OBJS = $(SRCS:.cpp=.o)
PROG = a3
all: $(SRCS) $(PROG)
$(PROG): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $# $(LINKFLAGS)
.cpp.o:
$(CC) $(CFLAGS) $< -c -o $# $(INCFLAGS)
depend:
makedepend $(INCFLAGS) -Y $(SRCS)
clean:
rm $(OBJS) $(PROG)
Could it be that the provided libRK4.a file was not compiled with the -fPIE flag?

aws-sdk-cpp: unresolved symbols

I am trying to build a simple example using aws sdk cpp. But I am stumbled on a building step. I am linking libaws-cpp-sdk-s3.so library, which is supposed to have all symbols from the source file. But the linker cannot even find a couple of them. The source file is:
#include <aws/core/Aws.h>
int main( int argc, char ** argv)
{
Aws::SDKOptions options;
Aws::InitAPI(options);
{
// make your SDK calls here.
}
Aws::ShutdownAPI(options);
return 0;
}
by using this Makefile:
CC = g++
CFLAGS = -g -c -Wall -std=c++11
LDFLAGS = -g
EXECUTABLE = ex1
RM = rm -f
SOURCES = main.cpp
OBJS = $(SOURCES:.cpp=.o)
all: $(EXECUTABLE)
$(EXECUTABLE): main.o -laws-cpp-sdk-s3
$(CC) $(LDFLAGS) main.o -o $#
main.o: main.cpp
$(CC) $(CFLAGS) $^ -o $#
.PHONY: clean
clean:
$(RM) $(EXECUTABLE) $(OBJS) $(SOURCES:.cpp=.d)
When I run make, I got this error. But why? I built
g++ -g main.o -o ex1
main.o: In function main':
/home/username/workspace/ex1/src/main.cpp:6: undefined reference toAws::InitAPI(Aws::SDKOptions const&)'
/home/username/workspace/ex1/src/main.cpp:12: undefined reference to `Aws::ShutdownAPI(Aws::SDKOptions const&)'
collect2: error: ld returned 1 exit status
Makefile:13: recipe for target 'ex1' failed
make: *** [ex1] Error 1
I don't see where you are linking libaws-cpp-sdk-core
You probably need:
$(EXECUTABLE): main.o -laws-cpp-sdk-s3 -laws-cpp-sdk-core
$(CC) $(LDFLAGS) main.o -o $#

Statically link against pocketsphinx (Library)

I am developing a little program with pocketsphinx (speech to text - library).
On Windows i was using Code::Blocks as development environment and i had success to build a program.
Now i try to port my program to Linux and i am having little problems to link against pocketsphinx.
This is the Makefile:
CC = g++
CFLAGS = -Wall -std=c++11
LDFLAGS = -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx
OBJ = obj/Application.o obj/Main.o obj/Recorder.o
all: $(OBJ)
$(CC) -L/usr/local/lib -o bin/Eve $(OBJ) -s -lsphinxbase -lpocketsphinx
obj/Main.o: src/Main.cpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Main.cpp -o obj/Main.o
obj/Application.o: src/Application.cpp src/Application.hpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Application.cpp -o obj/Application.o
obj/Recorder.o: src/Recorder.cpp src/Recorder.hpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Recorder.cpp -o obj/Recorder.o
It is the same which i was using on Windows, i just adjusted the file path.
I am receiving the following error:
$ make
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Application.cpp -o obj/Application.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Main.cpp -o obj/Main.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Recorder.cpp -o obj/Recorder.o
g++ -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o -s -lsphinxbase -lpocketsphinx
obj/Recorder.o: In function `Recorder::Recorder()':
Recorder.cpp:(.text+0x1c0): undefined reference to `ad_open_sps'
Recorder.cpp:(.text+0x20d): undefined reference to `ad_read'
Recorder.cpp:(.text+0x215): undefined reference to `cont_ad_init'
obj/Recorder.o: In function `Recorder::~Recorder()':
Recorder.cpp:(.text+0x2f3): undefined reference to `cont_ad_close'
Recorder.cpp:(.text+0x303): undefined reference to `ad_close'
obj/Recorder.o: In function `Recorder::recognizeFromMicrophone()':
Recorder.cpp:(.text+0x37a): undefined reference to `ad_start_rec'
Recorder.cpp:(.text+0x395): undefined reference to `cont_ad_calib'
Recorder.cpp:(.text+0x3f0): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x4e6): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x5b5): undefined reference to `ad_stop_rec'
Recorder.cpp:(.text+0x5d8): undefined reference to `ad_read'
Recorder.cpp:(.text+0x5f4): undefined reference to `cont_ad_reset'
collect2: error: ld returned 1 exit status
make: *** [all] Fehler 1
I don't think that it is a name mangling problem, since i built the lib on my own using the provided Makefile.
What can i do to link against the lib without errors?
EDIT: I figured out how to make it work. I simply modified the rule of the target "all" to this:
$(CC) -static -L/usr/local/lib -o bin/Eve $(OBJ) -s -lpocketsphinx -lsphinxbase -lsphinxad -lpthread
Functions like ad_read are defined in libsphinxad library, you need to add it to your linker command line:
g++ -static -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o \
-lpocketsphinx -lsphinxbase -libsphinxad
Please note that the order of libraries is important.

Undefined reference to SDL_*many*

This is my makefile:
CFLAGS = -Wall -pedantic -pedantic-errors -g -lSDL -lSDL_image
CC = g++
OBJ = Point.o Personaje.o Juego.o EventHandler.o
all: main
#%.o: %.cpp %.h
# $(CC) -c $(CFLAGS) $<
bin: other $(OBJ)
Point.o: Point.cpp Point.h
$(CC) -c $(CFLAGS) $<
Personaje.o: Personaje.cpp Personaje.h
$(CC) -c $(CFLAGS) $<
Juego.o: Juego.cpp Juego.h
$(CC) -c $(CFLAGS) $<
EventHandler.o: EventHandler.cpp EventHandler.h
$(CC) -c $(CFLAGS) $<
main: $(OBJ)
cd ../vista/; make
cp ../vista/*.o .
rm ../vista/*.o
g++ $(CFLAGS) $(OBJ) Pantalla.o Imagen.o main.cpp -o main
clean:
rm *.o;
rm main
PHONY: main
When I compile with "make" I get the following errors:
EventHandler.cpp:17: undefined reference to `SDL_PollEvent'
Pantalla.o: In function `Pantalla::initialize(int, int, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
Pantalla.cpp:6: undefined reference to `SDL_Init'
Pantalla.cpp:9: undefined reference to `SDL_SetVideoMode'
Pantalla.cpp:14: undefined reference to `SDL_WM_SetCaption'
and many other errors.
The header of "Pantalla.h" contains this line:
#include "SDL/SDL.h"
And I already installed the SDL packages.
I don't know what I'm doing wrong... please help!
CFLAGS = -Wall -pedantic -pedantic-errors -g -lSDL -lSDL_image
This isn't good. You're mixing compiler flags (warnings and debug) with linker information (which libraries you need to link to). Use two separate variables for that:
CFLAGS = -Wall -pedantic -pedantic-errors -g
LIBS = -lSDL -lSDL_image
Then when you compile (but don't link, i.e. when you generate the .o files), only use $(CFLAGS) (like you have now). But when you do link (to produce the final executable), add the linker directives.
g++ $(CFLAGS) -o main main.cpp $(OBJ) Pantalla.o Imagen.o $(LIBS)
The order you put your objects and libraries is important.
One problem is that you are using exactly the same flags for compilation and linking. The compiler doesn't use libraries; the linker doesn't know about pedantic.
A bigger problem is that your -lSDL -lSDL_image appears too early in the link command, g++ $(CFLAGS) $(OBJ) Pantalla.o Imagen.o main.cpp -o main. The linker won't look into libSDL.a or libSDL_image.a (or their equivalents) because there are no unresolved external references at the point where you specified the search. Put the -lSDL -lSDL_image options just before the -o option.