zaloo
zaloo

Reputation: 919

Makefile compiling all .cpp files in current directory and all subdirectories

I'm familiar with how to create basic makefiles, but I'm trying to create a .dylib (like a .dll) from all the .cpp files in my current directory and all subdirectories, and I'm at a loss for what I should do. Here's my current makefile that makes the .dylib for only 2 .cpp files. I have no idea how to do this for all .cpp files without hard coding. How should my makefile look?

# Define a variable for classpath
CLASS_PATH = ../bin

# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)

all: libhpaprogram.dylib

# $@ matches the target, $< matches the first dependancy
libhpaprogram.dylib:
    cc -v -c -stdlib=libstdc++ -fPIC -I/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/ HPAProgram.cpp -o libhpaprogram.o
    cc -v -c -stdlib=libstdc++ -fPIC -I/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/ DCDTWrapper.cpp -o DCDTWrapper.o 
    libtool -dynamic -lSystem libhpaprogram.o DCDTWrapper.o -o libhpaprogram.dylib

HPAProgram.h : HPAProgram.class
    javah -classpath $(CLASS_PATH) $*

clean:
    rm HPAProgram.h libhpaprogram.o libhpaprogram.dylib

Upvotes: 0

Views: 3029

Answers (2)

zaloo
zaloo

Reputation: 919

I figured out how to compile everything. I did some research on makefiles, and here was my final makefile:

SRC=DCDTsrc
TGT=obj
INCLUDES=-IDCDTsrc DCDTWrapper.h HPAProgram.h
FLAGS=-stdlib=libstdc++ -fPIC -I/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/ -v
SOURCES=$(wildcard $(SRC)/*.cpp) DCDTWrapper.cpp HPAProgram.cpp
OBJS=$(addprefix $(TGT)/, $(notdir $(SOURCES:.cpp=.o)))
CC=GCC

# Define a variable for classpath
CLASS_PATH = ../bin

# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)

all: libhpaprogram.dylib

$(TGT)/%.o: $(SRC)/%.cpp
    $(CC) $(FLAGS) -c $< -o $@

$(TGT)/%.o: %.cpp
    $(CC) $(FLAGS) -c $< -o $@

# $@ matches the target, $< matches the first dependancy
libhpaprogram.dylib: $(OBJS)
    libtool -dynamic -lSystem $(OBJS) libhpaprogram.dylib



HPAProgram.h : HPAProgram.class
    javah -classpath $(CLASS_PATH) $*

clean:
    rm -rf $(TGT)
    mkdir $(TGT)
    rm HPAProgram.h libhpaprogram.o libhpaprogram.dylib

Upvotes: 0

user3159253
user3159253

Reputation: 17455

First option you have is to use make wildcard patterns

The second option is to use a cross-platform tool like CMake and let it generate Makefiles for you. Thus you'll free yourself from [most of] gory details such as exact compiler and linker flags etc. CMake even supports generation of MS Visual Studio projects :)

Upvotes: 2

Related Questions