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

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

Related

Error "undefinded reference to omp_get_wtime"

Why am I getting the error message undefinded reference to 'omp_get_wtime' when I try to build my cpp file?
The full error message looks like this:
C:\Program Files\JetBrains\CLion 2022.2.4\bin\mingw\bin/ld.exe:
C:\Users\Hannah\AppData\Local\Temp\ccp3nN8r.o:daxpy.cpp:(.text+0x629): undefined reference to 'omp_get_wtime'
collect2.exe: error: ld returned 1 exit status
I looked into various posts already and most of them say to link -fopenmp in the makefile, but I already did that (it was done for us students).
My Makefile looks like this:
CC=gcc
CXX=g++
CFLAGS=-O3 -fopenmp -std=c99
CXXFLAGS=-O3 -fopenmp
EXECS=daxpy
all: $(EXECS)
matmul_serial-sol: daxpy.cpp
$(CXX) -o $# $< $(CXXFLAGS)
clean:
rm -f $(EXECS) *.o
and after suggestions from the comments:
CC=gcc
CXX=g++
CFLAGS=-O3 -fopenmp -std=c99
CXXFLAGS=-O3 -fopenmp
LDFLAGS= -fopenmp
EXECS=daxpy
all: $(EXECS)
daxpy: daxpy.cpp
$(CXX) -o $# $< $(CXXFLAGS)
clean:
rm -f $(EXECS) *.o
and I did include omp.h in my file.

/usr/bin/ld: cannot open output file bin/genericMatching: No such file or directory

I'm trying to compile my c++ program using makefile in ubuntu But there is an error which I can't understand. This is an error message I got.
Parallel/ParallelSlave.o Parallel/IOHandler.o commons/File19794.o -o bin/genericMatching
/usr/bin/ld: cannot open output file bin/genericMatching: No such file or directory
collect2: error: ld returned 1 exit status
Makefile:38: recipe for target 'bin/genericMatching' failed
This is codes inside my Makefile and there is no bin file/directory in my program folder :
# The compiler to use.
CC = mpiCC
# Directories for Includes and Common clases
IDIR =include
CDIR =commons/
JIANGDIR =MatcherJiang/
MCCDIR =MCC/
PDIR =Parallel/
BINDIR =bin/
# Compiler options -Weffc++
CFLAGS= -Wall -O2 -fopenmp -I$(IDIR) -I$(JIANGDIR) -I$(MCCDIR)
# Sources and Common clases sources
SOURCES= $(PDIR)genericMatching.cpp
SOURCESD= $(PDIR)DPDDFF.cpp
CSOURCES= $(CDIR)Fingerprint.cpp $(CDIR)Score.cpp $(JIANGDIR)FingerprintJiang.cpp $(MCCDIR)MCC.cpp $(MCCDIR)Cylinder.cpp $(CDIR)Functions.cpp $(CDIR)Minutia.cpp $(CDIR)GrahamScanConvexHull.cpp $(CDIR)Munkres.cpp $(PDIR)ParallelHandler.cpp $(PDIR)ParallelMaster.cpp $(PDIR)ParallelSlave.cpp $(PDIR)IOHandler.cpp $(CDIR)File19794.cpp
# Objects
OBJECTS=$(SOURCES:.cpp=.o)
OBJECTSD=$(SOURCESD:.cpp=.o)
COBJECTS=$(CSOURCES:.cpp=.o)
# Name of the executable
EXECUTABLE=$(BINDIR)genericMatching
EXECUTABLED=$(BINDIR)DPDDFF
all: $(EXECUTABLE) $(EXECUTABLED)
.PHONY: doc
doc:
doxygen Doxyfile
$(EXECUTABLE): $(OBJECTS) $(COBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) $(COBJECTS) $(OBJECTFILES) -o $# $(LDFLAGS)
$(EXECUTABLED): $(OBJECTSD) $(COBJECTS)
$(CC) $(CFLAGS) $(OBJECTSD) $(COBJECTS) $(OBJECTFILES) -o $# $(LDFLAGS)
.cpp.o:
$(CC) $(CFLAGS) -c $< -o $#
clean:
rm -f $(OBJECTS) $(OBJECTSD) $(COBJECTS) $(EXECUTABLE) $(EXECUTABLED)
mrproper: clean
rm -r doc/latex doc/html
When running this rule:
$(EXECUTABLE): $(OBJECTS) $(COBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) $(COBJECTS) $(OBJECTFILES) -o $# $(LDFLAGS)
it will implicitly run ld to create $(EXECUTABLE), which is bin/genericMatching. Because the bin directory does not exist, throws an error. I would suggest adding:
$(EXECUTABLE) $(EXECUTABLED): | $(BIN)
$(BIN) :
mkdir -p $#
to your makefile. Make will generate the directory before building the executables. Notice that the | makes it an order-only prerequisite, which means it will not rebuild $(EXECUTABLE) or $(EXECUTABLED) if $(BIN) is newer than either target.

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

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.

