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

C++ LNK2005,”;“已定义错误”;MSVC2010中的链接器错误,c++,visual-studio-2010,compiler-construction,point-clouds,C++,Visual Studio 2010,Compiler Construction,Point Clouds,我正在尝试使用点云库和带有多个文件的OpenCV实现一个测试项目。当我尝试编译时,我得到“已定义错误”消息。也许我正在做一些出于某种原因而无法实现的愚蠢的事情——我尝试了这里找到的几种解决方案,其中没有一种似乎对我的情况有帮助 我所拥有的: 一个libs.h文件,我在其中加载lib文件(在项目属性中,我仅设置.lib路径并“手动”加载libs,如标题): 一个主文件,此时我基本上删除了所有要调试的内容: // load the libs #ifndef PCLTEST_LIBS #include

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

我所拥有的:

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

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

// 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();
}
//加载libs
#ifndef PCLTEST_库
#包括“libs.h”
#恩迪夫
//pcltest包括
//如果只包括第一个,一切都好
#包括“opencvOperations.h”
//#包括此项会导致错误
#包括“files.h”
//这些也不起作用
//#包括“cloudOperations.h”
//#包括“visualize.h”
//C++标题
#包括
#包括
//#包括
//#包括
void writeInfo()
{
//一些std::cout调用
}
int main(int argc,char*argv[])
{
writeInfo();
//这个函数在opencvOperations.h中,工作正常
pcltest::openLena();
}
然后,我在main.obj中得到了一些错误消息,其中一些(与PCL相关的)符号已经在files.obj中定义。我在opencvOperations和文件中都使用PCL相关调用,第一个可以,第二个不起作用

编辑: 要添加更多详细信息,请选择my 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();
}
#pragma一次
#ifndef PCLTEST\u文件
#定义PCLTEST_文件
//pcl头
#ifndef PCL\u公共\u H_
#包括
#恩迪夫
#ifndef PCL\u IO\u文件_
#包括
#恩迪夫
#如果没有PCL_IO_PCD_IO_H_
#包括
#恩迪夫
#如果没有PCL\u IO\u PLY\u IO\u H\u
#包括
#恩迪夫
//增压头
#ifndef增压\u文件系统\u操作X\u水电站
#包括
#恩迪夫
#恩迪夫
名称空间pcltest
{
//函数打开PCL或二进制PLY文件
pcl::PointCloud::Ptr openCloud(std::string文件名);
//用于将点云保存为PCD格式的函数
void saveCloud();
}
在将代码拆分为单独的文件之前,一切都运行良好(使用相同的项目设置)

Edit2:

我找到了问题的根源

#include <pcl/io/ply_io.h>
#包括

原因是这个。现在,我去掉了所有与PLY相关的东西,一切都很好。我稍后会看,这可能是PCL库的特定问题。我仍然很奇怪为什么这个调用会在另一个文件中导致链接器错误,我甚至没有使用与PLY相关的函数/变量。

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

如果在include中实例化一个常量,那么也可以使用selectany,per--在我的例子中:

const int CSdata[] = {1, 2, 3, 4};
当包含在多个源部件中时,在链路时间产生LNK2005,通过以下方式避免:

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

不可移植,是的…

它说的是什么变量已经定义,该变量出现在哪里?对于文件.h,目前列出了4个符号:“enum pcl::io::ply::Format u cdecl pcl::io::ply::getEndianess(void)”“int u cdecl pcl::io::ply::getTypeFromTypeName(class std::basic_string const&)”“unsigned uuu int64 uuu cdecl pcl::io::ply::getMaximumCapacity(int)”“void uuu cdecl pcl::io::ply::write(struct pcl::io::ply::camera const&,class std::basic_ostream&,bool)”(我实际上没有调用任何一个)h文件是包含整个函数定义,还是仅包含原型?仅包含原型,我将其复制到原始文件中,其中包含一些实际的代码定义(与声明相反)在那些pcl头中进行操作?特别是,pcl::io::ply::getTypeFromTypeName和pcl::io::ply::write在哪里定义?好了,可以了!谢谢!:)以前没有看到您的答案,但可能非常有用later@Harry,这可能会奏效,但为什么呢?如果surface.h有一个“#pragma once”或“#ifndef#define#endinf”“这不应该是个问题。
const __declspec(selectany) int CSdata[] = {1, 2, 3, 4};