這是在我的系統目錄中的內容:如何在子目錄中使用.cpp和.h文件?
/directory/
classes/
class1.cpp
class1.h
main.cpp
這裏是上述文件的內容:
的main.cpp
#include "classes/class1.h"
int main(){
class1 test;
return 0;
}
class1.h
class class1 {
public:
class1();
void test();
};
class1.cpp
#include "class1.h"
class1::class1(){
}
class1::test(){
}
問題是爲什麼我無法編譯main.cpp
?
[email protected]:~/Desktop/directory$ g++ *.cpp
/tmp/ccEvIogL.o: In function `main':
main.cpp:(.text+0x1f): undefined reference to `class1::class1()'
collect2: error: ld returned 1 exit status
是不是反正有動力G ++搜索所有的'cpp'文件的子目錄? – Abraham
@Abraham不是GCC編號。如果你使用Makefile,那麼它是可能的,但我仍然建議你明確列出源文件。想想未來,你可能在根目錄中有多個項目,每個項目都是一個單獨的程序。只是盲目地獲取所有子目錄中的所有源文件將導致問題。 –
@Abraham使用SCons或CMake等構建系統。 –