Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile for building C++ Google Protocol Buffers Project

I Just started fooling around using Google Protocol Buffers and I am trying to incorporate the C++ output files from the protocol buffer compiler into my project. I have Been using a simple makefile for my projects so far and it does the trick for building source files all sharing the same extension. I use ".cpp" for my source files but Google Protocol Buffers outputs its source as ".pb.cc" files. I need to be able to compile and link both types of source files into one executable.

I have been searching and fiddling around with my makefile for a few hours now and have had no success.

My Current Makefile:

PROGRAM_NAME=aserv
CC=gcc
CXX=g++
RM=rm -f
CPPFLAGS=-g  --std=c++14 -O3 -I/usr/local/include/
LDFLAGS=-g -L/usr/local/lib -L/usr/local/lib/boost
LDLIBS= -lrtaudio -pthread -lboost_system -lprotobuf

INSTALL_DIR = /usr/local/bin/

SRCS=$(wildcard *.cpp)
OBJS=$(subst .cpp,.o,$(SRCS))


all: $(PROGRAM_NAME)
 $(PROGRAM_NAME): $(OBJS)
    $(CXX) $(LDFLAGS) -o  $(PROGRAM_NAME) $(OBJS) $(LDLIBS)

depend: .depend

.depend: $(SRCS)
    rm -f ./.depend
    $(CXX) $(CPPFLAGS) -MM $^>>./.depend;

clean:
    $(RM) $(OBJS) $(PROGRAM_NAME) .depend
install:
    cp  $(PROGRAM_NAME) $(INSTALL_DIR)$(PROGRAM_NAME)
uninstall:
    $(RM) $(INSTALL_DIR)$(PROGRAM_NAME)
dist-clean: clean
    $(RM) *~ .depend

include .depend

I am not too well versed in writing makefiles yet, so I just don't quite know what to do to make this work.

If it helps i have GNU make 4.1 and gcc 5.3.1 on Ubuntu 16.04 beta

like image 884
Alex Zywicki Avatar asked Apr 22 '16 06:04

Alex Zywicki


People also ask

Does Google use Protobuf?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.

What is Protobuf C?

This is protobuf-c, a C implementation of the Google Protocol Buffers data serialization format. It includes libprotobuf-c , a pure C library that implements protobuf encoding and decoding, and protoc-c , a code generator that converts Protocol Buffer . proto files to C descriptor code.

What is proto3 used for?

To simplify developer experience and improve runtime efficiency, gRPC APIs should use Protocol Buffers version 3 (proto3) for API definition. Protocol Buffers is a simple language-neutral and platform-neutral Interface Definition Language (IDL) for defining data structure schemas and programming interfaces.


2 Answers

I couldnt get your original Makefile to work so I changed a few things but I think the tricky part with this is the implicit rules that make generates to build your .o files. I think for the .pb.cc files you need to generate .pb.o objects so that the implicit rules can match them.

Try this:

PROGRAM_NAME = aserv
CC = gcc
CXX = g++
RM = rm -f

CXXFLAGS = --std=c++14 -pthread -g -O3 -MMD -MP
CPPFLAGS = -I/usr/local/include/
LDFLAGS = -L/usr/local/lib -L/usr/local/lib/boost
LDLIBS = -lrtaudio -lboost_system -lprotobuf

INSTALL_DIR = /usr/local/bin

SRCS = $(wildcard *.cpp) $(wildcard *.pb.cc)
OBJS = $(subst .pb.cc,.pb.o,$(subst .cpp,.o,$(SRCS)))
DEPS = $(subst .pb.cc,.pb.d,$(subst .cpp,.d,$(SRCS)))

all: $(PROGRAM_NAME)

$(PROGRAM_NAME): $(OBJS)
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)

-include $(DEPS)

clean:
    $(RM) $(OBJS) $(PROGRAM_NAME)

install:
    cp  $(PROGRAM_NAME) $(INSTALL_DIR)

uninstall:
    $(RM) $(INSTALL_DIR)/$(PROGRAM_NAME)

dist-clean: clean
    $(RM) *~  $(DEPS)
like image 168
Galik Avatar answered Sep 27 '22 17:09

Galik


If you wanto to only strictly rely on Make, and not the surrounding shell you can add another set of SRC and OBJ variables, which will serve as a second set of dependencies.

Add these right below the first set:

SRC1=$(wildcard *.pb.cc)
OBJ1=$(subst .pb.cc,.o,$(SRC1))

And change the .depend and $(PROGRAM_NAME) rule:

.depend: $(SRCS) $(SRC1)


$(PROGRAM_NAME): $(OBJS) $(OBJ1)
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@


%.o: %.c
    $(CC) $(ALL_CFLAGS) $(ALL_CPPFLAGS) -c -o $@ $<
like image 36
Leandros Avatar answered Sep 27 '22 17:09

Leandros