如何编写makefile混合C和C ++ [英] how to write makefile mixing C and C++

查看:342
本文介绍了如何编写makefile混合C和C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个Makefile中,我不知道如何编译C对象在同一个Makefile混合C和C ++。如果我首先编译C对象,然后运行这个Makefile,它的工作原理。任何人都可以帮助解决它为我?提前致谢!

In this Makefile, I don't know how to compile out c objects in the same Makefile mixing C and C++. If I first compile the C objects and then run this Makefile, it works. Can anyone help to fix it for me? Thanks in advance!

CXX = g++
CXXFLAGS = -Wall -D__STDC_LIMIT_MACROS


SERVER_SRC = \
    main.cpp

SERVER_SRC_OBJS = ${SERVER_SRC:.cpp=.o}


REDIS_SRC = \
    $(HIREDIS_FOLDER)/net.c \
    $(HIREDIS_FOLDER)/hiredis.c \
    $(HIREDIS_FOLDER)/sds.c \
    $(HIREDIS_FOLDER)/async.c

REDIS_SRC_OBJS = ${REDIS_SRC:.c=.o}


.SUFFIXES:
.SUFFIXES: .o .cpp
.cpp.o:
    $(CXX) $(CXXFLAGS) -I$(HIREDIS_FOLDER) \
    -c $< -o $*.o


all: server

net.o: net.c fmacros.h net.h hiredis.h
async.o: async.c async.h hiredis.h sds.h dict.c dict.h
hiredis.o: hiredis.c fmacros.h hiredis.h net.h sds.h
sds.o: sds.c sds.h


server: $(SERVER_SRC_OBJS) $(REDIS_SRC_OBJS)
    mkdir -p bin
    $(CXX) $(CXXFLAGS) -o bin/redis_main \
    -I$(HIREDIS_FOLDER) \
    $(REDIS_SRC_OBJS) \
    $(SERVER_SRC_OBJS) \
    -lpthread \
    -lrt \
    -Wl,-rpath,./


.PHONY: clean
clean:
    $(RM) -r bin/redis_main
    $(RM) ./*.gc??
    $(RM) $(SERVER_SRC_OBJS)
    $(RM) $(REDIS_SRC_OBJS)


推荐答案

G ++可以并且将编译.c和.cpp源文件,只是很好。

G++ can and will compile both .c and .cpp source files just fine.

需要做的是为服务器目标添加依赖关系。例如:

What you really need to do is add dependencies for "server" target. For example:

OBJ = net.o hiredis.o sds.o async.o

...

all: server

server: $(OBJ)


b $ b

此Howto 中有一些非常好的提示。

There are some really good tips in this Howto.

这篇关于如何编写makefile混合C和C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