简体   繁体   English

C ++ MSVS项目内链接器错误LNK2019 - 声明定义不匹配?

[英]C++ MSVS within-project linker error LNK2019 - Declaration definition mismatch?

I have tried to read everything I can find about linker errors that seems to be applicable to my code, but I haven't find a solution, so help wpuld be greatly appreciated. 我试图阅读有关链接器错误的所有内容,这些错误似乎适用于我的代码,但我找不到解决方案,所以非常感谢帮助wpuld。 From what I've read, the only reason for the linker error that seems to apply to my code, given that it worked fine before and it only includes files from one single MSVS project, is a declaration/definition mismatch, but don't take my word for that. 从我读过的,链接器错误的唯一原因似乎适用于我的代码,因为它之前工作正常并且它只包含来自单个MSVS项目的文件,是声明/定义不匹配,但不请相信我的话。

I have four files, feir.cpp , feir.h , filters.cpp and filters.h in a Visual Studio 2010 project. 我有四个文件, feir.cppfeir.hfilters.cppfilters.h在Visual Studio 2010中的项目。 Until the recent addition of a line of code in the main-function (in feir.cpp ) that calls the function resampler from the filters.cpp -file everything worked fine. 直到最近除了一条线在主功能(在代码的feir.cpp )调用该函数resamplerfilters.cpp -file一切正常。 This function-call is the first call to a function outside the feir.cpp -file, so I guess I'm doing something wrong in tying everything together. 这个函数调用是第一次调用feir.cpp -file之外的函数,所以我猜我把所有东西绑在一起都做错了。 The error message is shown at the end of this post. 错误消息显示在本文末尾。 Below are the relevant parts of code: 以下是代码的相关部分:

feir.cpp feir.cpp

...
#include "feir.h"
#include "itkImage.h"
#include <string>
#include "filters.h"

int main( int argc, char* argv[] )
{
...
ImageType::Pointer moving;  
ImageType::Pointer target;
std::tie(moving, std::ignore) = preRegistrationOperations( inputDir, movingDir, movingSeriesNumber, preparationsDone, verbose, selectSliceMoving ); 
std::tie(target, std::ignore) = preRegistrationOperations( inputDir, targetDir, targetSeriesNumber, preparationsDone, verbose, selectSliceTarget );
...
ImageType::Pointer resampledTarget = resampler ( target, moving );    
}

filters.h filters.h

...
ImageType::Pointer resampler( ImageType::Pointer image, ImageType::Pointer imageTemplate, std::string interpolate = "Linear");

filters.cpp filters.cpp

...
ImageType::Pointer resampler( ImageType::Pointer image, ImageType::Pointer imageTemplate, std::string interpolate )
{
...
ImageType::Pointer resampled = ImageType::New();
resampled = filter->GetOutput();    
return resampled;
}

EDIT: After @Roger Rowlands comment I added the following: I configure and generate the solution of this project with CMake, and then I build the solution in MSVS. 编辑:在@Roger Rowlands评论之后我添加了以下内容:我使用CMake配置并生成此项目的解决方案,然后我在MSVS中构建解决方案。 However, I have to admit that what the implications of all this is, goes beyond my limited programming knowledge, so I'm not sure whether this is the problem, and in that case, why. 但是,我不得不承认,这一切的含义是什么,超出了我有限的编程知识,所以我不确定这是否是问题,在这种情况下,为什么。 So should I make changes to my CMakeLists.txt-file and generate the solution again now that I have included an additional file in the project? 因此,如果我在项目中包含了一个附加文件,我是否应该更改我的CMakeLists.txt文件并再次生成解决方案? So far in my very short ITK-programming career I've only considered CMake a necessary evil and not bothered understanding what it actually does... My CMakeLists.txt-file looks like this: 到目前为止,在我非常短的ITK编程生涯中,我只考虑了CMake一个必要的邪恶,而不是理解它实际上做了什么...我的CMakeLists.txt文件看起来像这样:

CMakeLists.txt 的CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(feir)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
add_executable(feir feir.cpp)
target_link_libraries(feir ${ITK_LIBRARIES})

Hope someone can help me. 希望可以有人帮帮我。 Cheers! 干杯!

Linker error 链接器错误

Error 1 error LNK2019: unresolved external symbol "class itk::SmartPointer<class itk::Image<short,3> > __cdecl resampler(class itk::SmartPointer<class itk::Image<short,3> >,class itk::SmartPointer<class itk::Image<short,3> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?resampler@@YA?AV?$SmartPointer@V?$Image@F$02@itk@@@itk@@V12@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main C:\\Users\\310079322\\Documents\\ITK_VTK\\feir\\bin\\feir.obj feir 错误1错误LNK2019:未解析的外部符号“class itk :: SmartPointer <class itk :: Image <short,3 >> __cdecl resampler(class itk :: SmartPointer <class itk :: Image <short,3 >>>,class itk: :SmartPointer <class itk :: Image <short,3 >>,class std :: basic_string <char,struct std :: char_traits <char>,class std :: allocator <char >>))“(?resampler @@ YA? AV?$智能指针@ V'$图片@ F $ @版本02 ITK ITK @@@ @@ @ V12 0V?$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@函数_main C:\\ Users \\ 310079322 \\ Documents \\ ITK_VTK \\ feir \\ bin \\ feir.obj feir中引用的@Z)

You need to include all of an executable's (or library's) source files in its project so that Visual Studio knows to link them together. 您需要在其项目中包含所有可执行文件(或库)的源文件,以便Visual Studio知道将它们链接在一起。 In plain VS, that would mean adding them to the project's Source Files folder (and not just as solution items). 在普通的VS,那将意味着将它们添加到项目的源文件夹(而不是仅仅作为解决方案项)。

In CMake, it means listing them in the add_executable() or add_library() command. 在CMake中,它意味着在add_executable()add_library()命令中列出它们。 So change your CMakeList like this: 所以改变你的CMakeList如下:

add_executable(feir feir.cpp filters.cpp)

(Note: you might want to put header files in there as well. It's not necessary for building or linking, but they'll be shown in the correct filter in VS's solution explorer). (注意:您可能也想将头文件放在那里。它不需要构建或链接,但它们将显示在VS的解决方案资源管理器中的正确过滤器中)。

Without including all relevant files somehow, the linker has no idea of knowing that it should link them. 如果不以某种方式包含所有相关文件,链接器就不知道它应该链接它们。

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

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