For the next program it is necessary to add automation of the assembly of a multi-file project using script shells and auto-pickers

How to use Make?

#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { int N, j_max; cout << "\n Input size of matrix: "; cin >> N; cout << endl; srand(time(NULL)); int **m = new int* [N]; for (int i=0; i<N; i++) { m[i] = new int[N]; } for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { m[i][j] = rand()%50-0; cout << " " << m[i][j] << "\t"; } cout << endl; } cout << endl; for (int i=0; i<N; i++) { j_max = 0; for (int j=1; j<N; j++) if (m[i][j_max] < m[i][j]) { j_max = j; } m[i][j_max] = 0; } for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { cout << " " << m[i][j] << "\t"; } cout << endl; } cout << endl; bool kl = true; for (int i=1; i<N; i++) { for (int j=0; j<i; j++) { if (m[i][j] != m[j][i]) kl = false; } } if (kl) { cout << " Symetric matrix." << endl; } else { cout << " Not symetric matrix." << endl; } cout << endl; delete [] m; return 0; } 
  • one
    Where is the "multi-file"? In this case, IMHO, make is not needed. - Vladimir Martyanov
  • In my opinion, you lack basic knowledge a little. See a brief description of the assembly program: b3d.int.ru/node/11 - bigov

2 answers 2

for example:

if the file is called, for example, prog.cpp, then its compilation can be started with the command:

 $ make prog 

because of the implicit rules that can be viewed by passing the -p option to the make program, the mini-makefile of this content will actually be interpreted:

 %: %.cpp $(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@ 

which, after substituting all available variables (which can be seen in the output of the same make -p ), will turn into a command:

 g++ $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@ 

where $^ is the prog.cpp string, and $@ is the prog string.

the remaining variables ( CXXFLAGS , CPPFLAGS , etc.) are not defined by default, and their values ​​( $(CXXFLAGS) , $(CPPFLAGS) , etc.) will be substituted as spaces.


Considering the above, the initial stage, the starting point can be a makefile of such content:

 all: prog 

you can interpret it by simply calling the make program with no arguments:

 $ make 

There is nothing “sacred” in the name of the goal ( all ), it just so historically happened that the very first goal (that is, performed by default) is called (quite often) just like that.

  • one
    It can be added that if there are environment variables LDFLAGS, LDLIBS, etc., then their values ​​will be substituted if these variables are not redefined in the makefile (however, these values ​​in turn can be redefined by the arguments of the make call) - avp
  • @avp, by the way, do you have it, purely by chance, at hand ms / windows ? I have been tormented for a long time by vague suspicions that the rule mentioned in the answer should not sound like %: %.cpp , but like %.exe: %.cpp , but there is absolutely no place to check. - aleksandr barakin
  • Just got to the computer. Now there is no Windows, but closer to midnight I'll take a look, then I will accomplish my goal. - avp
  • No, make -p in MinGW (GNU Make version 3.78.1) writes the exact same rule %: %.cpp # commands to execute (built-in): $(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@ Therefore, make really does not work. The file is on the prog.exe disk, but in makefile all: prog , and there is no default for all: prog.exe . By the way, CXX = / mingw / bin / g ++ had to be added with pens, for some reason, without full paths, CreateProcess does not work, but I didn’t deal with these wind-up tricks. / If you have any other questions, do not hesitate (you can in the C / C ++ chat) - avp
  • @avp, thanks! and you can’t run prog (without the suffix) in ms / windows ? then, all such implicit rules should be corrected. Only bugreport, apparently, should not be sent to gnu.org, but to mingw.org. because make you, as I understand it, is part of mingw. - aleksandr barakin

An example of a small makefile allows you to collect small projects

 EXEC=glib_test SOURCE=$(wildcard *.c) OBJS=$(SOURCE:.c=.o) DEPEND=$(SOURCE:.c=.d) CXX=gcc CFLAGS=-g2 -Wall `pkg-config --cflags glib-2.0` LDFLAGS=-g2 LIB=-lglib-2.0 $(EXEC):$(OBJS) $(CXX) $(LDFLAGS) -o $@ $^ $(LIB) %.o:%.c $(CXX) $(CFLAGS) -c $< -o $@ %.d:%.c $(CXX) -MM -I. $< > $@ include $(DEPEND) .PHONY:clean clean: -rm -f $(EXEC) *.o *.d *~