I'm currently struggeling with a compilerproblem. The problem is, that i use one of the MoSync example apps called "European Countries" (written in c++) to write my own. But when i compile the modified code, it gives me following error in response:
Controller.cpp:24: Error: Unresolved symbol '__ZTVN13Flightmanager6FlightE',
I already had a look at the example several times and i already copied the code from the example to mine, but it doesn't solve any problems.
In paticutlar i might understand what the error means (i do have c experience), but i've never seen such structured error. I also looked at namespacing conventions but there shouldn't be any problems.
//Flight.h
namespace Flightmanager
{
class Flight
{
public:
static int flightCounter;
/**
* The constructor creates the user interface.
*/
Flight(char *flightnumber, char *gate, char *departure, char *additionalinfo, char *destinationairport, char *destinationairportshort) {
this->_id = flightCounter;
flightCounter ++;
this->_flightnumber = flightnumber;
this->_gate = gate;
this->_departure = departure;
this->_additionalinfo = additionalinfo;
this->_destinationairport = destinationairport;
this->_destinationairportshort = destinationairportshort;
}
virtual ~Flight();
}
//Controller.h
#include [all other includes]
#include "../Model/Flight.h"
namespace Flightmanager
{
Controller::Controller():
mFlightArray(NULL),
mCurrentlyShownScreen(NULL)
{
initScreenSizeConstants();
initPlatformType();
//error: Unresolved symbol '__TZVN13Flightmanager6FlightE'.
initData();
//error: Unresoled symbol '__TZVN13Flightmanager6Flight13flightCounterE'.
mFlightTableView = new TableViewController(*this);//error: Unresoled symbol '__TZVN13Flightmanager6Flight13flightCounterE'.
mFlightDetailView = new DetailViewController();
}
}
I use MoSync Version 3.2
Build date: 121219-1556
Thx
You need to link in something that has definitions for:
Flight::flightCounter
Flight::~Flight()
whether that's a .o object file for Flight.cpp (or some source file) or a library depends on your project.
Related
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
i am new to C++ need some help on this
mainclinet.CPP
#include "client.h"
#include "config.h*"
int main(){
g_pClientSampleConfig = new ClientSampleConfig;
g_pUaSession = NULL;
g_pCallback = NULL;
g_nsIndex = OpcUa_UInt16_Max;
g_nsIndex2 = OpcUa_UInt16_Max;
client();
}
function client() is declared in this files mainclient.CPP
client.h
OpcUa_UInt16 g_nsIndex2;
ClientSampleConfig* g_pClientSampleConfig = NULL;
UaSession* m_pSession;
void Connection_Initialzation();
config.cpp
ClientSampleConfig::ClientSampleConfig()
{
//some code
}
ClientSampleConfig::~ClientSampleConfig()
{
}
config.h
class ClientSampleConfig
{
public:
ClientSampleConfig();
~ClientSampleConfig();
}
the problem is i created a new file called clinet_start.cpp
in which i am defined the client(); with return type void when i trying to include the clinet.h in clinet_start.cpp it's giving the linker Error 2005 *class UaClientSdk::UaSession * g_pUaSession"* redefined i need help in this
I suggest you could refer to the Doc:Linker Tools Error LNK2005
If mainclinet.CPP and clinet_start.cpp include clinet.h , then both are compiled with your definition of client(), so both object files will contain code for client(). When the linker gathers all the functions, he sees that client() is defined in two object files and does not know which one to use. Thus the linker raises an LNK2005.
There are two solutions for you:
1,If you want client() to be inlined, you could try to use Inline Functions (C++)
2,If you don't care about inlining, you could try to use extern (C++)
I decided to learn C++ DirectX, but whenever I compile/debug a code, even the simplest ones, i get the LNK2019: unresolved external symbol _WinMain# 16 referenced in function "int __cdecl_main(void)" (?invoke_main##YAHXZ) error and LNK1120.
I tested two different codes, one with class and other just int function alone:
#pragma once
class Main
{
public:
Main();
~Main();
};
Main::Main(int x)
{
}
Main::~Main()
{
}
int example()
{
return 0;
}
First and foremost you should do yourself a favour and learn C++ properly from a book. Bjarne Stroustrup, the designer and implementer of C++, has a great book which will teach you lots: Programming: Principles and Practice using C++
Your program cannot link because there is no main() function, which is required.
As Steephen has pointed out already in his answer, you can change your program so it includes at least the following:
int main()
{
return 0;
}
It looks like you were trying to substitute main() with example(), but your program and and C++ program needs to have a main(), as it's the main entry point of your program. You might also like to read http://www.cplusplus.com/doc/tutorial/program_structure/
To keep you class definition intact do following changes
Main::Main() // you don't need a parameter for your constructor
{
}
int main() //Not int example() because you need a main for your program
{
return 0;
}
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...
I am fairly new to cpp but have been in c# for a while. I am trying to run a simple console application but I receive this LNK2001 error message.
I have the main.cpp, and have added another class, Zeus, with files, Zeus.h and Zeus.cpp.
Here is the main.cpp:
#include "Zeus.h"
#include <iostream>
int main()
{
Zeus::tick = 25.0;
using std::cout;
cout << "nothing";
}
Here is the Zeus.h:
static class Zeus
{
public:
static void testing(void);
public:
static double tick;
};
And here is the Zeus.cpp:
void Zeus::testing(void)
{
//Doesnt get this far
//But eventually something like
// cout << "test " << Zeus::tick;
}
And here is the error message:
Error 20 error LNK2001: unresolved external symbol "public: static double Zeus::tick"
Thanks,
You need to define Zeus::tick, typically you would to that in the in the Zeus.cpp file. You have only declared it.
double Zeus::tick = 0.0;
Also, there is no static class in C++.
As an aside, free functions can be put in namespaces, as opposed to being static functions of classes. This is the preferred way in C++, unless there are strong reasons for the function to be static.
namespace Dionysus {
void testing();
}
As the error message says: there's no definition of Zeus::tick. Add this to Zeus.cpp:
double Zeus::tick;
Oh, and in Zeus.h remove the static from
static class Zeus
In the main() function you have, what do you mean by a statement Zeus::tick = 25.0;?
Zeus is a class. So to access the individual elements of it you need to create its instance. Its just like a structure where you first create its instance to access its individual elements.
Try the following:
int main() {
Zeus myobject;
myobject.tick = 25.0;
/* Rest of the definition */
}