LNK2005,MSVC2010中的“已定义错误”链接器错误

时间:2011-12-01 14:54:59

标签: c++ visual-studio-2010 compiler-construction point-clouds

我正在尝试使用带有多个文件的Point Cloud Library和OpenCV来实现测试项目。当我尝试编译时,我收到“已定义的错误”消息。可能我做了一些愚蠢的事情,由于某种原因无法实现 - 我尝试了一些在这里找到的解决方案,在我的案例中似乎没有任何帮助。

我有什么:

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

#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中收到一些错误消息,其中一些(PCL相关的)符号已在files.obj中定义。我在opencvOperations和文件中使用PCL相关的调用,第一个是OK,第二个不起作用。

修改 要添加更多细节,我的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();
}

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

EDIT2:

我找到了问题的根源,

#include <pcl/io/ply_io.h>

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

2 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分: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};

非便携式,是的......