2014-01-31 31 views
0

我在使用makefile编译我的CS hw时遇到问题。这将编译代码块,但没有用gcc ...使用makefile编译C++项目的问题

- >这些文件都设在这里...... https://github.com/ericmwalsh/cppSortedList



的文件是...

SortedList.h

SortedList.cpp(包括SortedList.h)

PCTimer.h

fileListFunctions.cpp(包括PCTimer.h和SortedList.cpp)

testSortedList.cpp(包括fileListFunctions.cpp)



现在我知道我不应该包括内cpp文件对方,但由于某种原因,我无法用我的makefile:

testmake:SortedList.cpp fileListFunctions.cpp testSortedList.cpp
的gcc -o testmake SortedList.cpp fileListFunctions.cpp testSortedList.cpp -I。



我得到的错误如下:

gcc -o testmake SortedList.cpp fileListFunctions.cpp testSortedList.cpp -I. 
In file included from SortedList.cpp:9: 
./SortedList.h:41:17: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions] 
    int capacity=45500, size=0; 
       ^
./SortedList.h:41:29: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions] 
    int capacity=45500, size=0; 
          ^
SortedList.cpp:138:1: warning: control may reach end of non-void function [-Wreturn-type] 
} 
^ 
3 warnings generated. 
In file included from fileListFunctions.cpp:10: 
./PCTimer.h:32:9: error: use of undeclared identifier 'cerr' 
     cerr << "attempt to start an already running stopwatch" << endl; 
     ^
./PCTimer.h:32:68: error: use of undeclared identifier 'endl' 
     cerr << "attempt to start an already running stopwatch" << endl; 
                   ^
./PCTimer.h:43:9: error: use of undeclared identifier 'cerr' 
     cerr << "attempt to stop a non-running stopwatch" << endl; 
     ^
./PCTimer.h:43:62: error: use of undeclared identifier 'endl' 
     cerr << "attempt to stop a non-running stopwatch" << endl; 
                  ^
