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
Related
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.
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 $#
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
I have project files and I need to use an external test file named TestSuite1.cpp that includes an external header file SignalMasker.h (Was given it and it's object file SignalMasker.o) and my main header file uthreads.h.
I'm still getting undefined refrences such as:
TestSuite1.cpp:63: error: undefined reference to 'SignalMasker::~SignalMasker()'
This means my Makefile isn't including the SignalMasker.o file that resides in the same directory.
This is my Makefile:
CC = g++
FLAGS = -Wall -g
OBJECTS = uthreads.o Thread.o Scheduler.o SchedulerStarter.o TestSuite1.o
.PHONY : clean
all: test1
test1: $(OBJECTS)
g++ $(FLAGS) $(OBJECTS) SignalMasker.o -L . -o test1
TestSuite1.o : TestSuite1.cpp SignalMasker.h uthreads.h
$(CC) -c $(FLAGS) TestSuite1.cpp
uthreads.o : uthreads.cpp uthreads.h SchedulerStarter.h Scheduler.h Thread.h
$(CC) -c $(FLAGS) uthreads.cpp
Scheduler.o : Scheduler.cpp Scheduler.h Thread.h
$(CC) -c $(FLAGS) Scheduler.cpp
SchedulerStarter.o : SchedulerStarter.cpp SchedulerStarter.h Scheduler.h
$(CC) -c $(FLAGS) SchedulerStarter.cpp
Thread.o : Thread.cpp Thread.h uthreads.h translateAdd.h
$(CC) -c $(FLAGS) Thread.cpp
clean:
rm -f $(OBJECTS) *~
And now I'm getting:
~/Desktop/tests$ make
g++ -Wall -g uthreads.o Thread.o Scheduler.o SchedulerStarter.o TestSuite1.o SignalMasker.o -L . -o test1
/usr/bin/ld: error: SignalMasker.o: incompatible target
TestSuite1.cpp:36: error: undefined reference to 'SignalMasker::SignalMasker(int)'
TestSuite1.cpp:63: error: undefined reference to 'SignalMasker::~SignalMasker()'
TestSuite1.cpp:63: error: undefined reference to 'SignalMasker::~SignalMasker()'
TestSuite1.cpp:68: error: undefined reference to 'SignalMasker::SignalMasker(int)'
TestSuite1.cpp:111: error: undefined reference to 'SignalMasker::~SignalMasker()'
TestSuite1.cpp:111: error: undefined reference to 'SignalMasker::~SignalMasker()'
collect2: ld returned 1 exit status
EDIT:
I'm now pondering with the idea that maybe incompatible target means they compiled it under 64bit. My machine is 32bit
you are missing rule to compile SignalMasker.cpp in your makefile. you have to write a rule, the same way you have written for
SchedulerStarter.o, Thread.o
SignalMasker.o : SignalMasker.cpp SignalMasker.h
$(CC) -c $(FLAGS) SignalMasker.cpp
This will ensure the SignalMasker.o is generated with the same compilation flags that you build other objects. It will eliminate incompatibility issues in case of 32-bit/ 64-bit variants.
if you are copying this SignalMasker.o from elsewhere, check the compilation flags used for generating the object. Use the same flags in your makefile.
I have problems clinking simplest program on Ubuntu/Oneiric:
#include <glibmm/ustring.h>
int main()
{
Glib::ustring s = "Test string";
}
using Makefile
PACKAGES=glibmm-2.4 glib-2.0 gtkmm-3.0 gtk+-3.0
CC=g++
CFLAGS=`pkg-config --cflags $(PACKAGES)` --std=c++0x
LD=g++
LDFLAGS=`pkg-config --libs $(PACKAGES)`
build: ./main
run: build
./main
clean:
rm ./main.o
rebuild: clean build
./main: ./main.o
$(LD) $(LDFLAGS) ./main.o -o ./main
./main.o: ./main.cc
$(CC) $(CFLAGS) ./main.cc -c -o ./main.o
on make following errors appears:
./main.o: In function `main':
main.cc:(.text+0x15): undefined reference to `Glib::ustring::ustring(char const*)'
main.cc:(.text+0x21): undefined reference to `Glib::ustring::~ustring()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
On Ubuntu/Maverick the exactly same code links well with exactly same file...
if using ld on main.o it links successfully too but (as it was expected) _start is missing...
Any suggestions?
Try changing the relevant lines to this:
LDFLAGS=`pkg-config --libs-only-L --libs-only-other $(PACKAGES)`
LIBS=`pkg-config --libs-only-l $(PACKAGES)`
# ...
./main: ./main.o
$(LD) $(LDFLAGS) ./main.o -o ./main $(LIBS)
The reason is that the linker may search libraries in the order they are given on the command line, so they should always be placed last to be safe.