如何指示Makefile使用不同的编译器

发布于 2024-09-06 04:55:57 字数 1474 浏览 3 评论 0原文

我必须使用两个不同的编译器和库同时为两个不同的平台构建代码。我怎样才能用单个 make 文件来做到这一点。

目前我的 makefile 内容如下。我如何指示它在同一个 makefile 中将编译器更改为 gcc?

VPATH = /root/Documents/project/src 

CROSS_COMPILE  = /opt/compilers/bin/compiler1
CXX = $(CROSS_COMPILE)-g++
CC = $(CROSS_COMPILE)-gcc
AS = $(CROSS_COMPILE)-as
AR = $(CROSS_COMPILE)-ar
NM = $(CROSS_COMPILE)-nm
LD = $(CROSS_COMPILE)-ld
OBJDUMP = $(CROSS_COMPILE)-objdump
OBJCOPY = $(CROSS_COMPILE)-objcopy
RANLIB = $(CROSS_COMPILE)-ranlib
STRIP = $(CROSS_COMPILE)-strip


CFLAGS       = -c -Wall -D_REENTRANT -DACE_HAS_AIO_CALLS -D_GNU_SOURCE -DACE_HAS_EXCEPTIONS -D__ACE_INLINE__  
LDFLAGS      = -L. -L/etc/ACE_wrappers/lib 
CPPFLAGS     += -I/etc/ACE_wrappers -I/etc/ACE_wrappers/ace 
LDLIBS       = -lACE

OUTPUT_DIRECTORY=/root/Documents/bin
OBJ=/root/Documents/obj

ifneq ($(OUTPUT_DIRECTORY),)
all: $(OUTPUT_DIRECTORY)
$(OUTPUT_DIRECTORY):
    -@$(MKDIR) "$(OUTPUT_DIRECTORY)"
endif

ifneq ($(OBJ),)
all: $(OBJ)
$(OBJ_DIR):
    -@$(MKDIR) "$(OBJ)"
endif

SOURCES=File_1.cpp File_2.cpp 

OBJECTS=$(SOURCES:%.cpp=$(OBJ)/%.o)

$(OBJ)/%.o: %.cpp
    @echo Building Objects
    $(CC) $(CFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<


EXECUTABLE=MyExecutable

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    @echo $(SOURCES)
    $(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $(OUTPUT_DIRECTORY)/$@

File_1.o:File_1.cpp
File_1.o:File_1.cpp


.PHONY: clean
clean:
    rm $(OBJECTS) $(OUTPUT_DIRECTORY)/$(EXECUTABLE) 

I have to build my code for two different platforms at once using two different compilers and libraries. How can I do this with single make file.

Currently my makefile contents are given below. How can I instruct it to change the compiler to gcc within the same makefile?

VPATH = /root/Documents/project/src 

CROSS_COMPILE  = /opt/compilers/bin/compiler1
CXX = $(CROSS_COMPILE)-g++
CC = $(CROSS_COMPILE)-gcc
AS = $(CROSS_COMPILE)-as
AR = $(CROSS_COMPILE)-ar
NM = $(CROSS_COMPILE)-nm
LD = $(CROSS_COMPILE)-ld
OBJDUMP = $(CROSS_COMPILE)-objdump
OBJCOPY = $(CROSS_COMPILE)-objcopy
RANLIB = $(CROSS_COMPILE)-ranlib
STRIP = $(CROSS_COMPILE)-strip


CFLAGS       = -c -Wall -D_REENTRANT -DACE_HAS_AIO_CALLS -D_GNU_SOURCE -DACE_HAS_EXCEPTIONS -D__ACE_INLINE__  
LDFLAGS      = -L. -L/etc/ACE_wrappers/lib 
CPPFLAGS     += -I/etc/ACE_wrappers -I/etc/ACE_wrappers/ace 
LDLIBS       = -lACE

OUTPUT_DIRECTORY=/root/Documents/bin
OBJ=/root/Documents/obj

ifneq ($(OUTPUT_DIRECTORY),)
all: $(OUTPUT_DIRECTORY)
$(OUTPUT_DIRECTORY):
    -@$(MKDIR) "$(OUTPUT_DIRECTORY)"
endif

ifneq ($(OBJ),)
all: $(OBJ)
$(OBJ_DIR):
    -@$(MKDIR) "$(OBJ)"
endif

SOURCES=File_1.cpp File_2.cpp 

OBJECTS=$(SOURCES:%.cpp=$(OBJ)/%.o)

$(OBJ)/%.o: %.cpp
    @echo Building Objects
    $(CC) $(CFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ 
lt;


EXECUTABLE=MyExecutable

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    @echo $(SOURCES)
    $(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $(OUTPUT_DIRECTORY)/$@

File_1.o:File_1.cpp
File_1.o:File_1.cpp


.PHONY: clean
clean:
    rm $(OBJECTS) $(OUTPUT_DIRECTORY)/$(EXECUTABLE) 

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

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

发布评论

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

评论(3

晒暮凉 2024-09-13 04:55:57

我首先将所有特定于平台的定义放在一个单独的 makefile 中。这样你就可以这样做:

include $(TARGET).make

$(TARGET).make 为每个特定平台定义 CC 和其他变量。然后你可以递归调用 make 将 TARGET 设置为你想要的。比如:

build:
    $(MAKE) TARGET=platform1 all
    $(MAKE) TARGET=platform2 all

但实际上有很多很多方法可以实现同样的目标。

I'd start by putting all the platform-specific defines in a separate makefile. That way you can do:

include $(TARGET).make

Where $(TARGET).make defines CC and other variables for each particular platform. Then you can call make recursively setting TARGET to what you want. Something like:

build:
    $(MAKE) TARGET=platform1 all
    $(MAKE) TARGET=platform2 all

But really there are many, many ways in which you can achieve the same thing.

染墨丶若流云 2024-09-13 04:55:57

使用自动配置。当您使用 ./configure 配置项目时,它会自动选择可用的项目。

Use autoconf. When you configure the project with ./configure, it will automatically choose the one available.

清醇 2024-09-13 04:55:57

如果您的帖子中正确保留了换行符,那么阅读起来会容易得多。

不管怎样,我希望你必须有 Makefile 中许多内容的两个副本,这样它的形式

all : all-platform1 all-platform2

stuff-platform1 : requirements
    $(CC1) whatever
stuff-platform2 : requirements
    $(CC2) whatever

就是你可能能够像这样自动生成一个 makefile,以节省一些精力尽管。

That would be a lot easier to read if the linebreaks were properly preserved in your post.

Anyway, I expect that you're going to have to have two copies of a number of things in the Makefile, such that it's of the form

all : all-platform1 all-platform2

stuff-platform1 : requirements
    $(CC1) whatever
stuff-platform2 : requirements
    $(CC2) whatever

It's possible that you might be able to auto-generate a makefile like this, to save some effort though.

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