Unable to link in C++/CLI application - visual-studio

I am trying to wrap a C++ class "OpenViBE::Kernel::CPlayer" into a Managed C++ application, so I can later use it in C#.
#include "stdafx.h"
#include "ovkCPlayer.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
const OpenViBE::Kernel::IKernelContext* r=nullptr;
OpenViBE::Kernel::CPlayer* c=new OpenViBE::Kernel::CPlayer(*r);
//c->initialize();
return 0;
}
The above code compiles, it but does not link. The "OpenViBE::Kernel::CPlayer" is in project "OpenViBE-kernel-dynamic". I found where is the lib file from Properties->Linker->Advanced->Import Library. Then I added this folder to the lib path of my project (above) and the "OpenViBE-kernel-dynamic.lib" to the Linker->Input->Additional dependencies.
So the lib file is there, but the linker still can not link it:
error LNK2019: unresolved external symbol "public: __thiscall OpenViBE::Kernel::CPlayer::CPlayer(class OpenViBE::Kernel::IKernelContext const &)" (??0CPlayer#Kernel#OpenViBE##$$FQAE#ABVIKernelContext#12##Z) referenced in function ...
I do not have experience in writing C++/CLI applications, so I think I am missing something.

Related

How to dll import and export HINSTANCE from a .dll in c++?

I am trying to get the HINSTANCE of a .dll, but I get this error:
error LNK2019: unresolved external symbol "__declspec(dllimport) struct HINSTANCE__ * m_instance" (__imp_?m_instance##3PEAUHINSTANCE__##EA) referenced in function...
This is the code of the .dll:
#include <Windows.h>
__declspec(dllexport) HINSTANCE m_instance;
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID)
{
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
m_instance = (HINSTANCE)hModule;
}
return TRUE;
}
This is the code of the application:
__declspec(dllimport) HINSTANCE m_instance;
// use it for whatev
In summary, I need a way to get the HINSTANCE from my .dll into my .exe.
In summary, I need a way to get the HINSTANCE from my .dll into my .exe.
But, why??? Once the .exe has loaded the .dll into its memory, the .exe already has access to the .dll's HINSTANCE, from the return value of either LoadLibrary/Ex() or GetModuleHandle(). So, there is never a need to have a .dll export its own HINSTANCE.
The application is failing to link against the DLL's import library. By default, MSVC's linker generates an import library (LIB) when producing a DLL, that has the same base name as the DLL.
To allow the linker to resolve the symbol m_instance the application needs the DLL's import library as a linker input file. See .Lib Files as Linker Input to learn how to add linker input files in Visual Studio.
Though not strictly required, it's usually a good idea to export symbols using C linkage. While C++ linkage is largely unspecified and very toolchain-specific, C name decoration is de-facto standardized. The changes required are tiny (though, again, not required):
extern "C" __declspec(dllexport) HINSTANCE m_instance;
and
extern "C" __declspec(dllimport) HINSTANCE m_instance;

getting nafxcwd.lib(timecore.obj) error lnk2001 unresolved external symbol __mbctype after header-include from other project

I made a new project for unittesting with googleTest. I then added a source file from another project to test it. I then got severeal linker errors:
nafxcwd.lib(timecore.obj) : error LNK2001: unresolved external symbol __mbctype
nafxcwd.lib(apphelp.obj) : error LNK2001: unresolved external symbol __mbctype
nafxcwd.lib(filelist.obj) : error LNK2001: unresolved external symbol __mbctype
nafxcwd.lib(appcore.obj) : error LNK2001: unresolved external symbol ___argv
nafxcwd.lib(appcore.obj) : error LNK2001: unresolved external symbol ___argc
The key options in my projects are:
Testproject:
Runtime Library: Multithreaded Debug DLL (necessary for googleTest)
Use of MFC: Shared DLL
productive project: (this should actually not matter)
Runtime Library: Multithreaded Debug DLL
Use of MFC: Use MFC in a static library
Even if I copy all the options and files from the productive project with MFC as shared DLL these messages get thrown.
The file, that was imported to the testproject had the "stdafx.h"-include that was resolved to the productive projects one.
There,
#include <afxwin.h>
was included. Checking that one turned out, that for shared MFC-DLL, you need to
#define _AFXDLL
in your project / before the afxwin-include. Otherwise you get these errors.

