I'm trying to make a makefile with multiple files. Can someone help me?

I'm trying to make a makefile with multiple files. Can someone help me? The files I have are main.cpp, file1.h, file1.cpp, file2.h, file2.cpp.

/*simple command to execute on command line*/
g++ -std=c++11 -pthread main.cpp file1.cpp file2.cpp.
This command works fine and got output also, but struck to get output using makefile.

Here is my makefile:

main.o: main.cpp file1.h file2.h
g++ -g -std=c++11 -pthread main.cpp file1.cpp file2.cpp

Output: main.o file1.o file2.o
g++ -g -std=c++11 -pthread main.o file1.o file2.o -o Output

clean:
rm *.o Output

Errors:
g++ -g -std=c++11 -pthread main.o
g++: error: main.o: No such file or directory
g++: fatal error: no input files
compilation terminated.
make: *** [main.o] Error 4

Note:I used std::thread in programs and two classes are there one in file1 and another one in file2. In file1.cpp both file1.h and file2.h are declared and vice-versa.
Last edited on
The rule for creating main.o should only compile main.cpp.

g++ -g -std=c++11 -pthread -c main.cpp

You also need similar rules for the other source files.

Edit: don't forget the -c (compile) flag.
Last edited on
As you said i changed my makefile code, now no errors. But its not producing executable file. Please help me out to get executable file. I am not understanding where i am going wrong.

main.o: main.cpp file1.h file1.cpp file2.h file2.cpp
g++ -g -std=c++11 -pthread main.cpp file1.cpp file2.cpp

file1.o: file1.h file1.cpp
g++ -g -std=c++11 -pthread file1.cpp

file2.o: file2.h file2.cpp
g++ -g -std=c++11 -pthread file2.cpp

Output: main.o file1.o file2.o
g++ -g -std=c++11 -pthread main.o file1.o file2.o -o Output

clean:
rm *.o Output
Last edited on
You need to use the -c flag when creating the object files (.o), otherwise it will try to produce an executable.

Run the make command as "make Output" in order to produce the Output executable.
I have done correction to my makefile and now i am getting it work done. But is this right way to write makefile ?? Please guide me.... thanks in advance
Output: main.cpp file1.cpp file2.cpp
g++ -c -g -std=c++11 -pthread main.cpp file1.cpp file2.cpp -o Output

clean:
rm *.o Output

Because i have 5 files(main.cpp, file1.h, file1.cpp, file2.h, fil2.cpp) and dependencies also i need to take for consideration!! I dont thinks so this is rite way to do it in makefile. plz show me how it can be done.?
Last edited on
Yeah you should list the header files as dependencies too because you want the program to be recompiled if you make changes to a header. Your makefile will work but it has the disadvantage that it will recompile the whole program when you make a change. This might be okay for a small program or if it's just intended for the users to be able to compile your program. If you use the makefile during development you are slowed down because you have to spend a lot of time waiting for it to compile. For this small program it might be okay but if you have a larger program with say 100 files you really don't want everything to recompile each time you make a change because that could take several minutes.
Thanks Peter
file1.o: main.cpp file1.h file2.cpp
g++ -c -g -std=c++11 -pthread file1.cpp main.cpp

file2.o: main.cpp file2.h file2.cpp
g++ -c -g -std=c++11 -pthread file2.cpp main.cpp

main.o: main.cpp
g++ -c -g -std=c++11 -pthread main.cpp

Output: main.o file1.o file2.o file1.h file2.h
g++ -g -std=c++11 -pthread main.o file1.o file2.o -o Output

clean:
rm *.o Output

Edited makefile working without error but each time it executes all commands even though i am not editeed those files. Pleas have a look over it and guide me where i am doing wrong.

In Command Line: make Output
g++ -c -g -std=c++11 -pthread main.cpp
g++ -c -g -std=c++11 -pthread file1.cpp main.cpp
g++ -c -g -std=c++11 -pthread file2.cpp main.cpp
g++ -g -std=c++11 -pthread main.o file1.o file2.o -o Output

How i can make compile only those files which have been recently editted??
Last edited on
The dependencies lists the files that are needed to compile the target file. If any of the dependencies are updated it will rerun the command in order to update the target file.

To produce the file1.o you only need one .cpp file, that is file1.cpp. This is the only source file that should be listed as a dependency for file1.o. You should also list the header files that are included from this file, and if the header files includes other header files you should list them as well.

The dependencies for Output should only list the .o files.
Thank you peter........ Your guidence are very useful to create makefile for my project.
Last edited on
Topic archived. No new replies allowed.