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.
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.
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.
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.?
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.
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.
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.