VC++ Multiple Project Solution

I am very new to C++ and I am attempting to setup multiple projects in the same solution in VS2013. Currently I have stepped back to a simpler example project to try to figure out my error.
Project 1:
Main.cpp
#include "Test.h"
#include <iostream>
using namespace std;
int main()
{
cout << _MOVEMENTSPEED();
system("pause");
return 0;
}
Project 2
Test.h
#ifndef TEST_H
#define TEST_H
int _MOVEMENTSPEED();
#endif
Test.cpp
#include "Test.h"
int _MOVEMENTSPEED()
{
return 10;
}
Whenever I attempt to build this I get the error "error LNK2019: unresolved external symbol "int __cdecl _MOVEMENTSPEED(void)" (?_MOVEMENTSPEED##YAHXZ) referenced in function _main c:\Users\Max\documents\visual studio 2013\Projects\Project1\Project2\Main.obj" and "Error 2 error LNK1120: 1 unresolved externals c:\users\max\documents\visual studio 2013\Projects\Project1\Debug\Internal".
UPDATE
I tested this same code but within one project file in visual studio and it worked fine.
When you create multiple projects you should do the following:
Make sure the following:
Include the .h file properly from other project(Generally every project has its own directory), so you need to include the file like below:
#include "..\Test\Test.h"
Export the function / class by using _declspec(dllexport) and _declspec(dllimport)
Include the .lib file properly in the project settings of Link tab.
Set the project dependencies correctly.
The below links should help you:
http://support.microsoft.com/kb/815650
http://msdn.microsoft.com/en-us/library/799kze2z.aspx

glload and LNK errors in VC++ EE 2010

I'm trying to use glload from the Unofficial OpenGL SDK but I get LNK errors:
1> LINK : C:\Users\T\documents\visual studio 2010\Projects\testi\Debug\testi.exe not found or not built by the last incremental link; performing full link
1>glloadD.lib(gll_gl_ext.obj) : error LNK2019: unresolved external symbol __imp__wglGetProcAddress#4 referenced in function _WinGetProcAddress
1>glloadD.lib(wgll_ext.obj) : error LNK2001: unresolved external symbol __imp__wglGetProcAddress#4
1>C:\Users\T\documents\visual studio 2010\Projects\testi\Debug\testi.exe : fatal error LNK1120: 1 unresolved externals
#include <glload/gl_all.h>
#include <glload/gll.hpp>
void main()
{
glload::LoadFunctions();
}
Linker/Additional Dependencies: glloadD.lib
Where is the problem ?
Edit 1:
First I used Premake to generate build files for vs2010. Then I built all libraries. In my project I set Additional Include Directories, Additional Library Directories and Additional Dependencies for those libraries. I want to run an example from this page: link but I forgot to create OpenGL context before loading opengl functions. I don't need a window in this project so I just call glutInit, but I get an unhandled exception at 0x5bfed398 (msvcr100d.dll)
#include <glload/gl_all.h>
#include <glload/gll.hpp>
#include <freeglut/freeglut.h>
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glload::LoadFunctions();
}
Edit 2:
Calling glutCreateWindow before glload::LoadFunctions seems to be necessary. Following code works:
#include <glload/gl_all.h>
#include <glload/gll.hpp>
#include <freeglut/freeglut.h>
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutCreateWindow("");
glload::LoadFunctions();
}
The solution is to create a working context before initializing GlLoad. I've had this problem every time i've started an OpenGL project with GLSDK.

Using tinyxml2 with a Visual Studio, Visual C++ MFC Application

