c++ - LNK2005,MSVC2010 中的 "already defined error"链接器错误

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

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

我有什么:

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

#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 相关调用,第一个可以,第二个不行。

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

#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 相关函数/变量。

最佳答案

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

关于c++ - LNK2005,MSVC2010 中的 "already defined error"链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8343303/

相关文章:

c++ - Wt 从 http 响应回调添加新的小部件

c# - 如何同步数据库和DataGridView

c++ - 稍后在 C++ 中调用基类构造函数(不在初始化列表中)

c++ - 命名空间中的静态变量与非静态变量

C++:解析文本文件并将其读入数组

deployment - 在哪里可以找到有关 VS 2010 部署的最新信息/演练?

c# - Visual Studio 2010 Locals 窗口红色字体

静态函数可以比非静态函数更快吗?

java - 编译多个对象类时出现 ClassNotFound 错误

C# 语法 : What character is the "new line" character?