0

我在使用 makefile 编译并包含 gflags 时遇到问题。我有一段时间没有做 Makefiles 了。我得到的编译错误与 gflags 有关

这是我的生成文件:

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

这是我的 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;
}

编辑:这是错误消息

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
4

1 回答 1

0

链接器似乎没有与 with 链接,-lgflags并且您的 Makefile 可能是原因。您应该将您LDLIBS的放在构建参数的末尾。这个通用 Makefile 将编译所有 .cpp 源并链接同一目录中的所有目标文件。它还应该跟踪您的头文件依赖项。请注意LDLIBS构建指令末尾的 。试试你的程序。

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
于 2018-01-27T01:57:59.437 回答