Take the 2-minute tour ×

I am writing a Makefile by hand to create a PHP extension lib using SWIG. I have the following directory structure:

wrappers/   # SWIG generated C++ wrappers and header
objects/    # I want to place my object files here
bin/        # I want to place my executable (shared lib) here

This is what my Makefile looks like:

CFLAGS=-fPIC -c -Wall
INCLUDES=`php-config --includes` -Iwrappers
SOURCES=foo_wrap.cpp \
        foobar_wrap.cpp  \
        foofoobar_wrap.cpp   \


all: wrappers/$(SOURCES) bin/$(EXECUTABLE)

        $(CC) $(LDFLAGS) $(OBJECTS) -o objects/$(input)

        $(CC) $(CFLAGS) $< -o objects/$(input)

    rm -rf *o $(EXECUTABLE)

When I run make at the command line, I get the following error:

make: * No rule to make target foobar_wrap.cpp', needed byall'. Stop.

I want to build the shared library using CMake instead. Could someone please post an outline of the CMakeLists file I need to create to build the shared library, taking into accounts the directory structure of the project - i.e. where I want the built objects and binaries to go.

share|improve this question
@DavidSchwartz: yes all the files exist. The problem is with the locations. It is the directory structure of my project that is causing the (file not found) problems. –  Homunculus Reticulli Aug 27 '12 at 11:12
try: SOURCES=wrappers/foo_wrap.cpp wrappers/fo. .... and so on, remove wrappers/ from all: –  fritzone Aug 27 '12 at 11:12
@fritzone: Yes, thats what I had before, although that builds the object files, the object files were also being dumped into wrappers/ which I dont want. I want to generate the object files and binaries in the folders I have specified. –  Homunculus Reticulli Aug 27 '12 at 11:14
look at this: stackoverflow.com/questions/1814270/… –  fritzone Aug 27 '12 at 11:16
@fritzone: Do you want to post that as an answer?, its the closest to a solution so far. –  Homunculus Reticulli Aug 27 '12 at 11:46
show 1 more comment

2 Answers

up vote 0 down vote accepted

You can specify in the Makefile the following:


and then remove wrappers/ from all:

As observed by you, this will create the object files in the wrappers directory. In order to avoid that look at the gcc/g++ option to place all object files into separate directory answer for a solution to this problem.

share|improve this answer
add comment

wrappers/$(SOURCES) expands to nonsense, prefixing only the first filename with the path.

share|improve this answer
Ok, so that explains the error message. So how do I fix my Makefile so that sources are kept in wrappers and object files and binaries are built into objects and bin respectively? –  Homunculus Reticulli Aug 27 '12 at 11:18
add comment

Your Answer