I'm trying to get accustomed to both MFC development and Visual Studio so I have installed Visual Studio 2012 RC and have created a simple MFC Application. Currently the application is little more than the MFC Wizard generated for me.
I decided I wanted to incorporate an XML Library so I found this one on github. I download the ZIP file with the source code, unzip it and then in Visual Studio I goto the solution explorer, choose my solution, right click and choose "Add" > "Existing Project". I select the project file for the source code and it appears in my Solution Explorer tree.
I test the code compiles and it does. However I'm not quite sure how to use it from my current solution.
I try to use this code in my doc:
#include "../../TinyXML2/leethomason-tinyxml2-a3efec0/tinyxml2.h"
<...snip...>
BOOL LoadDocumentFromXML(const CString& filename) {
CT2CA pszConvertedAnsiString (filename);
std::string s(pszConvertedAnsiString);
tinyxml2::XMLDocument doc(true);
if (tinyxml2::XML_NO_ERROR != doc.LoadFile(s.c_str())) {
return FALSE;
}
return TRUE;
}
However I get this linker error when I try to build the project:
------ Build started: Project: GraphApp, Configuration: Debug Win32 ------
GraphAppDoc.cpp
GraphAppDoc.obj : error LNK2019: unresolved external symbol "public: __thiscall tinyxml2::XMLDocument::XMLDocument(bool)" (??0XMLDocument#tinyxml2##QAE#_N#Z) referenced in function "int __cdecl LoadDocumentFromXML(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?LoadDocumentFromXML##YAHABV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z)
GraphAppDoc.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall tinyxml2::XMLDocument::~XMLDocument(void)" (??1XMLDocument#tinyxml2##UAE#XZ) referenced in function "int __cdecl LoadDocumentFromXML(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?LoadDocumentFromXML##YAHABV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z)
GraphAppDoc.obj : error LNK2019: unresolved external symbol "public: int __thiscall tinyxml2::XMLDocument::LoadFile(char const *)" (?LoadFile#XMLDocument#tinyxml2##QAEHPBD#Z) referenced in function "int __cdecl LoadDocumentFromXML(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?LoadDocumentFromXML##YAHABV?$CStringT#_WV?$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z)
C:\Users\Phill\Documents\Visual Studio 2012\Projects\GraphApp\Debug\GraphApp.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
In the solution explorer I select my MFC application project, right click and choose "Dependencies". I make sure the MFC app is set to be dependent on tinyxml project and I ensure the "Build Order" is correct (tinyxml first). I also goto into "References..." and add tinyxml there too. I even add the debug directory of tinyxml to my MFC app's include path in project properties as well. What am I missing please?
Ok it turns out I didn't read the documentation fully. Reading the XML attached the project here states:
It is one header and one cpp file. Simply add these to your project
and off you go.
So I did this. Then I got some compiler warnings:
------ Build started: Project: GraphApp, Configuration: Debug Win32 ------
tinyxml2.cpp
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(24): warning C4627: '#include "tinyxml2.h"': skipped when looking for precompiled header use
Add directive to 'stdafx.h' or rebuild precompiled header
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(26): warning C4627: '#include <cstdio>': skipped when looking for precompiled header use
Add directive to 'stdafx.h' or rebuild precompiled header
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(27): warning C4627: '#include <cstdlib>': skipped when looking for precompiled header use
Add directive to 'stdafx.h' or rebuild precompiled header
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(28): warning C4627: '#include <new>': skipped when looking for precompiled header use
Add directive to 'stdafx.h' or rebuild precompiled header
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(29): warning C4627: '#include <cstddef>': skipped when looking for precompiled header use
Add directive to 'stdafx.h' or rebuild precompiled header
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(1834): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
So then I selected "tinyxml2.cpp" in the solutions explorer, right clicked, went to properties. In properties under "C++" I went to "Precompiled Headers" and changed the option that read:
Precompiled Header: Use
to
Precompiled Header: Not using precompiled headers
Then it magically worked!

Resources