wxwidgets app in Visual Studio gives error "LNK2019 unresolved external symbol" - c++

Im creating my first program with C++ and wxwidgets.
When I try to compile the project I get errors.
LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
LNK1120 1 unresolved externals
I have compiled the wxwidgets my self in Visual Studio.
After compiling I created a new C++ empty project in Visual Studio.
I went to configuration and added includes directories:
Configuration properties -> C/C++ -> General -> Additional include directories:
C:\Users\user\source\repos\wxWidgets\include; C:\Users\user\source\repos\wxWidgets\include\msvc
Configuration properties -> Linker -> Additional Library Directories:
C:\Users\user\source\repos\wxWidgets\lib\vc_lib
Then I added 2 classes, cApp and cMain.
cApp.h
#pragma once
#include "wx/wx.h"
#include "cMain.h"
class cApp : public wxApp
{
public:
cApp();
~cApp();
private:
cMain* m_frame1 = nullptr;
public:
virtual bool OnInit();
};
cApp.cpp
#include "cApp.h"
wxIMPLEMENT_APP(cApp);
cApp::cApp() {
}
cApp::~cApp() {
}
bool cApp::OnInit() {
m_frame1 = new cMain();
m_frame1->Show();
return true;
}
cMain.h
#pragma once
#include "wx/wx.h"
class cMain : public wxFrame
{
public:
cMain();
~cMain();
};
cMain.cpp
#include "cMain.h"
cMain::cMain() : wxFrame(nullptr, wxID_ANY, "MyProgram") {
}
cMain::~cMain() {
}

You have 2 problems (after the edit due to the comment below) the following problem:
You're building your application as a console mode application and not a GUI one. While it is possible to use wxWidgets from console applications too, this is probably not what you're trying to do, so ensure that the "Linker|System|SubSystem" option in the properties dialog of your project is set to "Windows".
You don't have wxIMPLEMENT_APP(cApp); macro in your code. Again, it is perfectly possible to avoid it, but this is probably not your goal here, so just add this line. This macro is what defines main or WinMain for your application, depending on the platform.

Your linker wants to find the main entry point (main is the default entry point). The wxIMPLEMENT_APP(cApp) macro on the Windows architecture has the WinMain entry point.
I have three quick solutions.
You can write a macro #define WinMain main before macro wxIMPLEMENT_APP(cApp)
You can tell the linker the name of the entry point(
In your case, the name of the entry point is WinMain).
project properties -> Configuration Properties -> Linker -> Advanced and on the right pane of the dialog is a place for the name of the entry point function.
Your Visual Studio project has a Console SubSystem installed (this SubSystem looks for the default main entry point). You must change the SubSystem value to WINDOWS (this SubSystem looks for the WinMain entry point).
Project properties -> Configuration Properties -> Linker -> System and then set the SubSystem property to Windows (/SUBSYSTEM:WINDOWS)

Related

Unresolved external symbol with google test framework

