我的問題類似於this one但我無法確定如何在eclipse中修復它。GCC無法自動從.h類解析.cpp包括
在eclipse上編譯一個小程序時,我有一個奇怪的行爲。當我將.cpp文件包含在頭部的末尾(並且將.h包含在.cpp中)時,它會進行編譯,否則就不會。
我試圖包含的類是在一個單獨的項目中,並且該項目已正確鏈接。
下面是一個例子:
在項目源
文件myclass.h
#ifndef MYCLASS_H_
#define MYCLASS_H_
namespace lol
{
class myclass{ public // definitions... }
}
//#include myclass.cpp //**works when I uncomment this**
#endif
文件myclass.cpp
#include "myclass.h" // ** does not work unless I include my .cpp (unity build like) **
// and I don't want to include .cpp files
namespace lol{ // not funny
myclass::myclass(){
} //code ... code
}
在其他項目 mainFile.cpp
#include "myclass.h" // works only if I include myclass.cpp at the end of myclass.h
using namespace lol;
int main(){
myclass obj = myclass(); // gives undefined reference to lol::myclass::myclass()
}
我可以通過建立一切從一個makefile這是我喜歡的一個解決方案解決這個問題,但我需要使用Eclipse,可悲。
有什麼建議嗎?
謝謝!
請閱讀gcc介紹和翻譯單元是什麼以及什麼是鏈接和它的全部功能。並且從不包含.cpp文件。 – PlasmaHH
沒有任何與您引用的問題類似。你應該真的按照@ PlasmaHH的建議! –
關於從不包含.cpp,我確實想避免它。我知道什麼是鏈接,我可以使用makefile,這是關於在eclipse中做的。你應該閱讀這個http://stackoverflow.com/questions/543697/include-all-cpp-files-into-a-single-compilation-unit – Johnride