Makefile to compile .cpp and .h series to lib

I am running Windows 7 with gcc / g ++ under Cygwin. What will the Makefile format be (and the extension, I think it is .mk?) For compiling .cpp (C ++ source) and .h (header) files into a static library (DLL). Say I have a variable set of files:

  • file1.cpp
  • file1.h

  • file2.cpp

  • file2.h

  • file3.cpp

  • file3.h

  • ....

What will be the makefile format (and extension) for compiling them into a static library? (I am very new to makefiles). What would be the fastest way to do this?

+3
source share
2 answers

, Makefile, , GNU make .

Make , C/C++. CC, CPP, CFLAGS, CPPFLAGS, CXX, CXXFLAGS LDFLAGS. C/C++, , (, " ", : ", ").

GNU make , C/C++, .

, make , make foobar, GNU Make foobar.o foobar.c foobar.cpp, , , foobar ( ) foobar.o. , GNU Make , foobar make - . , Make -p, , make -p.

GNU Make, make , , , , ( -r), , . - , , .

+2

, dll, , , :

gcc -shared -o mydll.dll file1.o file2.o file3.o

makefile ( Makefile), :

# You will have to modify this line to list the actual files you use.
# You could set it to use all the "fileN" files that you have,
# but that dangerous for a beginner.
FILES = file1 file2 file3

OBJECTS = $(addsuffix .o,$(FILES)) # This is "file1.o file2.o..."

# This is the rule it uses to assemble file1.o, file2.o... into mydll.dll
mydll.dll: $(OBJECTS)
    gcc -shared $^ -o $@    # The whitespace at the beginning of this line is a TAB.

# This is the rule it uses to compile fileN.cpp and fileN.h into fileN.o
$(OBJECTS): %.o : %.cpp %.h
    g++ -c $< -o $@         # Again, a TAB at the beginning.

, mydll.dll, "make".

. "make", make (, ), Make make ( "GNUMakefile", "makefile" "Makefile" ) ( make , mydll.dll).

+2

Source: https://habr.com/ru/post/1751191/


All Articles