I try to implement google test framework for verifing dll library into my existed solution by visual studio community 2019. Basically, I have 3 files:
Source code is built in single dll project.
FooCmdMgr.cpp
FooCmdMgr.h
Unit test code using the latest google test framework in other project in a same solution with source code.
FooCmdMgr_Test.cpp
FooCmdMgr.h
class FooCmdMgr
{
public:
FooCmdMgr(const FooCmdMgr&) = delete;
FooCmdMgr& operator=(const FooCmdMgr& ) = delete;
bool func_1() {
return true;
}
private:
FooCmdMgr();
~FooCmdMgr();
static FooCmdMgr& Instance ( );
friend FooCmdMgr& GetFooCmdMgr ( );
};
inline FooCmdMgr& GetFooCmdMgr()
{
return FooCmdMgr::Instance();
}
FooCmdMgr_Test.cpp
#include "stdafx.h"
#include "../FooCmdMgr.h"
TEST_F(CJobChangeTestFixture, UnitTest2) {
FooCmdMgr& FooCmdMgr = ICmdEx::FooCmdMgr();
//bool bRet = FooCmdMgr.func_1();
//EXPECT_EQ(bRet, true);
}
Solution was compliled successful but linker was not OK.
It shown error LNK2019 something likes this:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "private: static class ICmdEx::FooCmdMgr & __cdecl ICmdEx::FooCmdMgr::Instance(void)" (?Instance#FooCmdMgr#ICmdEx##CAAEAV12#XZ) referenced in function "class ICmdEx::FooCmdMgr & __cdecl ICmdEx::GetIKYCmdMgr(void)" (?GetIKYCmdMgr#ICmdEx##YAAEAVFooCmdMgr#1#XZ) IEditorDll_GTest E:\work\project\UnitTest\FooCmdMgr_Test.obj
Project structure
E:\work\project\FooCmdMgr.vcxproj
E:\work\project\FooCmdMgr.cpp
E:\work\project\FooCmdMgr.h
E:\work\project\FooCmdMgr.dll
E:\work\project\FooCmdMgr.lib
Google test project
E:\work\project\UnitTest\FooCmdMgr_Test.vcxproj
E:\work\project\UnitTest\FooCmdMgr_Test.cpp
I also change the configuration of linker in gtest project
General/Additional Library Directories
$(ProjectDir)../
Input/Additional Dependencies/
FooCmdMgr.lib
I am struggling to figure out what happened with linker? Please give me advise.
your Foo class should be like this
#pragma once
#ifdef PROJECT1_EXPORTS
#define PROJECT1_API __declspec(dllexport)
#else
#define PROJECT1_API __declspec(dllimport)
#endif
class PROJECT1_API Foo
{
};
PROJECT1_EXPORTS is defined in your main source PREPROCESSOR project.
and it will NOT be define in your Test project
I had the same problem and fixed it by adding the output .obj/.lib files to the dependencies of the test project as described here: https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2015/test/unit-testing-existing-cpp-applications-with-test-explorer?view=vs-2015&redirectedfrom=MSDN#objectRef
I have Visual Studio 2022 with Google Test, and for me, it was enough to perform only steps 2 to 4 from the instruction. I.e.:
In Solution Explorer, on the shortcut menu of the test project, choose Properties. The project properties window opens.
Choose Configuration Properties, Linker, Input, Additional Dependencies.
Choose Edit, and add the names of the .obj or .lib files. Do not use the full path names.
Choose Configuration Properties, Linker, General, Additional Library Directories.
Choose Edit, and add the directory path of the .obj or .lib files. The path is typically within the build folder of the project under test.
I use NVIDIA CUDA, so needed to add its .lib file and path too.

Unresolved external symbol: QMetaObject const QwtPlotMagnifier::staticMetaObject

I have a library that uses QwtPlotMagnifier, amongst other Qwt classes. I decided to subclass QwtPlotMagnifier so that I can emit a signal when the plot is rescaled.
The library (mylib.lib) compiles, but the application using it is now complaining about an unresolved external related to the moc output of QwtPlotMagnifier.
I am linking qwt statically; so the requirement to have the preprocessor directive QWT_DLL in the lowest level library doesn't apply here.
Here's the error (the subclass is called PlotMagnifier):
mylib.lib(moc_PlotMagnifier.obj) : error LNK2001: unresolved external symbol "public: static struct QMetaObject const QwtPlotMagnifier::staticMetaObject" (?staticMetaObject#QwtPlotMagnifier##2UQMetaObject##B)
Nothing special about the subclass declaration:
#pragma once
#include "qwt_plot_magnifier.h"
/**
subclass of QwtPlotMagnifier to provide a signal when zooming is complete
*/
class PlotMagnifier : public QwtPlotMagnifier
{
Q_OBJECT
public:
explicit PlotMagnifier(QWidget *w);
virtual ~PlotMagnifier();
signals:
void rescaled();
protected:
virtual void rescale(double factor);
};
I'm on visual studio 2013 fwiw. My application still includes qwtd.lib as it always has. This has got to be a stupid mistake on my part.. kickstart my brain please, someone!
Add this line to .pro file to give the compiler a hint for an external symbol:
DEFINES += QWT_DLL
In the file qwt_global.h have the macro. Without this macro, the compiler will think this is an internal symbol.
Check, if you have all needed includes in you Visual Studio project.
C/C++ / Additional Include Directories
Here should be a path to <qwt_dir\include> be
Linker / General / Additional Library Directories
Here should be a path to <qwt_dir\lib> be
Linker / Input
Should include qwtd.lib (for debug configuration) and qwt.lib (for release)
Also, check that you have those entries in Release and Debug configurations, it is easy to configure only Debug, while working on Release configuration.
Also, check that you have moc_* file (something like moc_plotmagnifier.cpp) for your PlotMagnifier under Generated Files in your project view, sometimes Qt addin fails to add them.

How to solve the "unresolved external symbol" link error in visual c++?

I Have the Visual Studio C++ 2008 Express Edition.
I am trying to compile a program but I am getting the following Link Error:
1>MSVCRT.lib(wcrtexew.obj) : error LNK2001: unresolved external symbol _wWinMain#16
What I tried to do:
I found this on google:
For Visual C++ .NET: In the Advanced category of the Linker folder in the Project Properties dialog box, set the Entry Point to wWinMainCRTStartup.
It was intended to work but didn't. How do I compile this app?
The code is stupidly simple:
#include "stdafx.h"
int main( int argc, char ** argv )
{
}
There are multiple ways to solve this:
Create a console application
Change the Subsystem console now in the linker settings ( Project Settings -> Linker -> System -> SubSystem (select Console))
Change the entry point in the linker settings to mainCRTStartup (Project Settings -> Linker -> Advanced -> Entry Point)
Define int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow ); instead of int main(int argc, char const ** argv)
Change the Character Set to Use Unicode Character Set (Project Settings -> General->Character Set )
It looks like when you created your project you selected a GUI (Win32, MFC, etc) program.
Those programs have a WinMain() instead of a main().
What you want is a Console project.
According to similar questions, that error happens when main isn't defined.
Maybe for some reason it's compiling a different file?
This answer suggests that maybe your flags are inconsistent.

error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup

While I am running the simple code as below I have two errors as following:
#include <iostream>
#include <string>
using namespace::std;
template <class Type>
class Stack
{
public:
Stack (int max):stack(new Type[max]), top(-1), maxsize(max){}
~Stack (void) {delete []stack;}
void Push (Type &val);
void Pop (void) {if (top>=0) --top;}
Type& Top (void) {return stack[top];}
//friend ostream& operator<< (ostream&, Stack&);
private:
Type *stack;
int top;
const int maxSize;
};
template <class Type>
void Stack <Type>:: Push (Type &val)
{
if (top+1<maxsize)
stack [++top]=val;
}
Errors:
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
What Should I do?
Thats a linker problem.
Try to change Properties -> Linker -> System -> SubSystem (in Visual Studio).
from Windows (/SUBSYSTEM:WINDOWS) to Console (/SUBSYSTEM:CONSOLE)
This one helped me
As the others mentioned you can change the SubSystem to Console and the error will go away.
Or if you want to keep the Windows subsystem you can just hint at what your entry point is, because you haven't defined ___tmainCRTStartup. You can do this by adding the following to Properties -> Linker -> Command line:
/ENTRY:"mainCRTStartup"
This way you get rid of the console window.
If you are having this problem and are using Qt - you need to link qtmain.lib or qtmaind.lib
Besides changing it to Console (/SUBSYSTEM:CONSOLE) as others have said, you may need to change the entry point in Properties -> Linker -> Advanced -> Entry Point. Set it to mainCRTStartup.
It seems that Visual Studio might be searching for the WinMain function instead of main, if you don't specify otherwise.
Include <tchar.h> which has the line:
#define _tWinMain wWinMain
If you use Unicode Character Set, but the entry wasn't set, you can specify /ENTRY:"wWinMainCRTStartup"
If you actually want to use _tWinMain() instead of main()
make sure your project relevant configuration have
Linker-> System -> SubSystem => Windows(/SUBSYSTEM:WINDOWS)
C/C++ -> Preprocessor -> Preprocessor Definitions => Replace _CONSOLE with _WINDOWS
In the c/cpp file where _tWinMain() is defined, add:
#include <Windows.h>
#include <tchar.h>
i don't see the main function.
please make sure that it has main function.
example :
int main(int argc, TCHAR *argv[]){
}
hope that it works well. :)
If your project is Dll, then the case might be that linker wants to build a console program. Open the project properties. Select the General settings. Select configuration type Dynamic Library there(.dll).
I'm not sure where to post this answer of mine but I think it's the right place.
I came across this very error today and switching the subsystems didn't change a thing.
Changing the 64bit lib files to 32bit (x86) did the trick for me, I hope it will help someone out there !
Your tried to turn that source file into an executable, which obviously isn't possible, because the mandatory entry point, the main function, isn't defined. Add a file main.cpp and define a main function. If you're working on the commandline (which I doubt), you can add /c to only compile and not link. This will produce an object file only, which needs to be linked into either a static or shared lib or an application (in which case you'll need an oject file with main defined).
_WinMain is Microsoft's name for main when linking.
Also: you're not running the code yet, you are compiling (and linking) it. C++ is not an interpreted language.
If you are using CMake, you can also get this error when you set SET(GUI_TYPE WIN32) on a console application.
The erudite suggestions mentioned above will solve the problem in 99.99% of the cases. It was my luck that they did not. In my case it turned out I was including a header file from a different Windows project. Sure enough, at the very bottom of that file I found the directive:
#pragma comment(linker, "/subsystem:Windows")
Needless to say, removing this line solved my problem.

