简体   繁体   English

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

[英]LNK2005, “already defined error” linker error in MSVC2010

I am trying to implement a test project using the Point Cloud Library and OpenCV with multiple files. 我正在尝试使用带有多个文件的Point Cloud Library和OpenCV来实现测试项目。 When I try to compile, I get the "already defined error" message. 当我尝试编译时,我收到“已定义的错误”消息。 Probably I'm doing something stupid that cannot realize for some reason - I tried out a couple of solutions found here, none of them seemed to be help in my case. 可能我做了一些愚蠢的事情,由于某些原因无法实现 - 我尝试了一些在这里找到的解决方案,在我的案例中似乎没有任何帮助。

What I have: 我有的:

A libs.h file, where I load the lib files (in Project properties, I only set up the .lib paths and load the libs "by hand", like the headers): 一个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

A main file from which I basically deleted everything at this point to debug: 一个主文件,我基本上删除了此时调试的所有内容:

// 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();
}

Then I get several error messages in my main.obj that some (PCL related) symbols are already defined in files.obj. 然后我在main.obj中收到一些错误消息,其中一些(PCL相关的)符号已在files.obj中定义。 I use PCL related calls both in opencvOperations and files, the first one is OK, the second one does not work. 我在opencvOperations和文件中使用PCL相关的调用,第一个是OK,第二个不起作用。

Edit: To add more detail, my files.h header: 编辑:要添加更多细节,我的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();
}

Before splitting the code into separate files, everything worked well (with the same project settings). 在将代码拆分为单独的文件之前,一切都运行良好(使用相同的项目设置)。

Edit2: EDIT2:

I located the source of the problem, 我找到了问题的根源,

#include <pcl/io/ply_io.h>

causes this. 导致这个。 For now, I got rid of everything related to PLY and everything works fine. 现在,我摆脱了与PLY相关的一切,一切正常。 I'll look at it later, this might be a PCL library specific issue. 我稍后会看,这可能是PCL库特定的问题。 Still strange to me why this call causes linker error in an other file, where I don't even use PLY related functions/variables. 我仍然很奇怪为什么这个调用导致另一个文件中的链接器错误,我甚至不使用PLY相关的函数/变量。

I had the same problem as you had. 我遇到了和你一样的问题。 I had a surface.h and surface.cpp file, and I found out that I had to include the ply_io.h file from surface.cpp rather than surface.h and now it compiles fine. 我有一个surface.h和surface.cpp文件,我发现我必须包括surface.cpp而不是surface.h的ply_io.h文件,现在它编译得很好。 I hope that helps or makes sense! 我希望这有帮助或有意义! haha 哈哈

If a constant is being instantiated in an include one can also use selectany, per http://msdn.microsoft.com/en-us/library/5tkz6s71%28v=vs.80%29.aspx -- in my case: 如果在include中实例化一个常量,也可以使用selectany,根据我的情况: http//msdn.microsoft.com/en-us/library/5tkz6s71%28v=vs.80%29.aspx

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

when included in more than one source part produces LNK2005 at link time, avoided via: 当包含在多个源部件中时,在链接时生成LNK2005,通过以下方式避免:

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

Non-portable, yeah ... 非便携式,是的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM