I am running Windows 7 with gcc/g++ under Cygwin. What would be the Makefile format (and extension, I think it’s .mk?) for compiling a set of .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 would be the makefile format (and extension) for compiling these into a static library? (I’m very new to makefiles) What would be the fastest way to do this?
The extension would be none at all, and the file is called
Makefile(ormakefile) if you want GNU Make to find it automatically.GNU Make, at least, lets you rely on certain automatic variables that alone give you control over much of the building process with C/C++ files as input. These variables include
CC,CPP,CFLAGS,CPPFLAGS,CXX,CXXFLAGS, andLDFLAGS. These control the switches to the C/C++ preprocessor, compiler, and the linker (the program that in the end assembles your program) thatmakewill use.GNU Make also includes a lot of implicit rules designed to enable it automatically build programs from C/C++ source code, so you don’t [always] have to write your own rules.
For instance, even without a makefile, if you try to run
make foobar, GNU Make will attempt to first buildfoobar.ofromfoobar.corfoobar.cppif it finds either, by invoking appropriate compiler, and then will attempt to buildfoobarby assembling (incl. linking) its parts from system libraries andfoobar.o. In short, GNU Make knows how to build thefoobarprogram even without a makefile being present — thanks to implicit rules. You can see these rules by invokingmakewith the-pswitch.Some people like to rely on GNU Make’s implicit rule database to have lean and short makefiles where only that specific to their project is specified, while some people may go as far as to disable the entire implicit rule database (using the
-rswitch) and have full control of the building process by specifying everything in their makefile(s). I won’t comment on superiority of either strategy, rest assured both do work to some degree.