0

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:

CC=g++
CFLAGS=-fPIC -c -Wall
INCLUDES=`php-config --includes` -Iwrappers
LDFLAGS=-shared
SOURCES=foo_wrap.cpp \
        foobar_wrap.cpp  \
        foofoobar_wrap.cpp   \
        foobarbar_wrap.cpp

OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=php_foobarlib.so

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

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

.cpp.o:
        $(CC) $(CFLAGS) $< -o objects/$(input)

clean:
    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.

Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • @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: – Ferenc Deak 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: http://stackoverflow.com/questions/1814270/gcc-g-option-to-place-all-object-files-into-separate-directory – Ferenc Deak 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

2 Answers2

0

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

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 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
0

You can specify in the Makefile the following:

SOURCES=wrappers/foo_wrap.cpp wrappers/fo. .... and so on,

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.

Community
  • 1
  • 1
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167