简体   繁体   English

Makefile编译当前目录和所有子目录中的所有.cpp文件

[英]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. 我熟悉如何创建基本的makefile,但我正在尝试从当前目录和所有子目录中的所有.cpp文件创建.dylib(如.dll),我不知所措我应该做。 Here's my current makefile that makes the .dylib for only 2 .cpp files. 这是我当前的makefile,只为2个.cpp文件生成.dylib。 I have no idea how to do this for all .cpp files without hard coding. 我不知道如何在没有硬编码的情况下为所有.cpp文件执行此操作。 How should my makefile look? 我的makefile应该怎么样?

# 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

First option you have is to use make wildcard patterns 您拥有的第一个选项是使用make wildcard patterns

The second option is to use a cross-platform tool like CMake and let it generate Makefiles for you. 第二种选择是使用像CMake这样的跨平台工具,让它为您生成Makefile。 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 :) 因此,您将从[大多数]血腥细节中解脱出来,例如精确的编译器和链接器标志等.CMake甚至支持生成MS Visual Studio项目:)

I figured out how to compile everything. 我想出了如何编译所有内容。 I did some research on makefiles, and here was my final makefile: 我对makefile进行了一些研究,这是我最后的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM