aws-sdk-cpp: unresolved symbols - c++

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 $#

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.

compiling error with makefile:

I am having the following error with makefile:
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function _start':
(.text+0x20): undefined reference tomain'
collect2: error: ld returned 1 exit status
makefile:7: recipe for target 'runme' failed
make: *** [runme] Error 1
That's my makefile code:
CXXFLAGS+=-std=c++11
OBJ=main.o Shape.o
all: runme
runme: main.o Shape.o
g++ $(OBJ) $(CXXFLAGS) -o runme
main.o: main.cpp Shape.hpp
g++ $(CXXFLAGS) main.cpp -o main.o
Shape.o: Shape.hpp Shape.cpp
g++ $(CXXFLAGS) Shape.cpp -o Shape.o
clean:
rm -f *.o
distclean: clean
rm -f runme

Linking dynamic library to my project

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.

How to make my project files with static library (Snappy)

I have project using Snappy library and makefile for it:
CXX=g++
CXXFLAGS=-c -Wall
LFLAGS=
OBJS=main.o Utilities.o FramingFormat.o Crc32.o
snappy.out: $(OBJS)
$(CXX) $(LFLAGS) $^ -o $#
$(OBJS): %.o:%.cpp
$(CXX) $(CXXFLAGS) $< -o $#
clean:
-rm -rf *.o
.PHONY: clean
Snappy library has been built earlier.
Now I run my makefile I have errors:
g++ main.o Utilities.o FramingFormat.o Crc32.o -o snappy.out
FramingFormat.o: In function `compressToFrame(char*, unsigned long, char*, unsigned long*)':
FramingFormat.cpp:(.text+0x5b): undefined reference to `snappy_compress'
FramingFormat.o: In function `uncompressFromFrameData(char*, unsigned long, char*, unsigned long*)':
FramingFormat.cpp:(.text+0x14a): undefined reference to `snappy_uncompress'
FramingFormat.o: In function `maxFrameLength(unsigned long)':
FramingFormat.cpp:(.text+0x2bf): undefined reference to `snappy_max_compressed_length'
FramingFormat.o: In function `uncompressedDataLength(char*, unsigned long, unsigned long*)':
FramingFormat.cpp:(.text+0x2f8): undefined reference to `snappy_uncompressed_length'
collect2: error: ld returned 1 exit status
make: *** [snappy.out] Error 1
It is because makefile don't know that I'm using snappy libs how to solve this problem? It's my directories:
snappy/catalog-with-snappy
snappy/catalog-with-project-using-snappy
[EDIT]
My makefile looks like this:
CXX=g++
CXXFLAGS=-c -Wall
LFLAGS=
OBJS=main.o Utilities.o FramingFormat.o Crc32.o
snappy.out: $(OBJS)
$(CXX) $(LFLAGS) $^ -L"../../SnappyLib1.1.2/SnappyLib1.1.2" -o $#
$(OBJS): %.o:%.cpp
$(CXX) $(CXXFLAGS) $< -L"../../SnappyLib1.1.2/SnappyLib1.1.2" -o $#
clean:
-rm -rf *.o
.PHONY: clean
use -lsnappy in the linker option, presuming you have snappy.so or snappy.a in the accessible directory. or you may have to use the directory explicitly

Link object with dependencies

my project tree:
./main.h
./main.cpp
./Makefile
./sources/myclass.cpp
./includes/myclass.h
./objects/
#./Makefile
BUILD = myexecutable
LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-audio -lsfml-system
CXXFLAGS = -Wall
RM = rm -rf
OBJECTS = main.o $(addprefix $(OBJ_DIR), myclass.o)
OBJ_DIR = objects/
SRC_DIR = sources/
.PHONY: clean
all: clean $(BUILD)
$(BUILD): $(OBJECTS)
$(CXX) $(LDFLAGS) -o $(BUILD) $(OBJECTS)
$(OBJ_DIR)%.o: $(SRC_DIR)%.cpp
$(CXX) -MM $(CXXFLAGS) -c $< > $(OBJ_DIR)$*.d # New line for create myclass.d
$(CXX) $(CXXFLAGS) -c $< -o $#
clean:
$(RM) $(BUILD) $(OBJ_DIR)*.o
when i try to run make i get this:
$ make
rm -rf myexecutable objects/*.o
g++ -MMD -Wall -c sources/myclass.cpp -o objects/myclass.o
g++ -lsfml-graphics -lsfml-window -lsfml-audio -lsfml-system -o myexecutable main.o objects/myclass.o
main.o: In function `main':
main.cpp:(.text+0x176): undefined reference to `mynamespace::MyClass::method()'
main.cpp:(.text+0x185): undefined reference to `mynamespace::MyClass::otherMethod()'
main.cpp:(.text+0x406): undefined reference to `mynamespace::MyClass::~MyClass()'
main.cpp:(.text+0x50e): undefined reference to `mynamespace::MyClass::~MyClass()'
collect2: ld devolvió el estado de salida 1
make: *** [myexecutable] Error 1
how to fix the main.o dependency of myclass.o?
Edit
i added a new line for create *.d dependency *.d files for i dont know how to use
Correct to:
LIBES = -lsfml-graphics -lsfml-window -lsfml-audio -lsfml-system
$(BUILD): $(OBJECTS)
$(CXX) -o $(BUILD) $(OBJECTS) $(LIBES)
The LIBES should replace LDFLAGS and the two lines for BUILD should be changed.
Order of linking matters a lot: put your object files, then the libraries from high-level to lowest-level system libraries.