0

私の問題はこれに似ていますが、日食で修正する方法がわかりません。

eclipse で小さなプログラムをコンパイルすると、奇妙な動作が発生します。ヘッダーの最後に .cpp ファイルを含める (および .cpp の .h のインクルードを削除する) と、コンパイルされますが、そうでない場合はコンパイルされません。

含めようとしているクラスは別のプロジェクトにあり、そのプロジェクトは適切にリンクされています。

次に例を示します。

プロジェクト ソース内

ファイル 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を使用する必要があります。

助言がありますか?

ありがとう!

4

2 に答える 2

1

インクルード ファイルの最後に「#endif」がありません。

代わりに「#pragma once」を使用してください。

// .h file
#pragma once

namespace lol
{
    class foo {};
}

// end of file

ここでコンパイルユニットとパイプラインの説明を参照してください。

于 2013-10-08T19:12:46.047 に答える