MFC C++ static library linked with non MFC console app - c++

I'm trying to compile a console program that uses a static library implementing CString.The console app has been created with the VS wizard with :
Win32 console application with precompiled headers, SDL verification but without ATL or MFC.
The static library is a MFC static library (wizard construction).
Where is (are) my mistake(s) ?
This is what I so long have tried:
I've created a new console app using MFC controls - this compile fine with the static library.
Then I've controlled and modified when necessary every link options, comparing the 2 console projects.
But the 1st console application does not compile.
I'm stucked !
I'm working with Visual Studio 2012 on Windows 10.
Here is the code :
File TestLib.h
#pragma once
#include <atlstr.h>
class TestLib
{
public:
TestLib(){};
TestLib(const CString &tst);
virtual ~TestLib(void);
private:
CString _tst;
};
Fichier TestLib.cpp
#include "stdafx.h"
#include "TestLib.h"
TestLib::TestLib(const CString &tst)
: _tst(tst)
{
}
TestLib::~TestLib(void)
{
}
Fichier ConsoleTest2.cpp
// ConsoleTest2.cpp : définit le point d'entrée pour l'application console.
#include "stdafx.h"
#include "TestLib.h"
int _tmain(int argc, _TCHAR* argv[])
{
TestLib *tst = new TestLib(); // This compile fine !
//TestLib *tst = new TestLib(_T("Test")); // This generates LNK2019 link error
return 0;
}

Here is the solution, thanks to Jochen Arndt
Just have to change the TestLib declaration to
TestLib::TestLib(LPCTSTR str)
: _tst(str)
{
}

Related

Requiring C++ DLL from Lua gives an error

I am trying to compile C++ code to a DLL file to use with Lua. To do this, I am using Visual Studio 2022 with Windows 10. My goal is to use the Lua require(modname) function to require my DLL and use the C++ methods in Lua. For now the DLL and the main.lua files are in the same directory. What is actually happening is that I get an error message saying:
error loading module 'lgf' from file 'lgf.dll': The specified module could not be found.
with the Lua code:
package.cpath = "?.dll;"..package.cpath
create = require("lgf")
My main.cpp file contains the following:
#include <iostream>
#include <string>
#include <window/include/window.hpp>
#include <font/include/font.hpp>
#include <image/include/image.hpp>
#include <keyboard/include/keyboard.hpp>
#include <mouse/include/mouse.hpp>
#include <rectangle/include/rectangle.hpp>
#include <lua/lua.hpp>
#define LIB
SDL_Window* sdlWin;
SDL_Renderer* sdlRen;
window win(sdlWin, sdlRen);
static const luaL_Reg lib[] = {
{"create", window::create},
{"isCloseRequested", window::isCloseRequested},
{"sync", window::sync},
{"update", window::update},
{"setVSync", window::setVSync},
{"setIcon", window::setIcon},
{"clearScreen", window::clearScreen},
{"render", window::render},
{"windowChangeColor", window::changeColorRGB},
{"close", window::close},
{"createRectangle", rectangle::create},
{"changeRectangleColor", rectangle::changeColor},
{"drawRectangle", rectangle::draw},
{"mouseButtonUp", Mouse::mouseButtonUp},
{"mouseButtonDown", Mouse::mouseButtonDown},
{"keyup", Keyboard::keyup},
{"keydown", Keyboard::keydown},
{"loadImage", ImageLoader::loadImage},
{"drawImage", ImageLoader::drawImage},
{"loadFont", FontLoader::loadFont},
{"loadText", FontLoader::loadText},
{"renderText", FontLoader::renderText},
{NULL, NULL}
};
extern "C" __declspec(dllexport) int luaopen_lgf(lua_State* L) {
luaL_newlib(L, lib);
return 22;
}
I am creating a simple graphics library. My dependencies are SDL2 and (of course) Lua.
I have tried creating an entry point (main) for the file, and I still get the same message. Let me know if I should include my other .cpp files in here.

C++ dll in hololens

I am working creating a hololens application using a native code dll based on c ++. The problem comes when I add it in the unity project (plugins / WSA / x86).
When generating the UWP solution in Visual Studio I get a failure of DllNotFound.
From what I have been able to read, it is necessary to create a UWP library to use it in my application. That library must contain my native code. The truth is that I'm not sure how to do that. Is there any way to stop my dll based on c ++ on a UWP dll ??
error: System.DllNotFoundException: Unable to load DLL 'nativoHololensPrueba.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E).
C++
SOURCE:
#include <iostream>
#include <stdio.h>
#include <memory>
#include "Header.h"
__declspec(dllexport)
int testo() {
return 10;
}
HEADER:
extern "C" {
__declspec(dllexport)
int testo();
}
c#
[DllImport("nativoHololensPrueba")]
public static extern int testo();
// Use this for initialization
public GameObject texto;
void Start () {
texto.GetComponent<TextMesh>().text = "Cambiando el nombre " + testo();
}
I also had this and I solved it by compiling the dll on ARM64 (or ARM) with /MT flag.

