Make: error compiling .o - makefile

My makefile below works after I clean and remove all object files, however if I edit a source file and then run make again, it outputs the error below. I believe this is coming from the lines:
%.o: %.cpp
$(CXX) -c $(CFLAGS) $< -o $#
I thought I had a basic grasp of automatic variables, but something isn't right.
Makefile:
all: main
CFLAGS=-fPIC -g -Wall `pkg-config --cflags opencv` -I /usr/local/include/libusb-1.0/
LIBS = `pkg-config --libs opencv`
INCLUDE = -I /usr/local/include/libfreenect
FREE_LIBS = -L/usr/local/lib -lfreenect
main: device.cpp cups.cpp main.cpp
$(CXX) $(INCLUDE) $(CFLAGS) $? -o $# $(LIBS) $(FREE_LIBS)
%.o: %.cpp
$(CXX) -c $(CFLAGS) $< -o $#
clean:
rm -rf *.o main
Output from make:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1

In the line
$(CXX) $(INCLUDE) $(CFLAGS) $? -o $# $(LIBS) $(FREE_LIBS)
you use $?, which means only the updated dependencies will be used in the command. (You should be seeing this in the command.)
Use $+ instead. (See the manual.)
(PS: This is one half of the answer; the other half is Sagar Sakre's advice.)

main taget should depend on .o files instead of .cpp
change
main: device.cpp cups.cpp main.cpp
to
main: device.o cups.o main.o
everything should work fine

Related

Link dylib library

I'm trying to link a dylib to my makefile on Mac, but Clang gives this message:
Undefined symbols for architecture x86_64:
"_zbesj_wrap", referenced from:
sp_bessel::besselJ(double, std::__1::complex<double>) in besselJ.o
"_zbesy_wrap", referenced from:
sp_bessel::besselJ(double, std::__1::complex<double>) in besselJ.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I installed the library in /usr/lib, where I can see libcomplex_bessel.0.6.0.dylib and libcomplex_bessel.dylib.
This is my makefile:
OBJS = main.o besselJ.o
CC = c++
CFLAGS = -std=c++11 -stdlib=libc++
LIBS = -L/usr/lib -lcomplex_bessel
PROGRAM_NAME = test
all: $(PROGRAM_NAME)
$(PROGRAM_NAME): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $#
main.o: main.cpp
$(CC) $(CFLAGS) -c $< -o $#
besselJ.o: besselJ.cpp
$(CC) $(CFLAGS) -c $< -o $# $(LIBS)
After reading other questions, I tried different combinations for -L and -l but nothing worked. Sorry but it's my first time with external libraries...
I changed my makefile to this:
OBJS = main.o besselJ.o
CC = c++
CFLAGS = -std=c++11 -stdlib=libc++
LIBS = -L/usr/lib -lcomplex_bessel
PROGRAM_NAME = test
all: $(PROGRAM_NAME)
$(PROGRAM_NAME): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $# $(LIBS)
main.o: main.cpp
$(CC) $(CFLAGS) -c $< -o $#
besselJ.o: besselJ.cpp
$(CC) $(CFLAGS) -c $< -o $#
but I still have problems, I get this message:
c++ -std=c++11 -stdlib=libc++ main.o besselJ.o -o test -L/usr/lib -lcomplex_bessel
ld: library not found for -lcomplex_bessel
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1
Ok, I think I solved it using xcode-select --install: other users had the same problem with libraries after updating it.
You need to pass libs to linker, not to the compiler:
OBJS = main.o besselJ.o
CC = c++
CFLAGS = -std=c++11 -stdlib=libc++
LIBS = -L/usr/lib -lcomplex_bessel
PROGRAM_NAME = test
all: $(PROGRAM_NAME)
$(PROGRAM_NAME): $(OBJS)
$(CC) $(OBJS) -o $# $(LIBS)
main.o: main.cpp
$(CC) $(CFLAGS) -c $< -o $#
besselJ.o: besselJ.cpp
$(CC) $(CFLAGS) -c $< -o $#

Error on makefile with threads and my library

So I have been searching and looking for something that could help me with the Makefile, but I did not find anything, so thats why I am here.
My makefile right now is like this:
CC = gcc
CFLAGS = -Wall
LDFLAGS += -L$(LIBB)
LDFLAGS += -static lib1.h
LDLIBS = -lm -lpthread -lrt -l
SOURCES=lib1.c prac3.c prac3_reader.c
LIBRARIES=lib1.o
INCLUDES=lib1.h
PROGRAMS=prac3 prac3_reader
all: $(OBJS) $(PROGRAMS)
$(PROGRAMS): $(LIBRARIES) $(INCLUDES)
$(CC) $(LDFLAGS) $(LIBRARIES) $(LDLIBS) $#.o -o $#
%.o: %.c $(INCLUDES)
$(CC) $(CFLAGS) -o $# -c $<
clean:
rm -rf *.o *~ $(PROGRAMS)
I know there are probably a lot of things that can be removed, but I do not really know which ones are. I have two programs called
prac3.c
and
prac3_reader.c
Also, I have my own library called
lib1.c
and also compiled like
lib1.h
When I go to my directory with the terminal and use the command make I recive this error:
gcc -L -static lib1.h lib1.o -lm -lpthread -lrt -l prac3.o -o prac3
/usr/bin/ld: no se puede encontrar -lprac3.o
collect2: error: ld returned 1 exit status
Makefile:15: recipe for target 'prac3' failed
make: *** [prac3] Error 1
I am running on Ubuntu.
The -l flag expects an argument. When it is combined in the gcc statement it causes the prac3.o argument to be considered as the name of a library. There is no such library prac3.o, so you get the error.
In general .o files aren't "libraries". They are object files. Remove the -l flag and you will be fine.
"libraries" are generally .a or .so files from a library path - but even then, you wouldn't specify the suffix (.e.g "-lpthreads").

ld can't find library given -L

I have an object file main.o, and need to link it against a shared library at ./libsvm/libsvm.so.2. I have the following Makefile but it doesn't work for me. Library path has been specified in -L./libsvm but gcc -lsvm still can't find the shared library (libsvm.so.2).
This is my Makefile:
CC = g++ -g
CFLAGS = -Wall
HEADERS = -I./libsvm
OBJ = main.o
LIBS = -L./libsvm
all: lib $(OBJ)
$(CC) $(LIBS) -lsvm $(OBJ) -o main
%.o: %.c
$(CC) $(CFLAGS) $(HEADERS) -c -o $# $<
lib:
cd libsvm; make
It just works if link them directly, as in
ld main.o libsvm/libsvm.so.2 -o main
I wonder what's wrong in the Makefile. Error message is the following
g++ -g -L./libsvm -lsvm main.o -o main
ld: library not found for -lsvm
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
-lsvm means use the file svm.so
But your library file has name svm.so.2. (Version 2)
So either rename or make a symbolic link with
ln -s svm.so.2 svm.so
Now the makefile should work.

Makefile add last flag

I am trying to compile my project using makefile.
Command line that works perfectly for me is:
g++ -I stuff/ -L stuff2/ src/Core.cpp -o file_name -ljvm
If I miss -ljvm at the end I end up with an error while compiling, this flag must go after file_name. The problem is that I am unable to add -ljvm successfully using makefile.
My makefile looks like this:
NAME = 'AI'
SRC = src
TGT = obj
PRG = application
INCLUDES = -Iinclude -I /usr/lib/jvm/java-7-openjdk-amd64/include/
LIBRARIES = -L /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/
CXXFLAGS = -Wall -O3 -std=c++0x $(INCLUDES) $(LIBRARIES)
SOURCES = $(wildcard $(SRC)/*.cpp)
OBJS = $(addprefix $(TGT)/, $(notdir $(SOURCES:.cpp=.o)))
$(TGT)/%.o: $(SRC)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $#
$(PRG)/$(NAME): $(OBJS)
$(CXX) $(LDFLAGS) $(OBJS) -o $#
Which gives me an error since I have not added -ljvm.
If I add it to: $(CXX) $(CXXFLAGS) -c $< -o $# -ljvm, nothing changes and I still get same error.
If I add it to last line $(CXX) $(LDFLAGS) $(OBJS) -o $# -ljvm I get a different error:
g++ obj/Core.o -o application/'AI' -ljvm
/usr/bin/ld: cannot find -ljvm
collect2: error: ld returned 1 exit status
make: *** [application/'AI'] Error 1
I have a feeling that I am missing something simple here. Any suggestions are much appreciated.
Your last line uses $(LDFLAGS) that is actually never defined.
ld complains about the fact it cannot find -ljvm, that's because you have to pass
-L /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/
to the linker (i.e. add it to your last command).
Try defining LDFLAGS with, at least, $(LIBRARIES) in it.

Problems wih make file code

When i run the code below , it give me an error message which is :
cc1: warning: main.c: not a directory [enabled by default]
frequent.o: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
make: * [main] Error 1
CC = gcc
CPPFLAGS = -I
main: main.c headers.h sortt.o frequent.o
#$(CC) $(CPPFLAGS) -c $^ -o $#
sortt.o: headers.h sortt.c
$(CC) $(CPPFLAGS) -c $< -o $#
frequent.o: headers.h frequent_word.c search_similar_word.o
$(CC) $(CPPFLAGS) -c $< -o $#
You have a lot of errors in your rules. Why is frequent.o listing search_similar_word.o as a prerequisite? There's no reason that you can't build frequent.o before or at the same time as search_similar_word.o.
Also, you are using $< in the compilation line which expands to the first prerequisite, but the first prerequisite for frequent.o is headers.h, so you're trying to compile headers.h which is wrong: you want to compile frequent_word.c.
Ditto for sortt.o.
It's also very odd to name your object files with different names than the source files.
In your CPPFLAGS you're listing a -I flag but you've given it no argument: that flag takes a directory as an argument.
And finally, you are using the -c flag when you're trying to link the final object (I'm assuming the comment character # is just an artifact of debugging); that's not right as the -c flag tells the compiler to generate an object file, and to not link the final object.
Your makefile should look something like this:
CC = gcc
CPPFLAGS = -I .
main: main.c headers.h sortt.o frequent.o
$(CC) $(CPPFLAGS) $^ -o $#
sortt.o: sortt.c headers.h
$(CC) $(CPPFLAGS) -c $< -o $#
frequent.o: frequent_word.c headers.h
$(CC) $(CPPFLAGS) -c $< -o $#
I have no idea what to do with search_similar_word.o since it doesn't seem to be used anywhere in your build.

Resources