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.
Related
Ok I Have a problem making a MakeFile for C++ project in Windows. I checked around and found some tutorials and this is what I got:
IDIR =../include
CC = g++
CFLAGS=-I$(IDIR)
ODIR = obj
LDIR =../lib
LIBS = -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system
_DEPS = Button.hpp
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = Test.o Button.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.c $(DEPS)
$(CC) -c -o $# $< $(CFLAGS)
Test.exe: $(OBJ)
$(CC) -o $# $^ $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
And this is the error message that pops up:
make: *** No rule to make target 'obj/Test.o', needed by 'Test.exe'. Stop.
EDIT:
Hi I'm still having trouble with my makefile. The point is that my project folder is a mess and I want to keep it neat.
My make file is having trouble working across multiple directories. Now all my cpp files, header files and object files are clumped in one folder and that works but since I'm adding more to my program it is becoming messy. I managed to make it work to have headers in one directory but objects are problematic hence I'm asking for help again.
This is my Makefile now:
IDIR = Headers
ODIR = obj
CC = g++
CPPFLAGS = -I$(IDIR) $(LIBS)
LIBS = -lm -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system
_DEPS = Button.hpp TextBox.hpp TextToWindow.hpp MessageBox.hpp CheckBox.hpp
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = Button.o TextBox.o TextToWindow.o MessageBox.o CheckBox.o Test.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.cpp $(DEPS)
$(CC) -c -o $# $< $(CPPFLAGS)
Test.exe: $(OBJ)
$(CC) -o $# $^ $(CPPFLAGS)
.PHONY: clean
clean:
del $(ODIR)*.o *.exe
And this is my working dir:
MainDir:
-Headers:
-hpp
-hpp
.
.
-cpp
-o
-cpp
.
.
It is working for headers but it cant make objects in that dir hence the error:
make: *** No rule to make target 'obj/Button.o', needed by 'Test.exe'. Stop.
Ok so I made it like this and it works, but I still can't make it work in multiple directories.
CC = g++
CPPFLAGS = -I $(LIBS)
LIBS = -lm -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system
DEPS = Button.hpp TextToWindow.hpp
OBJ = Test.o Button.o TextToWindow.o
%.o: %.cpp $(DEPS)
$(CC) -c -o $# $< $(CPPFLAGS)
Test.exe: $(OBJ)
$(CC) -o $# $^ $(CPPFLAGS)
.PHONY: clean
clean:
rm -f /*.o *~ core /*~
Oh, and the clean function doesn't work. If anyone can suggest a better way please do. Thanks!
INC_DIR = ./include
SRC_DIR = ./src
OBJ_DIR = ./obj
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp))
H_FILES = $(wildcard $(INC_DIR)/*.cpp)
OBJ_FILES=$(patsubst $(SRC_DIR)/%.cc,$(OBJ_DIR)/%.o,$(SRC_FILES)
TARGET = PT3
CC = g++
CFLAGS = - fPIC -c -Wall -Werror -pedantic -std=c++11 -Wno-c++11-extensions
CPPFLAGS = $(addprefix -I, $(INC_DIR))
clean:
rm -f $(OBJECTS) $(TARGET)
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# $^
%.o: %.cc
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# -c $<
g++ -shared -fPIC -o libtest.so $(OBJECTS)
main.o: main.cc
$(CC) $(CFLAGS) $(CPPFLAGS) -L/pt3/lib -o maintest main.cc -libtest
this is currently what I have and i know its not syntactically right or remotely working but Im getting stuck on creating the shared library so I dont even know what else wouldnt compile.**
INC_DIR = ./include
SRC_DIR = ./src
SRC_FILES = $(sort $(shell find $(SRC_DIR) -name '*.cc'))
OBJ_FILES = $(SRC_FILES:.cc=.o)
TARGET = PT3
CC = g++
CFLAGS = -fPIC -Wall -Werror -pedantic -std=c++11 -Wno-c++11-extensions
CPPFLAGS = $(addprefix -I, $(INC_DIR))
#clean:
# rm -f $(OBJECTS) $(TARGET)
all: $(TARGET)
$(TARGET): $(OBJ_FILES)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# $^
%.o: %.cc
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# -c $<
libtest.so: $(OBJ_FILES)
$(CC) -shared -fPIC -o $# $^
maintest: main.o libtest.so
$(CC) $(CFLAGS) $(CPPFLAGS) -L. -o maintest main.o -libtest
this is what i rewrote the code to however Im getting a no input files error, but Im not sure if thats coming from a wrong read / failure to get into the required folders, or due to possibly missing a -o or -c?
Ive worked on the code some more following the suggestions and have come to this:
CXX = g++
CXXFLAGS := -Wall -Werror -pedantic -std=c++11 -Wno-c++11-extensions
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp)
OBJECTS=$(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))
INC_DIR = include
SRC_DIR = src
OBJ_DIR = obj
TEST_DIR = tests
LIB_DIR = lib
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -fPIC -Iinclude -o $# -c $<
clean:
rm -f $(OBJECTS) $(TEST_DIR)/main.o
$(LIB_DIR)/libtest.so: $(OBJECTS)
#echo frank
$(CXX) -shared -fPIC -o $# $^
$(TEST_DIR)/main.o: $(TEST_DIR)/main.cc
$(CXX) $(CXXFLAGS) -Iinclude -o $# -c $<
maintest: $(TEST_DIR)/main.o $(LIB_DIR)/libtest.so
$(CXX) $(CXXFLAGS) -Llib -Iinclude -o $# $< -ltest
everything seems to compile fine however when running the maintest program it returns an error saying: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
thanks for the suggestions so far I feel like Im on the verge of actually getting the makefile working as intended
You've already written the recipe for the library (which builds it in the working directory -- we can change that later if you want):
g++ -shared -fPIC -o libtest.so $(OBJECTS)
The next step is to put it into a rule:
libtest.so: $(OBJECTS)
g++ -shared -fPIC -o libtest.so $(OBJECTS)
Then clean it up:
libtest.so: $(OBJECTS)
g++ -shared -fPIC -o $# $^
It looks as if you want the executable to be maintest, so let's write a rule for that:
maintest: main.o libtest.so
$(CC) $(CFLAGS) $(CPPFLAGS) -L. -o maintest main.o -ltest
Give that a try. We can make further adjustments once that much works.
EDIT: I see that there are a few other problems in your makefile. Your variables won't work as written. Do you name your source files "foo.cc" or "foo.cpp"?
EDIT: I can see that we'll have to do this in stages.
Step 1. Try this:
CXX = g++
CXXFLAGS := -Wall -Werror -pedantic -std=c++11 -Wno-c++11-extensions
INC_DIR = include
SRC_DIR = src
OBJ_DIR = obj
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -Iinclude -o $# -c $<
Try to build one or two object files with this, as in make obj/foo.o.
Step 2. Add a rule for main.o, and test it:
$(OBJ_DIR)/main.o: main.cc
$(CXX) $(CXXFLAGS) -Iinclude -o $# -c $<
Step 3. Add a "do nothing" rule for the library, and verify that it builds all of the objects:
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp)
OBJECTS=$(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))
libtest.so: $(OBJECTS)
#echo doing nothing
Step 4. Change the library rule to actually build the library:
libtest.so: $(OBJECTS)
$(CXX) -shared -fPIC -o $# $^
Step 5. Add a rule to build the test:
maintest: main.o libtest.so
$(CXX) $(CXXFLAGS) -L. -o $# $< -ltest
Make file:
INCLUDE = -I/usr/X11R6/include/
LIBDIR = -L/usr/X11R6/lib
COMPILERFLAGS = -Wall
CC = g++
CFLAGS = $(COMPILERFLAGS) $(INCLUDE)
LIBRARIES = -lX11 -lXi -lXmu -lglut -lGL -lGLU -lm
All: project
project: main.o landscape.o point.o
$(CC) $(CFLAGS) -o $# $(LIBDIR) $< $(LIBRARIES)
clean:
rm *.o
I have a landscape.cpp, landscape.h, point.cpp, point.h, and main.cpp files
I'm including "point.h" in my main.cpp file and i'm getting:
g++ -Wall -I/usr/X11R6/include/ -o project -L/usr/X11R6/lib main.cpp -lX11 -lXi -lXmu -lglut -lGL -lGLU -lm
/tmp/ccdpJ8HH.o: In function main':
main.cpp:(.text+0x1c0): undefined reference toPoint::Point(int, int)'
collect2: error: ld returned 1 exit status
Makefile:15: recipe for target 'project' failed
make: *** [project] Error 1
project: main.o landscape.o point.o
$(CC) $(CFLAGS) -o $# $(LIBDIR) $< $(LIBRARIES)
Here you need to link all .o files. The rule you have here will only use the main.o file. Because $<is the first dependency only. $^ should be for all three. So try:
project: main.o landscape.o point.o
$(CC) $(CFLAGS) -o $# $(LIBDIR) $^ $(LIBRARIES)
I suggest you to use a more complete Makefile.
Also, use CXX=g++ and CXXFLAGS instead of CC and CFLAGS, because you are compiling C++ and make have special variables.
Here is an example of Makefile I could use.
# Project name
NAME= project
# Include directory
INC_DIR= /usr/X11R6/include/
# Library directory
LIB_DIR= /usr/X11R6/lib/
# Compiler
CXX= g++
# Source files
SRC_DIR= # in case your cpp files are in a folder like src/
SRC_FILES= main.c \
landscape.c \
point.c
# Obj files
OBJ= $($(addprefix $(SRC_DIR), $(SRC_FILES)):.c=.o)
# that rule is composed of two steps
# addprefix, which add the content of SRC_DIR in front of every
# word of SRC_FILES
# And a second rule which change every ".c" extension into ".o"
LIBS= X11 \
Xi \
Xmu \
glut \
GL \
GLU \
m
# Compilation flags
CXXFLAGS= -Wall
CXXFLAGS+= $(addprefix -I, $(INC_DIR))
LDFLAGS= $(addprefix -L, $(LIB_DIR)) \
$(addprefix -l, $(LIBS))
# Rules
# this rule is only linking, no CFLAGS required
$(NAME): $(OBJ) # this force the Makefile to create the .o files
$(CXX) -o $(NAME) $(OBJ) $(LDFLAGS)
All: $(NAME)
# Remove all obj files
clean:
rm -f $(OBJ)
# Remove all obj files and the binary
fclean: clean
rm -f $(NAME)
# Remove all and recompile
re: fclean all
# Rule to compile every .c file into .o
%.o: %.c
$(CXX) -o $# -c $< $(CFLAGS)
# Describe all the rules who do not directly create a file
.PHONY: All clean fclean re
I'm not sure it's perfect, but it's better.
Also don't forget to put you project rule before your All rule to avoid relinking when simply calling make.
That way, you can also add beautiful messages (in the %.o: %.c rule for example).
With that you just have to do make re to get your binary fully updated.
I have this makefile:
CC=g++
CFLAGS=-c -Wall
all: hello
hello: main.o client.o
$(CC) main.o client.o -o hello
client.o: client.cpp client.h
$(CC) $(CFLAGS) client.cpp -o client.o
main.o: main.cpp
$(CC) $(CFLAGS) main.cpp -o main.o
clean:
rm -rf *o hello
Whenever I make changes in hello.h, client.o is rebuilt when I execute make. But when I try the resulting executable ./hello the change does not seem to happen.
The change is only reflected on ./hello if I add client.h to the main.o: rule like that
main.o: main.cpp client.h
$(CC) $(CFLAGS) main.cpp -o main.o
This will make things very difficult to maintain my code, any idea how to solve this problem?
Edit:
tried this change:
main.o: main.cpp
$(CC) $(CFLAGS) -MD main.cpp -o main.o
but did not help.
UPDATE (final version):
TARGET = hello
CC = g++
CPPFLAGS = -Wall -MP -MD
LINKER = g++ -o
LFLAGS = -Wall
SRCDIR = src
OBJDIR = obj
BINDIR = bin
SOURCES := $(wildcard $(SRCDIR)/*.cpp)
INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
DEPS := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.d)
RM = rm -rf
DIR_GUARD = mkdir -p $(#D)
$(BINDIR)/$(TARGET): $(OBJECTS)
#$(DIR_GUARD)
#$(LINKER) $# $(LFLAGS) $(OBJECTS)
#echo "Linking complete!"
$(OBJECTS): $(OBJDIR)/%.o: $(SRCDIR)/%.cpp
#$(DIR_GUARD)
#$(CC) $(CPPFLAGS) -c $< -o $#
#echo "Compiled "$<" successfully!"
-include $(DEPS)
.PHONEY: clean
clean:
#$(RM) $(OBJDIR)/* $(BINDIR)/*
#echo "Cleanup complete!"
Thanks guys for all the help, you are truly amazing.
The problem is that the dependency of main.o on client.h is not specified in your Makefile. Use:
main.o: main.cpp client.h
$(CC) $(CFLAGS) main.cpp -o main.o
I want my object files to be created in a subdirectory and not where Makefile lives. So, I saw this answer, which I couldn't apply to my case, so I tried this:
OBJS = main.o IO.o alloc.o communication.o
OBJSDIR = obj
SOURCE = main.cpp src/IO.cpp src/alloc.cpp src/communication.cpp
HEADER = headers/IO.h headers/alloc.h headers/communication.h
OUT = test
CXX = ../../mpich-install/bin/mpic++
CXXFLAGS = -I../../intel/mkl/include -Wl,--start-group -Wl,--end-group -lpthread -lm -ldl -Wall
LDFLAGS = ../../intel/mkl/lib/intel64/libmkl_scalapack_lp64.a -Wl,--start-group ../../intel/mkl/lib/intel64/libmkl_intel_lp64.a ../../intel/mkl/lib/intel64/libmkl_core.a ../../intel/mkl/lib/intel64/libmkl_sequential.a -Wl,--end-group ../../intel/mkl/lib/intel64/libmkl_blacs_intelmpi_lp64.a -lpthread -lm -ldl
all: $(OBJSDIR) $(OUT)
$(OBJSDIR):
mkdir $(OBJSDIR)
$(OUT): $(OBJS)
$(CXX) $(OBJS) -o $(OUT) $(CXXFLAGS) $(LDFLAGS)
# make -f Makefile clean
# create/compile the individual files >>separately<<
$(OBJSDIR)/main.o: main.cpp
$(CXX) -c main.cpp $(CXXFLAGS)
$(OBJSDIR)/IO.o: src/IO.cpp
$(CXX) -c src/IO.cpp $(CXXFLAGS)
$(OBJSDIR)/alloc.o: src/alloc.cpp
$(CXX) -c src/alloc.cpp $(CXXFLAGS)
$(OBJSDIR)/communication.o: src/communication.cpp
$(CXX) -c src/communication.cpp $(CXXFLAGS)
.PHONY: clean
clean:
rm -rf *.o
and I am getting:
gsamaras#pythagoras:~/konstantis/cholesky$ make
../../mpich-install/bin/mpic++ -I../../intel/mkl/include -Wl,--start-group -Wl,--end-group -lpthread -lm -ldl -Wall -c -o main.o main.cpp
make: *** No rule to make target 'IO.o', needed by 'test'. Stop.
I have a src folder, where all the .cpp files live (except from main.cpp, that lives in the same directory as the Makefile) and a headers directory, where all the header files live.
EDIT
I modified the first two lines, as such:
OBJSDIR = obj
OBJS = $(OBJSDIR)/main.o $(OBJSDIR)/IO.o $(OBJSDIR)/alloc.o $(OBJSDIR)/communication.o
and I am getting:
g++: error: obj/main.o: No such file or directory
...
The problem lies into the fact that the object files are still generated in the main directory!
You want to change the lines that invoke your compiler from:
$(OBJSDIR)/IO.o: src/IO.cpp
$(CXX) -c src/IO.cpp $(CXXFLAGS)
to:
$(OBJSDIR)/IO.o: src/IO.cpp
$(CXX) -c src/IO.cpp $(CXXFLAGS) -o $#
Note that $# is the automatic variable that corresponds to the target file being created. So in the above case it will be obj/IO.o. -o specifies the output filename.
Furthermore while it is unrelated to your question one of the nice things about placing all the build artifacts into a separate directory is cleaning is much easier:
clean:
rm -rf $(OBJSDIR) $(OUT)
Also, as a final note if you ever wish to do a parallel build you will have an issue as your object files rely on the build directory. There are a couple solutions to this including calling mkdir -p objs before every compiler invocation or setting up the directory as a dependency that is only built if it does not exist.