C++ Qt5 with net-snmp crashes after build

I am trying to use the http://www.net-snmp.org library in my project but after it build it crashes without the chance to get debug information.
To be more clear:
I hit the run button -> Starting program.exe -> The program crashed without any output or window
I am using the Windows 7 64 bit, MSVC++ 2010 32 Bit compiler Qt5.0.2 and the binaries for net-snmp from
http://softlayer-ams.dl.sourceforge.net/project/net-snmp/net-snmp%20binaries/5.7-binaries/net-snmp-5.7.0-1.x86.exe
I added these lines in my *.pro file to include the library
win32:INCLUDEPATH += "C:\snmp_5.7.0\include"
LIBS += -L"C:\snmp_5.7.0\lib" -lnetsnmp -lnetsnmpmibs -lnetsnmptrapd
and wanted to follow the "Simple application tutorial" (http://www.net-snmp.org/wiki/index.php/TUT:Simple_Application)
This is what I got so far:
snmpTest.h
#ifndef SNMPTEST_H
#define SNMPTEST_H
#include <QDebug>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/library/transform_oids.h>
class SnmpTest
{
public:
SnmpTest();
void doSnmp();
};
#endif // SNMPTEST_H
snmpTest.cpp
#include "snmptest.h"
SnmpTest::SnmpTest()
{
}
void SnmpTest::doSnmp()
{
const char *our_v3_passphrase = "XXXX";
struct snmp_session session, *ss;
struct snmp_pdu *pdu;
struct snmp_pdu *response;
oid anOID[MAX_OID_LEN];
size_t anOID_len = MAX_OID_LEN;
struct variable_list *vars;
int status;
/*
* Initialize the SNMP library
*/
init_snmp("app");
}
If I remove the line
init_snmp("app");
It builds and runs just fine.
I solved the problem by compiling the libary by myselfe
I also encountered this problem, first of all need to compile the net-snmp library, and then compile the netsnmp.dll need to be placed under the program executable file directory.

C++ Linker Error (Unresolved External) when linking to /clr DLL

I have a DLL with the /clr option ON. I have the following declaration in my DLL:
int __declspec(dllexport) __cdecl test();
Also, I have a console with /clr option ON. And have the following declaration on my main.cpp file:
int __declspec(dllimport) __cdecl test();
I added the Reference to the DLL project on the property settings of my console application. But I still get unresolved externals from the compiler about the test function.
I managed to compile by manually adding a reference to the lib file generated by the compiler. But then I can't hit breakpoints inside the DLL functions (it says the source code is different from the original version or the symbols have not been loaded...)
Can someone help me?
If your DLL doesn't use any managed functionality, simply remove the /clr option from that project and recompile. If you still get the errors, it's probably related to the references in the console application.
If the DLL use managed functionality, what you need is instead like so:
DLL:
#include "stdafx.h"
namespace Test1
{
public ref class Test2
{
public:
static int test()
{
return 1;
}
};
}
Console app:
#include <iostream>
int main(int argc, char* argv[])
{
int i = Test1::Test2::test();
std::cout << i << std::endl;
return 0;
}

Link a C++/CLI library in a CLR console project

I see on a question that it wasn't possible to link a static C++/CLI library, so I generated a dynamic one and try to link it in a C++ CLR console project with no success
I get:
Error 1 error LNK2020: unresolved token (06000001) cBox::.ctor pTest9.obj
I put the dll (Project ->reference->add new reference) set the include files in the project
(include file with no code)
I don't know what to do (I am new with the C++/CLI)
thanks for suggestions/solution
Library Project declared as DLL
#include "stdafx.h"
using namespace System;
ref class cBox
{
public:
cBox() ;
cBox(double lv,double bv,double hv);
double Volume();
private:
double Length;
double Width;
double Height;
};
CODE OF THE LIBRARY :
#include "stdafx.h"
#include "cBox.h"
cBox::cBox()
{
Console::WriteLine(L"No arg constructor called");
Length = 1.0 ;
Width = 1.0 ;
Height = 1.0 ;
}
cBox::cBox(double lv,double bv,double hv)
{
Console::WriteLine(L"Constructor called");
Length = lv;
Width = bv;
Height = hv;
}
double cBox::Volume()
{
return Length*Width*Height;
}
Then in the Console CLR Project I try to link this library, I get its .h file
#include "stdafx.h"
#include "cBox.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Test Library:\n");
cBox^ oBox; // handle of type box
oBox = gcnew cBox;
Console::WriteLine(L"Default Box Volume {0}",oBox->Volume());
return 0;
}
You need to make a couple of changes to get this to work. First, you need to make the class public so your application can use it:
public ref class cBox
{
...
And you need to remove the #include "cBox.h" from your console application project; the inclusion is implicit when using managed libraries.