I have two c++ programs (mqsim.cpp and ramulator.cpp) which are the main programs of two large projects and I want to use the functions of them in a main.cpp. I have written makefiles for them to create static libraries (libmqsim and libramsim) of them. The libraries are created successfully.
The makefiles to create static libraries are provided as below:
SRCDIR := src
OBJDIR := obj
MAIN := $(SRCDIR)/ramulator.cpp
OUT_DIR=./
OUT=libramsim.a
#This means to extract all cpp files except ramulator.cpp and Gem5Wrapper.cpp --> give all .cpp files
SRCS := $(filter-out $(MAIN) $(SRCDIR)/Gem5Wrapper.cpp, $(wildcard $(SRCDIR)/*.cpp))
#This changes every .cpp name to .o name --> give all .o files
OBJS := $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(SRCS))
# Ramulator currently supports g++ 5.1+ or clang++ 3.4+. It will NOT work with
# g++ 4.x due to an internal compiler error when processing lambda functions.
# CXX := clang++
CXX := g++
# CXX := g++-5
CXXFLAGS := -O3 -std=c++11 -g -Wall
.PHONY: all clean depend
all: depend ramulator
clean:
rm -f ramulator
rm -rf $(OBJDIR)
rm -f $(OUT_DIR)/*.a
depend: $(OBJDIR)/.depend
#create .o files
$(OBJDIR)/.depend: $(SRCS)
@mkdir -p $(OBJDIR) #create obj directory
@rm -f $(OBJDIR)/.depend
@$(foreach SRC, $(SRCS), $(CXX) $(CXXFLAGS) -DRAMULATOR -MM -MT $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(SRC)) $(SRC) >> $(OBJDIR)/.depend ;)
ifneq ($(MAKECMDGOALS),clean)
-include $(OBJDIR)/.depend
endif
ramulator: $(MAIN) $(OBJS) $(SRCDIR)/*.h | depend
ar rcs $(OUT_DIR)/$(OUT) $(OBJS)
ar -t $(OUT_DIR)/$(OUT)
#$(CXX) $(CXXFLAGS) -DRAMULATOR -o $@ $(MAIN) $(OBJS)
#libramulator.a: $(OBJS) $(OBJDIR)/Gem5Wrapper.o
# libtool -static -o $@ $(OBJS) $(OBJDIR)/Gem5Wrapper.o
$(OBJS): | $(OBJDIR)
$(OBJDIR):
@mkdir -p $@
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -DRAMULATOR -c -o $@ $< */
A simple main.cpp program is just written to call the functions of the static libraries. I have two print_help() (in libmqsim) and print()(in libramsim) functions that simply print a message on the console.
The print_help() in the libmqsim is executed with no errors but when I call the print() for libramsim, I get the following error:
In function `main':
main.cpp:(.text+0x1c): undefined reference to `print()'
collect2: error: ld returned 1 exit status
Makefile:38: recipe for target 'main.out' failed
make: *** [main.out] Error 1
I have created ramulator.h and mqsim.h which which include only the function declarations of ramulator.cpp and mqsim.cpp. This is the makefile for the main.cpp that uses the aforementioned libraries: