Answered by:
Linking issue 2019 in C++ (C++-CLI) with multiple files - header with utility functions - precompiled header - c++ classes

Question
-
Recently I asked a question on stackoverflow regarding a linking issue. Having a small to a medium sized project with precompiled headers, regardless of the type of management chosen (CLR CLI managed, native/unmanaged C++), there seems to be a problem with the following approach of including an utility functions header with an associated cpp implementation file. The structure is as follows:
A.h
#pragma once class A { bool sheet; public: A(void); ~A(void); };
A.cpp
#include "StdAfx.h" #include "A.h" #include "utils.h" A::A(void) { sheet = Dummy(); } A::~A(void) { }
utils.h
#pragma once bool Dummy();
utils.cpp
#include "stdafx.h" #include "utils.h" bool Dummy() {std::cout<< " ok " ; return true;}
If there are multiple files in the project, besides the A class and the utils collection, then the linker stops spewing out an error concerning an _unresolved external symbol_ in the A.obj object file. Although a sound approach, it won't get linked if there are multiple files concerned as it cannot find the executable code for the Dummy bool function. What should I investigate to get to the root of this error?
Wednesday, June 27, 2012 7:47 AM
Answers
-
For anyone else that might read this and seek help: do check if the "rebuild" and "clean" options work as intended (i.e. really clean the obj files in the output folders). This was my situation where a computer restart solved the problem.. the next day. It was because of the Visual Studio 2010 not being able to properly clean the projects..
Imagination is perpendicular to reality.
- Marked as answer by Teodor Cioaca Wednesday, July 4, 2012 11:24 AM
Wednesday, July 4, 2012 11:24 AM
All replies
-
If the linker can't find the Dummy() function then check to make sure utils.cpp is part of the project. It must be listed in Solution Explorer under Source Files. If it does not appear there use the File menu 'Add Existing Item' to add the file to the project.
- Proposed as answer by Rebecca Liu Friday, June 29, 2012 6:04 AM
Wednesday, June 27, 2012 12:40 PM -
If the linker can't find the Dummy() function then check to make sure utils.cpp is part of the project. It must be listed in Solution Explorer under Source Files. If it does not appear there use the File menu 'Add Existing Item' to add the file to the project.
All files were correctly added to the project. There are two projects that make use of these for files: a native/purce c++ solution and a C++ CLI clr managed solution and they both fail to build. If I start a new solution with just those 4 files, it miraculously works. The files are always part of the C++ project in discussion, in all cases. Furthermore, it's not a C++ CLI clr problem because it's failing for an unmanaged solution with the same error. I'm using VS2010 Proefssional.. although I presume this is not a problem..
Thanks!
Imagination is perpendicular to reality.
Wednesday, June 27, 2012 12:49 PM -
For anyone else that might read this and seek help: do check if the "rebuild" and "clean" options work as intended (i.e. really clean the obj files in the output folders). This was my situation where a computer restart solved the problem.. the next day. It was because of the Visual Studio 2010 not being able to properly clean the projects..
Imagination is perpendicular to reality.
- Marked as answer by Teodor Cioaca Wednesday, July 4, 2012 11:24 AM
Wednesday, July 4, 2012 11:24 AM