error LNK2005: new and delete already defined in LIBCMTD.lib(new.obj)

I have a Visual studio 2005 solution that has two projects. One is a static library and the other is a executable used to test the features in the static library. The static library uses MFC. I got the following errors when I built the solution.
uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2#YAPAXI#Z) already defined in LIBCMTD.lib(new.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (?? 3#YAXPAX#Z) already defined in LIBCMTD.lib(dbgdel.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U#YAPAXI#Z) already defined in libcpmtd.lib(newaop.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V#YAXPAX#Z) already defined in LIBCMTD.lib(delete2.obj)
I do not know how to overcome this. Can some one please explain why this error is occuring. Any explanation that gives an overview of .lib files linkage will be highly appreciated.
The CRT libraries use weak external linkage for the new, delete, and DllMain functions. The MFC libraries also contain new, delete, and DllMain functions. These functions require the MFC libraries to be linked before the CRT library is linked.
http://support.microsoft.com/kb/148652
Solution based on VS2005 (Replace Nafxcwd.lib with Uafxcwd.lib for ~VS2013)
go to project>properties>configuration properties>linker>input
add to "Additional dependency" -> Nafxcwd.lib Libcmtd.lib
add to "ignore specific library" -> Nafxcwd.lib;Libcmtd.lib
order of libraries is important( Nafxcwd.lib;Libcmtd.lib).
One thing to try is to make sure you have:
#include "stdafx.h"
as the first line in your .cpp files. I'm sure that's not the answer in all cases, but it made the identical error go away in my case.
I meet this problem in a MFC solution of Visual Studio 2010, while changing Use MFC in a Shared DLL into Use MFC in a Static Library in Project -> Properties -> Configuration Properties -> General.
I solve the problem by the following ways, please locate Project -> Properties -> Configuration Properties -> Linker -> Input at first.
In Debug mode:
Add uafxcwd.lib;Libcmtd.lib in Additional Dependencies.
Add uafxcwd.lib;Libcmtd.lib in Ignore Specific Default Libraries.
In Release mode:
Add uafxcw.lib;Libcmt.lib in Additional Dependencies.
Add uafxcw.lib;Libcmt.lib in Ignore Specific Default Libraries.
Notice:
Don't miss the ; between the two .lib files.
A suffix -d must be added in the files in Debug mode.
be sure that you have #include <afx.h> in "stdafx.h" BEFORE other includes like #include <string>
in config linker input
In additional dependicies put uafxcw.lib;LIBCMT.lib
In Ignore specific put put uafxcw.lib;LIBCMT.lib
Make sure the C++ runtime library that you are linking with is the same on your static library as well as your executable. Check your project properties C/C++->Code generation->runtime library settings.
Typo. One stupid way you got that is instead of include the header, you inlucde the cpp.
e.g.
#include <myclass.cpp> //should be #include <myClass.h>
First, libcmtd.lib is for a debug version and libcmt.lib is for production. Double-check that you're not including both. One place to check is the "Command Line" section of the Configuration Properties/Linker project properties.
If you go to the properties for the project, and open up the Configuration Properties/Linker/Input section, you can "Ingore Specific Library"...try listing libcmtd.lib in that field.
For me, I have a static library compiled with _CRTDBG_MAP_ALLOC, and the application not compiled with _CRTDBG_MAP_ALLOC, I was receiving then LNK2005. I've changed the application to compile with _CRTDBG_MAP_ALLOC, and the LNK2005 disappear.
Got rid of the problem
uafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new(unsigned __int64)"
In additional dependicies put uafxcw.lib.
In Ignore specific put put uafxcw.lib.
I had created two fresh projects with VS2017, one was working the other not, so I compared what was the difference. The one working was created with File > New Project > Visual C++ > MFC/ATL > MFC Application the one not working was created with File > New Project > Visual C++ > Windows Desktop > Windows Desktop Wizardthen adding MFC. In both cases I was using MFC as static lib. I had figured out two fixes. But before that we have to add imports because the second project had NONE!
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
Now either of the two fixes worked for me:
Project > Properties > Configuration Properties > General > Use of MFC set it to use in a Shared DLL, this should also automatically set C/C++ > Code Generation > Runtime Library to Multi-threaded debug dll /MDd make sure it indeed did that.
Try compile now, for me it worked.
I noticed the working project had some imports in stdafx.h, I copied them into pch.h in the other project, it worked.(Keeping the properties unchanged, so static lib was used). The code copied was this:
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
// turns off MFC's hiding of some common and often safely ignored warning messages
#define _AFX_ALL_WARNINGS
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
The other solutions changing Linker settings I tried them but they did not work.
I would appreciate if somebody knows why my solution works, it is weird, why including those headers in pch.h solves a linker issue whereas including those same headers anywhere else triggers that error??
Check the manifest file of both projects, make sure that they are linking the same version of the standard library. Most likely they are not, check the properties->code generation->standard library linking.
I also had a similar problem. The link given by Donnie explains the the reason. The solution was to look at the error messages and then removing those libs involved and adding those libs in the order of MFC libs first and then CRT libs.
The way to do that in vs2008 is given by ali.
I will also add that if you have replaced the new/delete operators (and if so, please do the array and the scalar both), you may need to tag them as __forceinline so that the obj doesn't collide with the lib.
For example, I did these to force aligned allocations and had the same trouble until I did that:
__forceinline void * operator new(size_t size)
{
return _aligned_malloc(size, 16);
}
__forceinline void operator delete(void* ptr)
{
_aligned_free(ptr);
}
__forceinline void * operator new [](size_t size)
{
return _aligned_malloc(size, 16);
}
__forceinline void operator delete [](void* ptr)
{
_aligned_free(ptr);
}
A header file declared and defined a variable. Possible solutions include:
Declare the variable in .h: extern BOOL MyBool; and then assign to it in a .c or .cpp file: BOOL MyBool = FALSE;.
Declare the variable static.
Declare the variable selectany.
https://msdn.microsoft.com/en-us/library/72zdcz6f.aspx
For me the problem was solved by changing
Project -> Properties -> Configuration Properties -> General: Use of
MFC = Use MFC in a Shared DLL
Before it was set to "Use Standard Windows Libraries"
Additionally I had to set the /MD option under
Project -> Properties -> C/C++ -> Code Generation : Runtime Library =
Multi-threaded DLL (/MD)
Another possible cause that I ran across while searching for this answer:
I accidentally left an #include "StdAfx.h" line at the top of a .cpp file that I moved from the application (which uses precompiled headers) into a shared static library (which doesn't use precompiled headers).
Got errors after applying Cipher Saw's solution to vs2015
1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "void __stdcall DDX_Control(class CDataExchange *,int,class CWnd &)" (?DDX_Control##YGXPAVCDataExchange##HAAVCWnd###Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "public: int __thiscall CWnd::ExecuteDlgInit(void *)" (?ExecuteDlgInit#CWnd##QAEHPAX#Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "public: void __thiscall CMFCDynamicLayout::GetHostWndRect(class CRect &)const " (?GetHostWndRect#CMFCDynamicLayout##QBEXAAVCRect###Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "void __cdecl AfxRegisterMFCCtrlClasses(void)" (?AfxRegisterMFCCtrlClasses##YAXXZ) already defined in uafxcwd.lib(afxctrlcontainer2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "protected: void __thiscall CMFCControlContainer::PreUnsubclassControl(class CWnd *)" (?PreUnsubclassControl#CMFCControlContainer##IAEXPAVCWnd###Z) already defined in uafxcwd.lib(afxctrlcontainer2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "public: int __thiscall CMFCControlContainer::SubclassDlgControls(void)" (?SubclassDlgControls#CMFCControlContainer##QAEHXZ) already defined in uafxcwd.lib(afxctrlcontainer2.obj)
Was able to fix them by changing libs list from uafxcw.lib;Libcmt.lib to afxnmcdd.lib;uafxcwd.lib;Libcmtd.lib (debug unicode build)