-1

I have Makefile

default_target: all
.PHONY : default_target
all: c11parser
clean:
    rm -r build
.PHONY : clean
c11parser: build/c11main.o build/C11
    c++ -o c11parser build/c11main.o -Ithirdparty/cpp_runtime/runtime/src build/C11/C11Lexer.o build/C11/C11Parser.o -lantlr4-runtime -Lthirdparty/cpp_runtime/dist -Wl,-rpath=thirdparty/cpp_runtime/dist
build/c11main.o: c11main.cpp build/C11/gen
    c++ -c c11main.cpp -o build/c11main.o -Ithirdparty/cpp_runtime/runtime/src -Ibuild/C11/gen
build/C11: build/C11/gen
    c++ -c build/C11/gen/C11Lexer.cpp -o build/C11/C11Lexer.o -Ithirdparty/cpp_runtime/runtime/src
    c++ -c build/C11/gen/C11Parser.cpp -o build/C11/C11Parser.o -Ithirdparty/cpp_runtime/runtime/src
build/C11/gen: C11.g4
    java -jar thirdparty/antlr/antlr-4.12.0-complete.jar -Werror -Dlanguage=Cpp -listener -visitor -o build/C11/gen -package C11 C11.g4

Tool Antlr4 generates classes in build/C11/gen directory.
C++ runtime library has both, shared and static libraries in directory thirdparty/cpp_runtime/dist:

-rw-rw-r-- 1 andrzej andrzej 2920088 Apr  5 13:43 libantlr4-runtime.a
-rwxrwxr-x 1 andrzej andrzej 1514240 Apr  5 13:43 libantlr4-runtime.so
-rwxrwxr-x 1 andrzej andrzej 1514240 Apr  5 13:43 libantlr4-runtime.so.4.12.0

Tool Antrl4 generates files from grammar.

  1. How to choose static, not shared library?
  2. How to generate executable already striped
  3. Striped parsed has 700K although has shared library , why so big?
  4. I have commands: c++ -c path1/file.cpp -o path2/file.o, it is inefficient to call compiler for creating one object file, but because path1 is not path2, I must. Better will without -o, results in the same folder, next move object file to other folder?
Saku
  • 383
  • 2
  • 12
  • as for question 2. - have a look here https://stackoverflow.com/questions/1349166/what-is-the-difference-between-gcc-s-and-a-strip-command – Synopsis Apr 05 '23 at 14:32
  • as for question 1. -you could just add the static lib *.s file to the linking list instead of '-l'ing it – Synopsis Apr 05 '23 at 14:36

1 Answers1

1

If you rely on automatic rules, your life becomes a lot simpler:

# Make these headers available for all g++ invocations
CXXFLAGS = -Ithirdparty/cpp_runtime/runtime/src -Ibuild/C11/gen

# Statically link the antlr4 runtime
LDFLAGS = -Lthirdparty/cpp_runtime/dist -static -lantlr4-runtime 

# List of files generated by antlr
GENERATED_SRC = build/C11/gen/C11Lexer.cpp build/C11/gen/C11Parser.cpp

# First target in the Makefile is the default target
c11parser: build/c11main.o $(GENERATED_SRC:.cpp=.o)
  $(CXX) -o $@ $^ $(LDFLAGS)
  strip $@

# but if you insist ...
all: c11parser
.PHONY: all clean


# This teaches Make how to build any .cpp file to an object file under build/
build/%.o: %.cpp
  $(CXX) -o $@ $^ $(CXXFLAGS)

$(GENERATED_SRC): C11.g4
  java -jar thirdparty/antlr/antlr-4.12.0-complete.jar -Werror -Dlanguage=Cpp -listener -visitor -o build/C11/gen -package C11 $^

Note there are no rules for compiling the files in build/C11/gen, because we build them in the same directory. If you do not want that you can use a similar trick as the build/%.o rule.

Botje
  • 26,269
  • 3
  • 31
  • 41