【发布时间】:2015-07-02 21:16:52
【问题描述】:
我正在尝试在 Visual Studio 2013 中使用 libnoise library,这是一个用于生成 perlin 噪声的 c++ 库。教程 3 链接到一个名为 noiseutils 的插件实用程序,它由一个 .cpp 和一个 .h 组成,它我放在这里:
我将项目设置指向包含文件夹(其内容如下图所示),我的代码如下所示。
#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;
}
所有这些代码都在他们网站上提供的第三个教程中给出。 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>------ 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 ==========
我查看了这个问题,发现我可以简单地禁用对noiseutils.cpp 使用预编译头文件,因为它不需要访问stdafx。这样,问题就解决了。
出于好奇,什么是 stdafx 以及它在 Visual Studio 项目中扮演什么角色?我是否需要禁用我添加到项目中的任何源代码的预编译标头,以便它忽略 stdafx?
【问题讨论】:
-
您需要将noiseutils.cpp 添加到您的项目中。
-
事后看来这似乎很明显......我会更新结果。
-
stdafx.h 是为您的项目拉入预编译头文件的头文件的默认名称(编译成 stdafx.cpp) .
#include "stdafx.h"在使用预编译头文件时需要在任何其他语句之前。因此,要么禁用项目范围内的预编译头文件,要么在解决方案资源管理器中右键单击 noiseutils.cpp,转到属性,然后在 C/C++ 设置中的某处,在 Precompiled Headers 页面上的设置允许您可以为单个源文件关闭它。
标签: c++ visual-studio-2013 noise-generator