fileListFunctions.cpp:12:38: error: unknown type name 'SortedArrayList' 
void insertAllWords(char * fileName, SortedArrayList * inp){ 
            ^
fileListFunctions.cpp:15:5: error: use of undeclared identifier 'std' 
    std::ifstream infile(fileName); 
    ^
fileListFunctions.cpp:16:10: error: use of undeclared identifier 'std' 
    for(std::string line; getline(infile, line);){ 
     ^
fileListFunctions.cpp:16:37: error: use of undeclared identifier 'infile' 
    for(std::string line; getline(infile, line);){ 
            ^
fileListFunctions.cpp:18:9: error: use of undeclared identifier 'std' 
     std::transform(line.begin(), line.end(), line.begin(), ::tolower); 
     ^
fileListFunctions.cpp:21:5: error: use of undeclared identifier 'infile' 
    infile.close(); 
    ^
fileListFunctions.cpp:23:5: error: use of undeclared identifier 'cout' 
    cout << "SortedArrayList: Time to insertAllWords from " << fileName << ": " << t.elapsedTime() << " secs" << endl; 
    ^
fileListFunctions.cpp:23:114: error: use of undeclared identifier 'endl' 
    cout << "SortedArrayList: Time to insertAllWords from " << fileName << ": " << t.elapsedTime() << " secs" << endl; 
                               ^
fileListFunctions.cpp:25:36: error: unknown type name 'SortedArrayList' 
bool findAllWords(char * fileName, SortedArrayList * inp){ 
           ^
fileListFunctions.cpp:28:5: error: use of undeclared identifier 'std' 
    std::ifstream infile(fileName); 
    ^
fileListFunctions.cpp:29:10: error: use of undeclared identifier 'std' 
    for(std::string line; getline(infile, line);){ 
     ^
fileListFunctions.cpp:29:37: error: use of undeclared identifier 'infile' 
    for(std::string line; getline(infile, line);){ 
            ^
fileListFunctions.cpp:31:9: error: use of undeclared identifier 'std' 
     std::transform(line.begin(), line.end(), line.begin(), ::tolower); 
     ^
fileListFunctions.cpp:34:5: error: use of undeclared identifier 'infile' 
    infile.close(); 
    ^
fileListFunctions.cpp:36:5: error: use of undeclared identifier 'cout' 
    cout << "SortedArrayList: Time to findAllWords from " << fileName << ": " << t.elapsedTime() << " secs" << endl; 
    ^
fatal error: too many errors emitted, stopping now [-ferror-limit=] 
20 errors generated. 
testSortedList.cpp:12:5: error: unknown type name 'SortedArrayList' 
    SortedArrayList * a = new SortedArrayList(); 
    ^
testSortedList.cpp:12:31: error: unknown type name 'SortedArrayList' 
    SortedArrayList * a = new SortedArrayList(); 
          ^
testSortedList.cpp:22:5: error: use of undeclared identifier 'cout' 
    cout << endl << "AFTER REMOVALS:" << endl << endl; 
    ^
testSortedList.cpp:22:13: error: use of undeclared identifier 'endl' 
    cout << endl << "AFTER REMOVALS:" << endl << endl; 
      ^
testSortedList.cpp:22:42: error: use of undeclared identifier 'endl' 
    cout << endl << "AFTER REMOVALS:" << endl << endl; 
             ^
testSortedList.cpp:22:50: error: use of undeclared identifier 'endl' 
    cout << endl << "AFTER REMOVALS:" << endl << endl; 
               ^
testSortedList.cpp:34:5: error: use of undeclared identifier 'cout' 
    cout << endl << "AFTER INSERTS:" << endl << endl; 
    ^
testSortedList.cpp:34:13: error: use of undeclared identifier 'endl' 
    cout << endl << "AFTER INSERTS:" << endl << endl; 
      ^
testSortedList.cpp:34:41: error: use of undeclared identifier 'endl' 
    cout << endl << "AFTER INSERTS:" << endl << endl; 
             ^
testSortedList.cpp:34:49: error: use of undeclared identifier 'endl' 
    cout << endl << "AFTER INSERTS:" << endl << endl; 
               ^
testSortedList.cpp:39:5: error: unknown type name 'SortedLinkedList' 
    SortedLinkedList * a = new SortedLinkedList(); 
    ^
testSortedList.cpp:39:32: error: unknown type name 'SortedLinkedList' 
    SortedLinkedList * a = new SortedLinkedList(); 
          ^
testSortedList.cpp:49:5: error: use of undeclared identifier 'cout' 
    cout << endl << endl << "REMOVED AFTER THIS :" << endl << endl; 
    ^
testSortedList.cpp:49:13: error: use of undeclared identifier 'endl' 
    cout << endl << endl << "REMOVED AFTER THIS :" << endl << endl; 
      ^
testSortedList.cpp:49:21: error: use of undeclared identifier 'endl' 
    cout << endl << endl << "REMOVED AFTER THIS :" << endl << endl; 
        ^
testSortedList.cpp:49:55: error: use of undeclared identifier 'endl' 
    cout << endl << endl << "REMOVED AFTER THIS :" << endl << endl; 
                ^
testSortedList.cpp:49:63: error: use of undeclared identifier 'endl' 
    cout << endl << endl << "REMOVED AFTER THIS :" << endl << endl; 
                  ^
testSortedList.cpp:57:5: error: use of undeclared identifier 'cout' 
    cout << "Beginning the testing with SortedArrayList..." << endl << endl; 
    ^
testSortedList.cpp:57:64: error: use of undeclared identifier 'endl' 
    cout << "Beginning the testing with SortedArrayList..." << endl << endl; 
                  ^
fatal error: too many errors emitted, stopping now [-ferror-limit=] 
20 errors generated. 
make: *** [testmake] Error 1 
+0

你为什么要包括源文件到源文件?这不是你如何使用多个源文件。您将每个源文件编译为目标文件,然后将它们链接在一起。 –

+0

您需要在C++ 11模式下使用标记“std = C++ 11”进行编译。然后你需要在std ::'前加上std lib的东西。 – juanchopanza

+1

这似乎也需要一个[良好的图书清单](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list),因为大多数列出的错误似乎是初学者的错误。 –

回答

1

你需要的所有相关文件(.cpp)编译成第一(.o)对象。并将其与最终对象(main.o)链接以形成可执行文件。

看一看How to make a SIMPLE C++ Makefile?

这将工作,

#Declaration and Definition of Macros which are used in make 
CPP = g++ 
OBJ = SortedList.o fileListFunctions.o testSortedList.o 
INCS = -I"include" -I"include" -I"." 
BIN=testmake 
CXXFLAGS=$(INCS) -O3 -Wall 
RM = rm -f 

.PHONY: all all-before all-after clean clean-custom 

all: all-before $(BIN) all-after 

clean: clean-custom 
    ${RM} $(OBJ) $(BIN) 

$(BIN): $(OBJ) 
    $(CPP) $(OBJ) -o $(BIN) $(LIBS) 

SortedList.o: SortedList.cpp 
    $(CPP) -c SortedList.cpp -o [email protected] $(CXXFLAGS) 

fileListFunctions.o: fileListFunctions.cpp 
    $(CPP) -c fileListFunctions.cpp -o [email protected] $(CXXFLAGS) 

testSortedList.o: testSortedList.cpp 
    $(CPP) -c testSortedList.cpp -o [email protected] $(CXXFLAGS) 
+0

testmake:SortedList.cpp fileListFunctions.cpp testSortedList.cpp \t g ++ -o testmake SortedList.cpp fileListFunctions.cpp testSortedList.cpp -I。 –

+0

如果你的Git拉我链接库,我刚更新的代码就是我目前正与 ^上面贴提出的是编辑(我改变GCC与G ++) –

+0

@EricWalsh你应该了解更多的Makefile为了理解它。无论如何发布的makefile将适用于您的项目。祝你好运 – Itachi