2

我正在尝试使用带有多个文件的点云库和 OpenCV 来实现一个测试项目。当我尝试编译时,我收到“已定义错误”消息。可能我正在做一些由于某种原因无法实现的愚蠢行为 - 我尝试了一些在这里找到的解决方案,但它们似乎都对我没有帮助。

我有的:

一个 libs.h 文件,我在其中加载 lib 文件(在项目属性中,我只设置 .lib 路径并“手动”加载 lib,如标题):

#pragma once

#ifndef PCLTEST_LIBS
#define PCLTEST_LIBS

#ifdef _DEBUG
  #pragma comment(lib, "pcl_apps-gd.lib")
  #pragma comment(lib, "pcl_common-gd.lib")
  // a bunch of other debug libs
#else
  // the release libs
#endif
#endif

一个主文件,此时我基本上删除了所有内容以进行调试:

// load the libs
#ifndef PCLTEST_LIBS
#include "libs.h"
#endif

// pcltest includes
// if only this first one is #included, everything is OK
#include "opencvOperations.h"
// #including this one causes the error
#include "files.h"
// these ones are not working also
//#include "cloudOperations.h"
//#include "visualize.h"

// c++ headers
#include <stdio.h>
#include <string>
//#include <sstream>
//#include <iostream>

void writeInfo()
{
    // some std::cout calls
}

int main( int argc, char* argv[] )
{
    writeInfo();
    // this function is in opencvOperations.h and works OK
    pcltest::openLena();
}

然后我在 main.obj 中收到几条错误消息,表明 files.obj 中已经定义了一些(PCL 相关的)符号。我在 opencvOperations 和 files 中都使用 PCL 相关调用,第一个可以,第二个不起作用。

编辑: 要添加更多详细信息,我的 files.h 标头:

#pragma once

#ifndef PCLTEST_FILES
#define PCLTEST_FILES

// pcl headers
#ifndef PCL_COMMON_H_
#include <pcl/common/common_headers.h>
#endif
#ifndef PCL_IO_FILE_IO_H_
#include <pcl/io/file_io.h>
#endif
#ifndef PCL_IO_PCD_IO_H_ 
#include <pcl/io/pcd_io.h>
#endif
#ifndef PCL_IO_PLY_IO_H_ 
#include <pcl/io/ply_io.h>
#endif
// boost headers
#ifndef BOOST_FILESYSTEM_OPERATIONSX_HPP 
#include <boost/filesystem/operations.hpp>
#endif

#endif

namespace pcltest
{
    // function to open PCL or binary PLY files
    pcl::PointCloud<pcl::PointXYZ>::Ptr openCloud(std::string filename);

    // function to save the point cloud to PCD format
    void saveCloud();
}

在将代码拆分为单独的文件之前,一切正常(使用相同的项目设置)。

编辑2:

我找到了问题的根源,

#include <pcl/io/ply_io.h>

导致这个。现在,我摆脱了与 PLY 相关的所有内容,一切正常。我稍后再看,这可能是 PCL 库特定的问题。我仍然很奇怪为什么这个调用会导致另一个文件中的链接器错误,我什至不使用 PLY 相关的函数/变量。

4

2 回答 2

8

我遇到了和你一样的问题。我有一个surface.h 和surface.cpp 文件,我发现我必须包含来自surface.cpp 而不是surface.h 的ply_io.h 文件,现在它编译得很好。我希望这有帮助或有意义!哈哈

于 2012-01-13T23:12:07.623 回答
2

如果在包含中实例化一个常量,则也可以使用 selectany,根据http://msdn.microsoft.com/en-us/library/5tkz6s71%28v=vs.80%29.aspx - 在我的情况下:

const int CSdata[] = {1, 2, 3, 4};

当包含在多个源部分中时,在链接时会产生 LNK2005,通过以下方式避免:

const __declspec(selectany) int CSdata[] = {1, 2, 3, 4};

非便携式,是的...

于 2012-09-11T15:31:42.457 回答