views:

68

answers:

2

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?

+1  A: 

The extension would be none at all, and the file is called Makefile if you want GNU make to find it automatically.

The following links give some nice and clean examples on writing a simple Makefile. One is C++ though, but just replace g++ with gcc, and cpp with c.

http://www.digitalpeer.com/id/example

http://www.cs.duke.edu/~ola/courses/programming/libraries.html

amn
A: 

There are a lot of options you can set when building a dll, but here's a basic command that you could use if you were doing it from the command line:

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

And here's a makefile (typically called Makefile) that will handle the whole build process:

# 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's 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.

Now to build mydll.dll, just type "make".

A couple of notes. If you just type "make" without specifying the makefile or the target (the thing to be built), Make will try to use the default makefile ("GNUMakefile", "makefile" or "Makefile") and the default target (the first one in the makefile, in this case mydll.dll).

Beta