1

我有这个目录结构:

 - src
  - /GUILayer
  - /PhysicLayer
  - /LogicLayer
  - /Utilities
  - main.cpp
  - makefile
  - makefile.inc

这是我目前makefile的内部src

include makefile.inc

DIRS    = PhysicLayer LogicLayer GUILayer Utilities
OBJLIBS = PhysicLayer.a LogicLayer.a GUILayer.a Utilities.a

EXTERNOBJ =  $(foreach path, $(DIRS), $(wildcard $(path)/*.o))

TARGETS = main.cpp 
EXE = main

all : $(EXE)

%.a :
    -for d in $(DIRS); do (cd $$d; $(MAKE)); done
    ar cr $@ $^

main: $(OBJLIBS)
    $(ECHO) $(CC) $(CFLAGS) $(EXTERNOBJ) $(OBJLIBS)  -o $@ $(TARGETS)
    $(CC) $(CFLAGS) $(EXTERNOBJ) $(OBJLIBS)  -o $@ $(TARGETS) 

clean :
    -$(RM) -f ./main
    find . -name "*.o" -type f -print | xargs /bin/rm -f
    find . -name "*.a" -type f -print | xargs /bin/rm -f

文件夹中的文件生成 .o 就好了,但main根本不生成。这是终端的输出:

g++ -Wall -pedantic -pedantic-errors -g -ggdb PhysicLayer.a LogicLayer.a GUILayer.a Utilities.a -o main main.cpp
/tmp/ccA3VJed.o: In function `main':
/home/user/Desktop/project/src/main.cpp:14: undefined reference to `Indexador::Indexador()'
/home/user/Desktop/project/src/main.cpp:19: undefined reference to `Indexador::indexar(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/user/Desktop/project/src/main.cpp:38: undefined reference to `Indexador::~Indexador()'
/home/user/Desktop/project/src/main.cpp:38: undefined reference to `Indexador::~Indexador()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
4

1 回答 1

2

在您的规则中,$(CC) $(CFLAGS) $(EXTERNOBJ) -c $@ $(TARGETS)您应该更改-c-o,否则g++将假定这$@是一个输入文件并查找main尚不存在的 。

于 2013-04-26T05:39:08.887 回答