0

I am having trouble compiling using a makefile and including gflags. I have not done Makefiles in a while. The compile error I am getting is related to gflags

This is my makefile:

CXX = g++
CPPFlags = -g -Wall -std=c++11
LDLIBS = -lgflags
pa1: Main.cpp PA1.o Node.o
    $(CXX) $(CPPFlags) $(LDFLAGS) Main.cpp Node.o PA1.o -o PA1
PA1.o:PA1.h PA1.cpp
    $(CXX) $(CPPFlags) -c PA1.cpp -o PA1.o
Node.o:Node.h Node.cpp
    $(CXX) $(CPPFlags) -c Node.cpp -o Node.o

This is my main.cpp

#include "PA1.h"
#include <string>
#include <iostream>
#include <fstream>
#include <string>
#include <gflags/gflags.h>
DEFINE_string(root, "0,0", "Root");
int main(int argc, char *argv[])
{
   gflags::ParseCommandLineFlags(&argc, &argv, true);
   PA1 run= PA1(argv[2]);
   std::string rc=FLAGS_root;
   int r= rc[0];
   int c= rc[2];
   if(run.ReadMaze()==-1)
   {
     return -1;
   }
   run.SolveMaze(r,c);
   return 0;
}

edit: This is the error message

g++ -g -Wall -std=c++11  Main.cpp Node.o PA1.o -o PA1
/tmp/ccIdQf46.o: In function `main':
/home/peteryan/Documents/Main.cpp:10: undefined reference to 
`google::ParseCommandLineFlags(int*, char***, bool)'
/tmp/ccIdQf46.o: In function `__static_initialization_and_destruction_0(int, 
int)':
/home/peteryan/Documents/Main.cpp:7: undefined reference to 
`google::FlagRegisterer::FlagRegisterer(char const*, char const*, char 
const*, char const*, void*, void*)'
collect2: error: ld returned 1 exit status
Makefile:5: recipe for target 'pa1' failed
make: *** [pa1] Error 1

1 Answers1

1

The linker does not appear to be linking with with -lgflags and your Makefile is likely the cause. You should put your LDLIBS at the end of your build arguments. This generic Makefile will compile all the .cpp sources and link all the object files in the same directory. It also should track your header file dependencies. Notice the LDLIBS at the end of the build directive. Give it a try for your program.

program_name := PA1

CXX := g++
CXXFLAGS := —g -Wall std=c++11
LDLIBS := -lgflags

source_files := $(wildcard *.cpp)
objects := ${source_files:.cpp=.o}

all: $(program_name)

$(program_name): $(objects)
    $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $(program_name) $(objects) $(LDLIBS)

depend: .depend

.depend: $(source_files)
    rm -f ./.depend
    $(CXX) $(CXXFLAGS) -MM $^>>./.depend;

clean:
    rm -f $(objects)

distclean: clean
    rm -f *~ .depend

include .depend
Justin Randall
  • 2,243
  • 2
  • 16
  • 23