Linking failure within solution - c++

EDIT: I know there are similar questions, but I cannot find an answer to a following issue: Why the methods inside the class are working correctly and outside are not.
I've got a weird problem in my project which I'm developing in MSVC++ 2012. My project consists of different modules of code. The important modules from the problem's point of view is a library and the GUI. They exist as different projects in the same solution.
I have some methods in the library which are part of the classes (in this case Calibration3D):
void Calibration3D::load(const std::string &path)
I use it without problems when I need it in the GUI, however I need to use a following method (outside the class):
void xxxyyy()
But when I'm trying to use that function (outside the class but in the same namespace) I get a following error:
1>project_xml.obj : error LNK2001: unresolved external symbol "void __cdecl cci::xxxyyy(void)" (?xxxyyy#cci##YAXXZ) 1>D:\praca_pw\cci\build-msvc2012\x64\Release\\ccigui.exe : fatal error LNK1120: 1 unresolved externals
Anybody knows how to solve it?

When I have a header file like this:
namespace xyz {
void foo();
class bar { ... };
}
then I write the cpp file like this:
#include "xyz.h"
namespace xyz {
void foo() { ... }
bar::bar() { ... }
}
This means I have to type a lot less and make fewer mistakes with regard to namespaces.

OK, solved, it seems that when a method is defined inside the namespace in header file, it should also be defined explicitly as part of namespace in implementation file, in this case:
cci::xxxyyy()
{
...
}
will work and
xxxyyy()
{
...
}
will not.

Related

Link error LNK2019 unresolved external symbol using private constructors and private friend function

I am new to C++ but not to programming. I'm developing a plugin and learning the language at the same time. The plugin is for old software but still being used, so I'm using VS2015 and an SDK to match. I'm having a problem that I just don't know enough to solve but I know that it's the result of something that I'm doing wrong or don't understand. Please also consider that I'm using a third party SDK, with only .H/.HPP files and an occasional .CPP, but that's it. Everything else is wrapped in their libraries. Therefore, I don't have the liberty to change any behavior.
My code snippets are parts of their headers (can't change) and the .cpp is my modified sample code that comes along with their SDK and which I'm using as my base. It is also the area of code that causes the link error. Their samples all work, I can compile them and run them no problem. My code also works and is doing what I want. Things only break when I use my modified code. The reason I'm doing this is because I need access to the message passed into the plugin and can't find any other way to get it other than to try and override "PluginMain". The original sample code actually does call into PluginSetup.cpp because it runs other code within it as setup prior to continuing on. I've only posted the part of my code which is my attempt to override the function as I mentioned and I just included the variable declaration that causes the error. If I comment my variable declaration and other code related to it, program compiles and works again. If I move the variable declaration to another .cpp file in my codebase, code compiles no problem. It just don't like being in PluginSetup.cpp but part from maybe the main.cpp file (which I can't do anything with), PluginSetup.cpp is the first that gets called. So this is where I chose to put my override.
Am I using the friend function correctly? As you can see from the codebase, they've made the ctor as well as the friend function private.
This may also go hand in hand with a question I asked before on how to instantiate a class from this implementation using private friend function and ctors?
Hopefully, what I've posted is enough to give someone all that's needed to figure out what the problem might be.
ns1ns2Main.h
namespace ns1
{
namespace ns2
{
class Plugin;
...
}
}
extern "C" __declspec(dllexport) __MainError PluginMain(const char* caller_, const char* selector_, void* message_);
ns1ns2Plugin.h
#include "ns1ns2Main.h"
namespace ns1
{
namespace ns2
{
class Plugin
{
Public:
static Plugin* const instance();
private:
friend __MainError (::PluginMain) (const char*, const char*, void*);
Plugin();
Plugin(const Plugin&);
virtual ~Plugin();
};
}
}
PluginSetup.cpp
#include "ns1ns2Main.h"
#include "ns1ns2Plugin.h"
//-> My Modification Begins
__MainError (::PluginMain) (const char* caller, const char* selector, void* message)
{
ns1::ns2::Plugin plugin;
if (!plugin.instance())
{
plugin = ns1::ns2::Plugin();
}
if (strcmp(caller, kSPInterfaceCaller) == 0)
{
if (strcmp(selector, kSPInterfaceStartupSelector) == 0)
{
bool bStatus = ns1::ns2::pluginSetup(&plugin);
if (bStatus)
{
plugin_ = clpcsx::Plugin::instance();
plugin_->startup();
}
}
else if (strcmp(selector, kSPInterfaceShutdownSelector) == 0)
{
plugin_ = clpcsx::Plugin::instance();
plugin_->shutdown();
}
}
return error;
}
//<- My Modification Ends
namespace ns1
{
namespace ns2
{
void pluginLoaded()
{
// no-op
}
bool pluginSetup(Plugin* const plugin)
{
clpcsx::Plugin::create(plugin);
plugin->setStartupCallback(NS1_NS2_CALLBACK(clpcsx::Plugin, CLPCSX_PLUG_INST, startup));
plugin->setPostStartupCallback(NS1_NS2_CALLBACK(clpcsx::Plugin, CLPCSX_PLUG_INST, postStartup));
plugin->setPreShutdownCallback(NS1_NS2_CALLBACK(clpcsx::Plugin, CLPCSX_PLUG_INST, preShutdown));
plugin->setShutdownCallback(NS1_NS2_CALLBACK(clpcsx::Plugin, CLPCSX_PLUG_INST, shutdown));
return true;
}
void pluginDestroy(Plugin* const plugin)
{
clpcsx::Plugin::destroy();
}
}
}
Link Error
1>PluginSetup.obj : error LNK2019: unresolved external symbol "private: __cdecl ns1::ns2::Plugin::Plugin(void)" (??0Plugin#ns2#ns1##AEAA#XZ) referenced in function PluginMain
You have to tell the linker to include the libraries. Since this is VS you can add to the main .cpp file
#pragma comment(lib, "xxxx.lib")
where 'xxxx.lib' is the name of the library that has those ns functions. You need to make sure they are in the VS linker path too

LNK2019 Error with nested class in C++

I am new to C++, so I am sorry if this a simple or obvious error. I have been reading a lot other questions and the documentation, but I haven't been able to find a solution yet.
I am trying to add a new class definition to a large existing project. Everything compiles perfectly fine without my additions. However, when I add my code below I get a LNK2019 error on the constructor method (and any additional method). I have noticed adding/referencing properties does not cause this linker error, only the methods. Below is the most simple example that produces the error:
Header:
namespace foo
{
class bar_interface
{
public:
//My code
#ifndef Point_H
#define Point_H
class Point
{
public:
Point(int x, int y);
};
#endif
//existing code
void onStartup();
}
}
Class:
//My code
#ifndef Point_H
#define Point_H
class foo:bar_interface::Point{
public:
Point(int x, int y)
{
};
};
#endif
//existing code
void bar_interface::onStartup()
{
foo::bar_interface::Point p( (int)8, (int)8 );
//continue existing code
}
Error 1 error LNK2019: unresolved external symbol "public: __thiscall
foo::bar_interface::Point::Point(int,int)"
(??0Point#bar_interface#foo##QAE#HH#Z)
referenced in function "public: void __thiscall
foo::bar_interface::onStartup(void)"
(?onStartup#bar_interface#foo##QAEXXZ)
I realize that probably do not need such explicit calls to Point or casting the numbers as ints, but I wanted to make sure I wasn't missing anything obvious (removing them doesnt change the error). I have tried moving the 'Point' class to its own file and defining it outside the 'bar_interface' but within the 'foo' namespace. Removing the #ifndef code creates a C2011 redefinition error. I am at a loss for how to proceed.
Unresolved external means that the definition is missing, that is that the linker cannot find an implementation of the named function.
Somewhere you need:
namespace foo
{
bar_interface::Point::Point(int,int)
{ ... }
}
Remove all your lines from the code above that start from # and the reason of the issue becomes cleaner.

Unresolved External Symbol C++

I have read this informative stackoverflow question regarding unresolved external symbols, but I am still not sure how to solve my issue.
Within Visual Studio 2012, I have a solution consisting of multiple projects, one of which is a static library called common. Each project that produces an executable consists of a header and associated cpp file of all global functions used throughout that specific program, called programglobals. In the process of developing one of these projects, I started duplicating some of this code from one project's programglobals to another. Now that I have completed all the projects, I want to extract the duplicate code into a associated header and cpp file within the common library, but I believe I might be referencing them incorrectly, which is producing these unresolved external symbol errors
Here is a dumbed down example of what I am currently attempting.
Common Library Files
//solutionglobals.h
void commonFunction();
//solutionglobals.cpp
void commonFunction() {
int asdf;
}
Project A Files
// programglobals.h
#include "../common/solutionglobals.h
void functionUsedInProjectA();
// programglobals.cpp
void functionUsedInProjectA() {
int x;
}
// output.h
#include "programglobals.h"
void asdfA();
// output.cpp
void asdfA() {
int x;
functionUsedInProjectA();
commonFunction();
}
Project B Files
// programglobals.h
#include "../common/solutionglobals.h
void functionUsedInProjectB();
// programglobals.cpp
void functionUsedInProjectB() {
int x;
}
// output.h
#include "programglobals.h"
void asdfB();
// output.cpp
void asdfB() {
int x;
functionUsedInProjectB();
commonFunction();
}
Any reference to commonFunction() results in an unresolved external symbol error.
Thanks!
You will have to specify in your executable projects that they reference the static lib. May be http://msdn.microsoft.com/en-us/library/vstudio/ms235627%28v=vs.110%29.aspx#uselibinapp helps (lower third of the article).
Before you can use the math routines in the static library, you must
reference it. To do this, open the shortcut menu for the MyExecRefsLib
project in Solution Explorer, and then choose References. In the
MyExecRefsLib Property Pages dialog box, expand the Common Properties
node, select Framework and References, and then choose the Add New
Reference button.
The linker cannot 'see' your function and as such thinks that it does not have an external symbol referencing it, hence the error.
You must #pragma comment(lib, [library here]) to reference the external function.
The following code can be used to reproduce this error:
[header file- test.h]:
#include "StdAfx.h"
void someOtherFunction();
void someFunction(string thisVar);
[code file- test.cpp]:
#include "StdAfx.h"
#include "test.h"
void someOtherFunction()
{
printf("Hello World!");
}
[function body for someFunction(string thisVar) is missing!]

Linker errors when calling C++/CLI code from pure C++ code

I am using VS2012 and I am trying to call CLI code from C++. So I created two projects. One is executable which is pure C++ (without CLI support) and second is dynamic library which is CLI (with /clr switch). If I have main (in executable):
// main.cpp file
#include "..\CLILibrary\CCli.h"
int main()
{
Ccli test = Ccli();
test.Write();
return 0;
}
And one class in CLI library (build with CLR switch on):
// Ccli.h file
#pragma once
class Ccli
{
public:
void Write();
void CallRealCLIClass();
};
// Ccli.cpp file
#include " Ccli.h"
void Ccli::Write()
{
System::Console::WriteLine("In Ccli class.");
}
void Ccli::CallRealCLIClass()
{
// here I would like to call RealCLI class
}
Everything works fine so far. I understand, that header file (Ccli.h) cannot use anything from CLI since it has to be readable for my executable which is purely in C++ (theoretically it could if I would use something like #ifdef _MANAGED but thatÆs not my point). But in source file (Ccli.cpp) it is fine.
But now I want to use class which will be fully CLI. And I want to call it from Ccli.cpp file. So I created following class in my CLI library:
// RealCLI.h file
#pragma once
ref class RealCLI
{
public:
RealCLI(void);
System::String^ GetString();
void Write(System::String^ s);
};
// RealCLI.cpp file
#include "RealCLI.h"
RealCLI::RealCLI(void){}
System::String^ GetString()
{
System::String^ s = gcnew System::String("GetString in RealCLI class");
return s;
}
void Write(System::String^ s)
{
System::Console::WriteLine(s);
}
Now I have following problem and I don't know why. I get this error from linker:
Error 1 error LNK2020: unresolved token (06000002) RealCLI::GetString ...\RealCLI\RealCLI.obj
Error 2 error LNK2020: unresolved token (06000003) RealCLI::Write ...\RealCLI\RealCLI.obj
Error 3 error LNK1120: 2 unresolved externals ...\Debug\RealCLI.dll 1
So my library is fine (it is built without problem) but my executable have these linker errors. I don't understand why? I don't use this file in my executable project, so why is my executable even care about it? I find a way how to fix it. But since I don't know the reason why is the original program not working I consider it just as workaround. My workaround is delete RealCLI.cpp file and put everything in header file:
// RealCLI.h file
#pragma once
ref class RealCLI
{
public:
RealCLI(void) {}
// I cannot even put definition outside declaration of my class
System::String^ GetString()
{
System::String^ s = gcnew System::String("GetString in RealCLI class");
return s;
}
void Write(System::String^ s)
{
System::Console::WriteLine(s);
}
};
Why is that? What am I doing wrong? Is some of my assumptions wrong?
EDIT:
// Ccli.cpp file
#include " Ccli.h"
// !!!added this line:
#include "RealCLI.h"
void Ccli::Write()
{
System::Console::WriteLine("In Ccli class.");
}
void Ccli::CallRealCLIClass()
{
// here I would like to call RealCLI class
}
I repaired namespaces in RealCli.cpp which helped. But when I added #include "RealCLI.h" I get these error anyway:
Error 2 error LNK2020: unresolved token (06000001) RealCLI::.ctor D:\ftp\my\vyuka-cppToCLI-test\vyuka-ManagedUmanaged\UnmanagedToManagedSource.obj
Error 3 error LNK2020: unresolved token (06000002) RealCLI::GetString D:\ftp\my\vyuka-cppToCLI-test\vyuka-ManagedUmanaged\UnmanagedToManagedSource.obj
Error 4 error LNK2020: unresolved token (06000003) RealCLI::Write D:\ftp\my\vyuka-cppToCLI-test\vyuka-ManagedUmanaged\UnmanagedToManagedSource.obj
Error 5 error LNK1120: 3 unresolved externals D:\ftp\my\vyuka-cppToCLI-test\Debug\vyuka-ManagedUmanaged.exe 1
You are making basic C++ mistake. In RealCLI.cpp:
Instead of:
System::String^ GetString() { ... }
use:
System::String^ RealCLI::GetString() { ... }
Similarly for Write()
Actually I didn't set linker right. Code is (after edit) Ok. I'm linking to .obj files, because I get errors when linking directly to .dll (because it is not pure C++ but CLI). And I linked just Ccli.obj then few weeks later I add another file and forgot link RealCLI.obj...

Unresolved external symbol, cannot figure out why

I have two files that are causing me a lot of grief: camAVTEx.h and camAVTEx.cpp. Here is the general setup for the two files:
//.h////////////////////////////////////////////////
/*
#includes to some other files
*/
class camera_avtcam_ex_t : public camera_t
{
public:
camera_avtcam_ex_t();
virtual ~camera_avtcam_ex_t();
private:
//some members
public:
//some methods
};
void GlobalShutdownVimbaSystem();
//.cpp/////////////////////////////////////////////
#include "StdAfx.h"
#include "camAVTEx.h"
//some other #includes
camera_avtcam_ex_t::camera_avtcam_ex_t()
{
}
//rest of the class' functions
void GlobalShutdownVimbaSystem()
{
//implememtation
}
Then, in a file in a different directory, I do a #include to the exact location of the .h file and try to use the class:
//otherfile.cpp
#include "..\..\src\HardSupport\Camera.h"
//this is the base camera class (camera_t)
#include "..\..\src\HardControl\camAVTEx.h"
//this is indeed where both the .h and .cpp files are located
void InitCam
{
camera_t* maincam = new camera_avtcam_ex_t();
}
void OnExit()
{
GlobalShutdownVimbaSystem();
}
When I compile, I get the following errors:
8>otherfile.obj : error LNK2001: unresolved external symbol "public: __cdecl
camera_avtcam_ex_t::camera_avtcam_ex_t(void)" (??0camera_avtcam_ex_t##QEAA#XZ)
8>otherfile.obj : error LNK2001: unresolved external symbol "void __cdecl
GlobalShutdownVimbaSystem(void)" (?GlobalShutdownVimbaSystem##YAXXZ)
8>....\bin\x64\Release\otherfile.exe : fatal error LNK1120: 2 unresolved externals
I cannot for the life of me figure out why it can't find the implementations for these two functions.
So I guess my question is fairly obvious: Why am I getting these errors and what do I need to change to fix them?
Whatever how you look at it, the error you have : unresolved external symbol "public: __cdecl camera_avtcam_ex_t::camera_avtcam_ex_t(void)" (??0camera_avtcam_ex_t##QEAA#XZ) means that the compiler knows the symbol camera_avtcam_ex_t::camera_avtcam_ex_ (that's the class constructor) since he saw its declaration in the camAVTEx.h file but halas, it can't find (= resolve) the implementation of this symbol (in short, the code).
This usually happen because of several possible causes :
you didn't tell the compiler about the code (.cpp) you try to use so he doesn't know it. Try to add the file to your project.
you compile the missing code, but don't link with it. Check if you don't have two separated projects or try to add the lib to your project if it comes from a lib.
in some way, the compiled code does not match its definition (happens when mixing C and C++ or messing with namespaces) Check if you are not declaring contradicting enclosing namespaces.
(maybe other reasons I don't know ?)