Click here to Skip to main content
15,881,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody.

I'm trying to compile a console program that uses a static library implementing CString.

I'm working with Visual Studio 2012 on Windows 10.

Here is the code :

File TestLib.h
C++
#pragma once
#include <atlstr.h>

class TestLib
{
public:
	TestLib(){};
	TestLib(const CString &tst);
	virtual ~TestLib(void);
private:
	CString _tst;
};


File TestLib.cpp
C++
#include "stdafx.h"
#include "TestLib.h"


TestLib::TestLib(const CString &tst)
	: _tst(tst)
{
}


TestLib::~TestLib(void)
{
}


File

// 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;
}


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) ?

What I have tried:

I've crated 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 !
Posted
Updated 17-Jul-17 0:58am
v2

1 solution

It would be good to see the full error message to see which function(s) are missing.

If you build your app without MFC support, it is not linked with the library that contains the CString support functions. Because you are including atlstr.h, you are not getting compilation errors. But in the line
TestLib *tst = new TestLib(_T("Test"));
you are creating a temporary CString and the constructor calls functions from the MFC library which results in the linker error.

The simplest solution would be changing the second constructor of your library to
TestLib::TestLib(LPCTSTR str)
	: _tst(str)
{
}
Then your console application passes a TCHAR* pointer instead of creating a temporaray CString and the assignment is done within your library which is linked with the MFC library.
 
Share this answer
 
Comments
Alain Leglise 17-Jul-17 7:59am    
To answer your first purpose, here is the full error message (sorry in french, but important is the function)

Erreur 1 error LNK2019: symbole externe non résolu "public: __thiscall TestLib::TestLib(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (??0TestLib@@QAE@ABV?$CStringT@_WV?$StrTraitATL@_WV?$ChTraitsCRT@_W@ATL@@@ATL@@@ATL@@@Z) référencé dans la fonction _wmain C:\Users\Alain\Documents\Visual Studio 2012\Projects\CStringLibraryTest\ConsoleTest2\ConsoleTest2.obj ConsoleTest2
Jochen Arndt 17-Jul-17 8:15am    
Most of the CString stuff is inlined but the error shows that the symbols ATL::StrTraitATL and ATL::ChTraitsCRT can not be found.

Thank you for accepting my solution.
Alain Leglise 17-Jul-17 8:26am    
So far, the best shot I ever found :)
Thanks to you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900