c++ makefile destructor undefined reference

This is a puzzle to me:
c++ undefined reference to destructor
That's the issue, the code is the same as the link, full makefile and errors here. The linked answers did help but only to highlight that I had some headers in a place I did not expect.
makefile
CXX = g++
BIN = .
LIBS = -L.
INCLUDE = -I . -I
CXXFLAGS = -pipe # -O6
LFLAGS = -lm
GeomTest_OBJS = geomTest.o SASAGeometry.o
geomTest_source = SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h geomSetup.cpp
SASAGeometry.o : SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h
geomTest.o : geomSetup.cpp
geomTest : $(GeomTest_OBJS) makefile
$(CXX) -o geomTest.o -o SASAGeometry.o $(LIBS) $(INCLUDE) $(CXXFLAGS) $(geomTest_source) $(LFLAGS)
$(CXX) $(LIBS) $(INCLUDE) $(CXXFLAGS) -o $(BIN)/geomTest geomTest.o SASAGeometry.o $(LFLAGS)
clean : \rm *.o *~ p1
I have both declared and instantiated the destructor AND not (allowing the compiler to do its thing)
error
geomSetup.cpp:(.text+0x5ab): undefined reference to `SASAGeometry::~SASAGeometry()'
geomSetup.cpp:(.text+0x5cd): undefined reference to `SASAGeometry::~SASAGeometry()'
no other errors. (sorry if its preferable to bump seemingly sorted issues rather than link to them, but my effort at that didn't work)
Thanks in advance!
EDIT:
MAKE SURE TO COMPILE THE CORRECT SOURCE FILES, NOT THE OLD ONES YOU FORGOT TO COPY.
Hi #trojanfoe and #Kerrick SB, both answers made me look at my makefile and realise it was ugly. Here is the revised version. I am still getting the same 'undefined reference to destructor' error though:
makefile:
CXX = g++
BIN = .
LIBS = -L.
INCLUDE = -I.
CXXFLAGS = -pipe # -O6
LDFLAGS = -lm
GeomTest_OBJS = sasa_transformMatrix.o SASAGeometry.o geomSetup.o
SASAGeometry.o : SASAGeometry.cpp SASAGeometry.h
sasa_transformMatrix.o : sasa_transformMatrix.cpp sasa_transformMatrix.h
geomSetup.o : geomSetup.cpp
geomTest : $(GeomTest_OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $# $+
clean : \rm *.o *~ p1
error:
geomSetup.o: In function `main':
geomSetup.cpp:(.text+0x5ab): undefined reference to `SASAGeometry::~SASAGeometry()'
geomSetup.cpp:(.text+0x5cd): undefined reference to `SASAGeometry::~SASAGeometry()'
collect2: ld returned 1 exit status
make: *** [geomTest] Error 1
Why would the linker complain about the destructor and not the constructor or any other methods/functions in the class?
Thanks again!
Your executable output file and your object file geomTest.o have the same name! That's bound to get you into trouble when the linker overwrites the object file.
Change it to $(CXX) -o geomTest ..., or better even to $(CXX) $# ... to avoid such problems in the future.
In fact, you are misusing the linker command altogether: you just want to have one single -o option, and the objects are listed directly, without flags:
g++ -o myprog main.o foo.o bar.o
Within the Makefile, do yourself a favour and use magic macros:
myprog: main.o foo.o bar.o
$(CXX) $(LDFLAGS) -o $# $+ $(LIBRARIES)
Here -o $# matches the target name, i.e. -o myprog, and $+ matches all the dependent names, i.e. main.o foo.o bar.o.
The guiding idea behind using variables is that you should never say the same thing more than once if you can help it. So you can have myprog: $(MyObjects) as the rule, but then use $+ in the command line to avoid repetition of MyObjects. This improves locality and maintainability.
The -o SASAGeometry.o in the geomTest target looks highly suspect to me - you are overwriting one of the dependency objects (actually both dependencies).
Try this:
geomTest : $(GeomTest_OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $# $(GemoTest_OBJS) $(LIBS)
(note that $LDFLAGS is the conventional variable in which to hold linker flags, not $LFLAGS which is used with the lex tool).