简体   繁体   中英

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. 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.

I have four files, feir.cpp , feir.h , filters.cpp and filters.h in a Visual Studio 2010 project. 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. 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. The error message is shown at the end of this post. Below are the relevant parts of code:

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

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

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. 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? 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:

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

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. In plain VS, that would mean adding them to the project's Source Files folder (and not just as solution items).

In CMake, it means listing them in the add_executable() or add_library() command. So change your CMakeList like this:

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).

Without including all relevant files somehow, the linker has no idea of knowing that it should link them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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