Visual Studio 2013 でパーリン ノイズ生成用の C++ ライブラリであるlibnoise ライブラリを使用しようとしています。チュートリアル 3 は、.cpp と .h で構成される、noiseutils というアドオン ユーティリティにリンクしています。
プロジェクト設定はインクルード フォルダー (内容は下の画像にあります) を指しています。コードは次のようになります。
#include "stdafx.h"
#include <noise/noise.h>
#include "noiseutils.h"
int _tmain(int argc, _TCHAR* argv[])
{
noise::module::Perlin myModule;
utils::NoiseMap heightMap;
utils::NoiseMapBuilderPlane heightMapBuilder;
heightMapBuilder.SetSourceModule(myModule);
heightMapBuilder.SetDestNoiseMap(heightMap);
heightMapBuilder.SetDestSize(256, 256);
heightMapBuilder.SetBounds(2.0, 6.0, 1.0, 5.0);
heightMapBuilder.Build();
utils::RendererImage renderer;
utils::Image image;
renderer.SetSourceNoiseMap(heightMap);
renderer.SetDestImage(image);
renderer.Render();
utils::WriterBMP writer;
writer.SetSourceImage(image);
writer.SetDestFilename("tutorial.bmp");
writer.WriteDestFile();
return 0;
}
そのコードはすべて、Web サイトで提供されている 3 番目のチュートリアルに記載されています。Visual Studio には、このすべてのコードで指定されたクラスまたは関数が見つからないという苦情はありません。しかし、プロジェクトをビルドしようとすると...
1>------ Build started: Project: libnoisetest, Configuration: Debug Win32 -- ----
1> libnoisetest.cpp
1>b:\visual studio\extra libraries\libnoise\include\noiseutils.h(1217): warning C4244: 'initializing' : conversion from 'const double' to 'float', possible loss of data
1>libnoisetest.obj : error LNK2019: unresolved external symbol "public: __thiscall noise::utils::GradientColor::~GradientColor(void)" (??1GradientColor@utils@noise@@QAE@XZ) referenced in function "public: __thiscall noise::utils::RendererImage::~RendererImage(void)" (??1RendererImage@utils@noise@@QAE@XZ)
1>libnoisetest.obj : error LNK2019: unresolved external symbol "public: __thiscall noise::utils::NoiseMap::NoiseMap(void)" (??0NoiseMap@utils@noise@@QAE@XZ) referenced in function _wmain
1>libnoisetest.obj : error LNK2019: unresolved external symbol "public: __thiscall noise::utils::NoiseMap::~NoiseMap(void)" (??1NoiseMap@utils@noise@@QAE@XZ) referenced in function _wmain
1>libnoisetest.obj : error LNK2019: unresolved external symbol "public: __thiscall noise::utils::Image::Image(void)" (??0Image@utils@noise@@QAE@XZ) referenced in function _wmain
1>libnoisetest.obj : error LNK2019: unresolved external symbol "public: __thiscall noise::utils::Image::~Image(void)" (??1Image@utils@noise@@QAE@XZ) referenced in function _wmain
1>libnoisetest.obj : error LNK2019: unresolved external symbol "public: void __thiscall noise::utils::WriterBMP::WriteDestFile(void)" (?WriteDestFile@WriterBMP@utils@noise@@QAEXXZ) referenced in function _wmain
1>libnoisetest.obj : error LNK2019: unresolved external symbol "public: __thiscall noise::utils::NoiseMapBuilderPlane::NoiseMapBuilderPlane(void)" (??0NoiseMapBuilderPlane@utils@noise@@QAE@XZ) referenced in function _wmain
1>libnoisetest.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall noise::utils::NoiseMapBuilderPlane::Build(void)" (?Build@NoiseMapBuilderPlane@utils@noise@@UAEXXZ) referenced in function _wmain
1>libnoisetest.obj : error LNK2019: unresolved external symbol "public: __thiscall noise::utils::RendererImage::RendererImage(void)" (??0RendererImage@utils@noise@@QAE@XZ) referenced in function _wmain
1>libnoisetest.obj : error LNK2019: unresolved external symbol "public: void __thiscall noise::utils::RendererImage::Render(void)" (?Render@RendererImage@utils@noise@@QAEXXZ) referenced in function _wmain
1>B:\Visual Studio\_Projects\libnoisetest\Debug\libnoisetest.exe : fatal error LNK1120: 10 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
要するに、Visual Studio は、実際にビルドしている間、「noiseutils」内のものを見つけることができません。ここで何が間違っていますか?
アップデート:
Praetorian の提案に従って、noiseutils.cpp をソース フォルダーにコピーした後、次の一連の警告と 1 つのエラーが表示されます。
1>------ Build started: Project: libnoisetest, Configuration: Debug Win32 ------
1> noiseutils.cpp
1>b:\visual studio\extra libraries\libnoise\include\noiseutils.cpp(24): warning C4627: '#include <fstream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>b:\visual studio\extra libraries\libnoise\include\noiseutils.cpp(26): warning C4627: '#include <noise/interp.h>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>b:\visual studio\extra libraries\libnoise\include\noiseutils.cpp(27): warning C4627: '#include <noise/mathconsts.h>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>b:\visual studio\extra libraries\libnoise\include\noiseutils.cpp(29): warning C4627: '#include "noiseutils.h"': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>b:\visual studio\extra libraries\libnoise\include\noiseutils.cpp(1303): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
この問題を調べたところ、stdafx にアクセスする必要がないため、noiseutils.cpp のプリコンパイル済みヘッダーの使用を無効にできることがわかりました。それで、問題は解決しました。
好奇心から、stdafx とは何ですか? Visual Studio プロジェクトでどのような役割を果たしますか? stdafx を無視するために、プロジェクトに追加するソース コードのプリコンパイル済みヘッダーを無効にする必要がありますか?