源文件具有不同编译器标志的 Makefile

发布于 2025-01-07 12:03:09 字数 623 浏览 0 评论 0原文

如何对 Makefile 中的不同源文件使用不同的编译器标志?例如,我想要一个生成的 Makefile:

g++ -c -COMPILER_FLAGS_1   -g source1.cpp -o source1.o

g++ -c -COMPILER_FLAGS_2   -g source2.cpp -o source2.o
g++ -c -COMPILER_FLAGS_2   -g source3.cpp -o source3.o
g++ -c -COMPILER_FLAGS_2   -g source4.cpp -o source4.o

g++ -c -COMPILER_FLAGS_3   -g source5.cpp -o source5.o
g++ -c -COMPILER_FLAGS_3   -g source6.cpp -o source6.o
g++ -c -COMPILER_FLAGS_3   -g source7.cpp -o source7.o

g++ -g -o output source1.o source2.o source3.o source4.o source5.o source6.o source7.o 

目前我有大约 20 个源文件(并且预计会增长),因此一个易于维护的文件会很好。

提前致谢。

How do I use different compiler flags for different source files in a Makefile? For example, I'd like a Makefile that will produce:

g++ -c -COMPILER_FLAGS_1   -g source1.cpp -o source1.o

g++ -c -COMPILER_FLAGS_2   -g source2.cpp -o source2.o
g++ -c -COMPILER_FLAGS_2   -g source3.cpp -o source3.o
g++ -c -COMPILER_FLAGS_2   -g source4.cpp -o source4.o

g++ -c -COMPILER_FLAGS_3   -g source5.cpp -o source5.o
g++ -c -COMPILER_FLAGS_3   -g source6.cpp -o source6.o
g++ -c -COMPILER_FLAGS_3   -g source7.cpp -o source7.o

g++ -g -o output source1.o source2.o source3.o source4.o source5.o source6.o source7.o 

At the moment I've got about 20 source files (and that's expected to grow), so an easy to maintain file would be nice.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰之心 2025-01-14 12:03:09

您可以执行类似以下操作(未经测试,因此语法可能略有偏差):

OBJS_1 := source1.o
OBJS_2 := source2.o source3.o source4.o
OBJS_3 := source5.o source6.o source7.o

OBJS := $(OBJS_1) $(OBJS_2) $(OBJS_3)

output: $(OBJS)
    $(CXX) -g -o $@ $^

$(OBJS_1): CXXFLAGS := $(COMPILER_FLAGS_1)
$(OBJS_2): CXXFLAGS := $(COMPILER_FLAGS_2)
$(OBJS_3): CXXFLAGS := $(COMPILER_FLAGS_3)

$(OBJS): %.o: %.cpp
    $(CXX) -c $(CXXFLAGS) -g 
lt; -o $@

You could do something like the following (untested, so the syntax might be slightly off):

OBJS_1 := source1.o
OBJS_2 := source2.o source3.o source4.o
OBJS_3 := source5.o source6.o source7.o

OBJS := $(OBJS_1) $(OBJS_2) $(OBJS_3)

output: $(OBJS)
    $(CXX) -g -o $@ $^

$(OBJS_1): CXXFLAGS := $(COMPILER_FLAGS_1)
$(OBJS_2): CXXFLAGS := $(COMPILER_FLAGS_2)
$(OBJS_3): CXXFLAGS := $(COMPILER_FLAGS_3)

$(OBJS): %.o: %.cpp
    $(CXX) -c $(CXXFLAGS) -g 
lt; -o $@
霓裳挽歌倾城醉 2025-01-14 12:03:09

这是我在 Solaris LINUX 上编写和测试的 UNIX/LINUX makefile,用于处理 GNUmakefile 不同部分的不同编译标志。如果可以改进,请告诉我。谢谢

# GNUmakefile
#
# makefile for mdRightFielder
#
# Builds:
#   libmdRightFielder.so or libmdRightFielder.sl

ifndef SUB
include ../header.mk

else

VPATH=../Source:../Source/PCRE:../Source/SQLite:../../cpswindows/Source:../../util/mdLicense
INCL=-I../Include -I../Include/PCRE -I../Include/SQLite -I../../cpswindows/Include -I ../../util -I../../util/mdLicense
APIOBJ=cGlobalDataDestructor.o cPCRE.o CppInterface.o cRightFielder-FillTokenGaps.o
PCREOBJ=pcre_chartables.o pcre_compile.o pcre_exec.o pcre_fullinfo.o pcre_get.o pcre_globals.o pcre_newline.o \
    pcre_tables.o pcre_try_flipped.o
SQLITEOBJ=sqlite3.o
CPSWINDOWSOBJ=BinarySearch.o cConfigFile.o cCriticalSection.o cDateTime.o cException.o cFile.o cSQLite.o \
    QuickSort.o StringFunctions.o
MDLICENSEOBJ=CBigNum.o mdLicense.o RSA.o

ifeq ($(CPU),sparc)
    ifdef workshop
        CALIGN=-xmemalign=1s 
        ifdef release
            CXXALIGN=-Qoption cg -xmemalign=1s
        else
            CXXALIGN=-Qoption ccfe -y-xmemalign=1s
        endif
    endif
endif

