LNK2019 while using SDL2 and vcpkg [duplicate] - visual-studio

This question already has answers here:
Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
(20 answers)
Closed 6 months ago.
I am using SDL2 and vcpkg.
I install sdl2 by vcpkg and build project in Visual Studio 2022 Debug x86
LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
code:
#include "SDL2/SDL.h"
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
int main(int argc, char* argv[]) {
SDL_Window* window = NULL;
SDL_Surface* surface = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("%s\n", SDL_GetError());
}
return 0;
}

(More of a comment than a solution, but I cannot comment yet)
Have you tried the solutions proposed in this thread?
According to an answer there, the most probable solution would be that your Linker's SubSystem setting is not set to Windows:
Apart from that, there are some other potential solutions in that thread, if none of them work, please report back.

Related

Manual OpenGL Extension Loading LNK2005 and LNK1169

I'm using Visual C++ and manually attempting to load OpenGL extensions. However, for some reason defining the pointers from the Khronos Groups' provided headers leads to linker errors and as such I never get the change to even define these functions within my OpenGL context. Below I have included a simplified version of my code and it's structure that causes the same issue.
//Test.cpp
#include "MyGL.h"
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
}
//MyGL.h
#pragma once
#include "MyGLOpenGL.h"
//MyGL.cpp
#include "MyGL.h"
//MyGLOpenGL.h
#pragma once
#include <windows.h> // Windows functions
#include <GL/gl.h> // Provided w/ Compiler
#include "GL/glext.h" // Put out by Khronos Group
#include "GL/wglext.h" // Put out by Khronos Group
#pragma comment(lib, "opengl32.lib") // Provided w/ Compiler
#ifndef GL_OPENGL
#define GL_OPENGL
void glInitPointers(); // Defines pointers to opengl functions
void* glGetAnyProcAddress(const char* name); // Gets a pointer to any OpenGL function
extern PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
extern PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
#endif
//MyGLOpenGL.cpp
#include "MyGLOpenGL.h"
void glInitPointers() {
wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)glGetAnyProcAddress("wglChoosePixelFormatARB"); //load function
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)glGetAnyProcAddress("wglCreateContextAttribsARB"); //load function
return;
}
void* glGetAnyProcAddress(const char* name) {
void *pointer = (void *)wglGetProcAddress(name);
if (pointer == 0 || (pointer == (void*)0x1) || (pointer == (void*)0x2) || (pointer == (void*)0x3) || (pointer == (void*)-1)) {
HMODULE module = LoadLibraryW(L"opengl32.dll");
pointer = (void *)GetProcAddress(module, name);
}
return pointer;
};
Compiling this gives me the following linker errors:
1>MyGLOpenGL.obj : error LNK2001: unresolved external symbol "int (__stdcall* wglChoosePixelFormatARB)(struct HDC__ *,int const *,float const *,unsigned int,int *,unsigned int *)" (?wglChoosePixelFormatARB##3P6GHPAUHDC__##PBHPBMIPAHPAI#ZA)
1>MyGLOpenGL.obj : error LNK2001: unresolved external symbol "struct HGLRC__ * (__stdcall* wglCreateContextAttribsARB)(struct HDC__ *,struct HGLRC__ *,int const *)" (?wglCreateContextAttribsARB##3P6GPAUHGLRC__##PAUHDC__##PAU1#PBH#ZA)
1>G:\Development\Test\Debug\Test.exe : fatal error LNK1120: 2 unresolved externals
As the issue was unresolved external symbols, I tried removing the extern keyword to arrive at getting these two errors instead:
1>MyGLOpenGL.obj :error LNK2005: "int (__stdcall* wglChoosePixelFormatARB)(struct HDC__ *,int const *,float const *,unsigned int,int *,unsigned int *)" (?wglChoosePixelFormatARB##3P6GHPAUHDC__##PBHPBMIPAHPAI#ZA) already defined in MyGL.obj
1>MyGLOpenGL.obj : error LNK2005: "struct HGLRC__ * (__stdcall* wglCreateContextAttribsARB)(struct HDC__ *,struct HGLRC__ *,int const *)" (?wglCreateContextAttribsARB##3P6GPAUHGLRC__##PAUHDC__##PAU1#PBH#ZA) already defined in MyGL.obj
1>Test.obj : error LNK2005: "int (__stdcall* wglChoosePixelFormatARB)(struct HDC__ *,int const *,float const *,unsigned int,int *,unsigned int *)" (?wglChoosePixelFormatARB##3P6GHPAUHDC__##PBHPBMIPAHPAI#ZA) already defined in MyGL.obj
1>Test.obj : error LNK2005: "struct HGLRC__ * (__stdcall* wglCreateContextAttribsARB)(struct HDC__ *,struct HGLRC__ *,int const *)" (?wglCreateContextAttribsARB##3P6GPAUHGLRC__##PAUHDC__##PAU1#PBH#ZA) already defined in MyGL.obj
1>G:\Development\Test\Debug\Test.exe : fatal error LNK1169: one or more multiply defined symbols found
I've also already made sure that "#pragma once" was in ever file, header guards, adding "OpenGL32.lib" into the additional dependencies in visual studio, adding a pragma comment for the lib, setting the pointers equal to null in the declaration, and I'm just at a complete loss for anything else to try, even after googling the issue. And in my case, using GLEW or any other extension loading library because that is exactly what I'm trying to create.
In C and C++ there's an important difference between declaration and definition. A statement of the form
extern <type> <symbol>;
declares that there is some symbol somewhere, but it doesn't actually bring it into existence. It's more like a promise to the compiler, that the symbol will be defined somewhere else. When you wrote
extern PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
extern PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
you actually didn't create the variables for those function pointers, you just "listed them in the table of contents" for your program. You actually have to define them somewhere. I.e. in some compilation unit you have to write (without the extern).
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
This has absolutely nothing to do with initialization, as hinted by #EricLopushansky; it's all about them being getting actually defined. Since these are global scope, they'll be initialized to 0 anyway, even if you don't explicitly write that = 0.
This isn't very well explained by the OpenGL wiki in the article Load OpenGL Functions but simply do the following. Setting the pointer to null within the cpp file will fix the issue.
For example:
PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = NULL;
For anyone interested, here is the relevant code from GLEW that serves the same purpose:
//glew.h
#ifdef GLEW_STATIC
# define GLEWAPI extern
#else
# ifdef GLEW_BUILD
# define GLEWAPI extern __declspec(dllexport)
# else
# define GLEWAPI extern __declspec(dllimport)
# endif
#endif
#define GLEW_FUN_EXPORT GLEWAPI
//wglew.h
#define WGLEW_GET_FUN(x) x
#define wglChoosePixelFormatARB WGLEW_GET_FUN(__wglewChoosePixelFormatARB)
typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int* piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
#define WGLEW_FUN_EXPORT GLEW_FUN_EXPORT
WGLEW_FUN_EXPORT PFNWGLCHOOSEPIXELFORMATARBPROC __wglewChoosePixelFormatARB;
//glew.c
# define glewGetProcAddress(name) wglGetProcAddress((LPCSTR)name)
PFNWGLCHOOSEPIXELFORMATARBPROC __wglewChoosePixelFormatARB = NULL;
r = ((wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)glewGetProcAddress((const GLubyte*)"wglChoosePixelFormatARB")) == NULL) || r;
//I would be using this static so it would evaluate in my code to
//MyGLOpenGL.h
#define wglChoosePixelFormatARB __wglewChoosePixelFormatARB
typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int* piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
extern PFNWGLCHOOSEPIXELFORMATARBPROC __wglewChoosePixelFormatARB;
//MyGLOpenGL.cpp
PFNWGLCHOOSEPIXELFORMATARBPROC __wglewChoosePixelFormatARB = NULL;
wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress((const GLubyte*)"wglChoosePixelFormatARB")

Cannot link to WinApi in Qt Console application [duplicate]

This question already has answers here:
Qt: windows functions are unresolved external symbols
(2 answers)
Closed 5 years ago.
I am trying to access some functions from WinApi in a Qt project with no success.
Here is the code:
#include <QCoreApplication>
#include <iostream>
#include <string>
#include <stdio.h>
#include <windows.h>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
char title[1024];
HWND hwndHandle = GetForegroundWindow();
GetWindowText(hwndHandle, (LPWSTR)title, 1023);
return a.exec();
}
And these are the errors I get:
main.obj:-1: error: LNK2019: unresolved external symbol
__imp__GetWindowTextW#12 referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol
__imp__GetForegroundWindow#0 referenced in function _main
is there any lib I should include in pro file to make this work?
Accordind to the MSDN documentation those functions in library User32. So you have to add
LIBS += -luser32
in pro file.

Unresolved external symbols since Visual Studio 2015 update 3: boost python linking error if destructor is virtual

We are getting strange unresolved symbols linking errors since we updated to Visual Studio 2015 update 3. Anyone else encountered the same kind of issues ?
What is really weird is that boost::get_pointer is a template method, defined in a boost header. I do not understand how we can get an undefined external symbol in that case :(.
Here is a reproducer, with boost 1.61 and Python 3.5.1:
#include <vector>
#include <boost/python.hpp>
using namespace boost::python;
class Canard {
public:
Canard() {}
virtual ~Canard() {}
};
BOOST_PYTHON_MODULE(coin)
{
register_ptr_to_python< std::shared_ptr<Canard> >();
}
And the error:
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "class Canard const volatile * __cdecl boost::get_pointer<class Canard const volatile >(class Canard const volatile *)" (??$get_pointer#$$CDVCanard###boost##YAPEDVCanard##PEDV1##Z) referenced in function "private: static struct _typeobject * __cdecl boost::python::objects::make_ptr_instance<class Canard,struct boost::python::objects::pointer_holder<class std::shared_ptr<class Canard>,class Canard> >::get_derived_class_object<class Canard>(struct boost::mpl::bool_<1>,class Canard const volatile *)" (??$get_derived_class_object#VCanard###?$make_ptr_instance#VCanard##U?$pointer_holder#V?$shared_ptr#VCanard###std##VCanard###objects#python#boost###objects#python#boost##CAPEAU_typeobject##U?$bool_#$00#mpl#3#PEDVCanard###Z) CCMasterKernelPyPy C:\work\dev\builds\internal\Master\SDK\MasterKernelPyPy\main.obj 1
But as soon as I remove the virtual in front of the destructor of the Canard class, then it starts working.... Does anyone have a clue ? Is it a Visual Studio bug ?
Visual Studio 2015 update 3 has added lots of features and improvements (see the release notes https://www.visualstudio.com/news/releasenotes/vs2015-update3-vs#visualcpp).
It also have some known issues (https://msdn.microsoft.com/vs-knownissues/vs2015-update3 see the Passing non-pointer-like types to uninitialized_copy section).
To fix your problem you need to explicitly specify the conversion to pointer of your class, explicitly:
namespace boost
{
template <>
Canard const volatile * get_pointer<class Canard const volatile >(
class Canard const volatile *c)
{
return c;
}
}
Good luck,
Ohad

Symbols Missing for nsiCookieManager2 program

Please help me with the missing LIBs for this MOZILLA program.
Trying to create cookie using nsICookieManager2
I have tried with all the existing libs in Mozilla SDK
Regards
C:\Code>cl.exe FFCookie.cpp /I "C:\xulrunner-sdk\include" mozalloc.lib xpcomglue.lib /link /LIBPATH:"C:\xulrunner-sdk\lib"
Symbols Missing:
FFCookie.obj : error LNK2019: unresolved external symbol "public: void
__thiscall nsCOMPtr_base::assign_from_gs_contractid_with_er ror(class nsGetServiceByContractIDWithError const &,struct nsID const &)"
(?assign_from_gs_contractid_with_error#nsCOMPtr_base##QA
EXABVnsGetServiceByContractIDWithError##ABUnsID###Z) referenced in
function "public: __thiscall nsCOMPtr::
nsCOMPtr(class
nsGetServiceByContractIDWithError const &)"
(??0?$nsCOMPtr#VnsICookieManager####QAE#ABVnsGe
tServiceByContractIDWithError###Z)
FFCookie.obj : error LNK2019: unresolved external symbol "public: void
__thiscall nsCOMPtr_base::assign_from_qi(class nsQueryInter face,struct nsID const &)"
(?assign_from_qi#nsCOMPtr_base##QAEXVnsQueryInterface##ABUnsID###Z)
referenced in function "public: __t hiscall nsCOMPtr::nsCOMPtr(class
nsQueryInterface)" (??0?$nsCOMPtr#VnsICookieMan
ager2####QAE#VnsQueryInterface###Z) FFCookie.exe : fatal error
LNK1120: 2 unresolved externals
#include "nsICookieManager.h"
#include "nsICookieManager2.h"
#include "nsServiceManagerUtils.h"
#include "nsComPtr.h"
#include "nsNetCID.h"
#include "nsStringAPI.h"
#include "mozilla-config.h"
int main()
{
nsresult rv;
nsCOMPtr<nsICookieManager> cookieManager = do_GetService (NS_COOKIEMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
if (cookieManager)
{
nsCOMPtr<nsICookieManager2> cookieManager2 = do_QueryInterface(cookieManager);
if (cookieManager2)
{
cookieManager2->Add(NS_LITERAL_CSTRING("ud.abc.com"),
NS_LITERAL_CSTRING("//"),
NS_LITERAL_CSTRING("TK"),
NS_LITERAL_CSTRING("abc"), 0x1, 0x1, 0, -1);
}
}
return 0;
}
Questions:
I dont find any info with function documentation regarding which LIB to include (as I find on MSDN)
Any clue on how to figure out LIB corresponding to particular function for MOZILLA.
The problem isn't with the lib, the symbol missing is defined in the xpcomglue library. However, you seem to have some compile parameters that don't match the parameters used to compile XULRunner/Firefox. The symbols your compiler is looking for contain "QAEX" as parameter description whereas the library defines them with "QAIX". Looking at the name mangling table, your compiler expects unsigned char where Mozilla has unsigned int. I suspect that the reason is you compiling your application without Unicode support - change main() into wmain()?

How do you compile static Taglib libraries for Windows?

I don't want to compile dynamic libs, so this question was not useful.
I downloaded taglib and compiled it using:
cmake -DENABLE_STATIC=ON -DENABLE_STATIC_RUNTIME=ON -DWITH_MP4=ON -G "Visual Studio 10"
That generates the Visual Studio solutions and I can compile the "tag" project which produces tag.lib in taglib/Release.
The problem comes when I try to use the library in a test application - nothing much, just a simple test:
#include "stdafx.h"
#include "fileref.h"
int _tmain(int argc, _TCHAR* argv[])
{
TagLib::FileRef d("");
return 0;
}
I get the following Linker errors:
Error 1 error LNK2001: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall TagLib::FileRef::~FileRef(void)" (__imp_??1FileRef#TagLib##UAE#XZ) C:\...\taglib_test\taglib_test\taglib_test.obj taglib_test
Error 2 error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall TagLib::FileRef::FileRef(class TagLib::FileName,bool,enum TagLib::AudioProperties::ReadStyle)" (__imp_??0FileRef#TagLib##QAE#VFileName#1#_NW4ReadStyle#AudioProperties#1##Z) C:\...\taglib_test\taglib_test\taglib_test.obj taglib_test
Error 4 error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall TagLib::FileName::FileName(char const *)" (__imp_??0FileName#TagLib##QAE#PBD#Z) C:\...\taglib_test\taglib_test\taglib_test.obj taglib_test
Error 3 error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall TagLib::FileName::~FileName(void)" (__imp_??1FileName#TagLib##QAE#XZ) C:\...\taglib_test\taglib_test\taglib_test.obj taglib_test
Error 5 error LNK1120: 4 unresolved externals C:\...\taglib_test\Release\taglib_test.exe taglib_test
Can somebody please give me an idea as to what's going on here?
The following are the preprocessor defines in the tag project:
WIN32
_WINDOWS
NDEBUG
HAVE_CONFIG_H
_CRT_SECURE_NO_DEPRECATE
_CRT_NONSTDC_NO_DEPRECATE
TAGLIB_STATIC
CMAKE_INTDIR="Release"
For those who have had this problem:
I fixed it by defining TAGLIB_STATIC in the test project:
#include "stdafx.h"
//This should have been generated by the build system in taglib_config.h
//but was not.
#define TAGLIB_STATIC
#include "fileref.h"
int _tmain(int argc, _TCHAR* argv[])
{
TagLib::FileRef d("");
return 0;
}

Resources