COMPILER_FLAGS_1=-D_NO_GUI 
COMPILER_FLAGS_2=-D_NO_GUI -DHAVE_CONFIG_H 


CXXFLAGS+=-DPCRE_STATIC -DUSE_STATIC
CFLAGS+=-D_NO_GUI -DHAVE_CONFIG_H -DPCRE_STATIC -DUSE_STATIC
DEPFLAGS+=-DLINK_SIZE=2 -D_NO_GUI -DHAVE_CONFIG_H -DPCRE_STATIC -DUSE_STATIC

.PHONY: all clean

all:    libmdRightFielder.so
    cp -fp ../Include/mdRightFielder.h ../../util/mdEnums.h libmdRightFielder.so $(SHIP)
    if [ `uname` = HP-UX ] ; \
    then \
      /bin/mv -f $(SHIP)/libmdRightFielder.so $(SHIP)/libmdRightFielder.sl ; \
    fi

clean:
    rm -f *.o *.so *.sl deps
    rm -f core core.[0-9]*

$(APIOBJ): CXXFLAGS+=$(COMPILER_FLAGS_1) 
$(PCREOBJ): CXXFLAGS+=$(COMPILER_FLAGS_2) 

MARYOBJS = $(APIOBJ) $(PCREOBJ)



libmdRightFielder.so: \
    $(MARYOBJS)
     -$(CXX) $(CXXFLAGS) $(INCL) $(SHARED) $^ -o $@ $(LDLIBS)

mary:
    %.o : %.cpp  # cancel implicit CPP compilation rule

        %.o :  %.cpp
    $(CXX) $(CXXFLAGS) $(INCL) $(PIC) 
lt; -o $@ -c


        %.o : %.c     # cancel implicit C compilation rule

        %.o : %.c
    $(CC) $(CFLAGS) $(INCL) $(PIC) 
lt; -o $@ -c

endif

Here is UNIX/LINUX makefile which I wrote and tested on Solaris LINUX to handle different compilation flags for different sections of a GNUmakefile. Please let me know if it can be improved. Thank you

# GNUmakefile
#
# makefile for mdRightFielder
#
# Builds:
#   libmdRightFielder.so or libmdRightFielder.sl

ifndef SUB
include ../header.mk

else

VPATH=../Source:../Source/PCRE:../Source/SQLite:../../cpswindows/Source:../../util/mdLicense
INCL=-I../Include -I../Include/PCRE -I../Include/SQLite -I../../cpswindows/Include -I ../../util -I../../util/mdLicense
APIOBJ=cGlobalDataDestructor.o cPCRE.o CppInterface.o cRightFielder-FillTokenGaps.o
PCREOBJ=pcre_chartables.o pcre_compile.o pcre_exec.o pcre_fullinfo.o pcre_get.o pcre_globals.o pcre_newline.o \
    pcre_tables.o pcre_try_flipped.o
SQLITEOBJ=sqlite3.o
CPSWINDOWSOBJ=BinarySearch.o cConfigFile.o cCriticalSection.o cDateTime.o cException.o cFile.o cSQLite.o \
    QuickSort.o StringFunctions.o
MDLICENSEOBJ=CBigNum.o mdLicense.o RSA.o

ifeq ($(CPU),sparc)
    ifdef workshop
        CALIGN=-xmemalign=1s 
        ifdef release
            CXXALIGN=-Qoption cg -xmemalign=1s
        else
            CXXALIGN=-Qoption ccfe -y-xmemalign=1s
        endif
    endif
endif

COMPILER_FLAGS_1=-D_NO_GUI 
COMPILER_FLAGS_2=-D_NO_GUI -DHAVE_CONFIG_H 


CXXFLAGS+=-DPCRE_STATIC -DUSE_STATIC
CFLAGS+=-D_NO_GUI -DHAVE_CONFIG_H -DPCRE_STATIC -DUSE_STATIC
DEPFLAGS+=-DLINK_SIZE=2 -D_NO_GUI -DHAVE_CONFIG_H -DPCRE_STATIC -DUSE_STATIC

.PHONY: all clean

all:    libmdRightFielder.so
    cp -fp ../Include/mdRightFielder.h ../../util/mdEnums.h libmdRightFielder.so $(SHIP)
    if [ `uname` = HP-UX ] ; \
    then \
      /bin/mv -f $(SHIP)/libmdRightFielder.so $(SHIP)/libmdRightFielder.sl ; \
    fi

clean:
    rm -f *.o *.so *.sl deps
    rm -f core core.[0-9]*

$(APIOBJ): CXXFLAGS+=$(COMPILER_FLAGS_1) 
$(PCREOBJ): CXXFLAGS+=$(COMPILER_FLAGS_2) 

MARYOBJS = $(APIOBJ) $(PCREOBJ)



libmdRightFielder.so: \
    $(MARYOBJS)
     -$(CXX) $(CXXFLAGS) $(INCL) $(SHARED) $^ -o $@ $(LDLIBS)

mary:
    %.o : %.cpp  # cancel implicit CPP compilation rule

        %.o :  %.cpp
    $(CXX) $(CXXFLAGS) $(INCL) $(PIC) 
lt; -o $@ -c


        %.o : %.c     # cancel implicit C compilation rule

        %.o : %.c
    $(CC) $(CFLAGS) $(INCL) $(PIC) 
lt; -o $@ -c

endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文