The following code is taken from here. I removed all Windows NT part as I am working on Windows 7.
I copied this code and run in visual studio 2010 (New project-> VC++->CLR->CLR Console... ). But it is giving lots of unresolved extern 'c' errors as listed below the code. What wrong I have committed?
#define STRICT 1
#include <windows.h>
#include <iostream>
using namespace std;
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
DWORD dwThreadId, dwProcessId;
HINSTANCE hInstance;
char String[255];
HANDLE hProcess;
if (!hWnd)
return TRUE; // Not a window
if (!::IsWindowVisible(hWnd))
return TRUE; // Not visible
if (!SendMessage(hWnd, WM_GETTEXT, sizeof(String), (LPARAM)String))
return TRUE; // No window title
hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
dwThreadId = GetWindowThreadProcessId(hWnd, &dwProcessId);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
cout << hWnd << ' ' << dwProcessId << '\t' << String << '\t';
cout << "(None)\n";
CloseHandle(hProcess);
return TRUE;
}
int main(int argc, char *argv[], char *envp[]) {
EnumWindows(EnumWindowsProc, NULL);
return 0;
}
This is giving following Errors(and other similar unresolved extern C errors)
1>wndowfind.obj : error LNK2028: unresolved token (0A000342) "extern "C" int __stdcall
EnumWindows(int (__stdcall*)(struct HWND__ *,long),long)" 3
(?EnumWindows##$$J18YGHP6GHPAUHWND__##J#ZJ#Z) referenced in function "int __cdecl
main(int,char * * const,char * * const)" (?main##$$HYAHHQAPAD0#Z)
1>wndowfind.obj : error LNK2028: unresolved token (0A000346) "extern "C" unsigned long
__stdcall GetWindowThreadProcessId(struct HWND__ *,unsigned long *)"
(?GetWindowThreadProcessId##$$J18YGKPAUHWND__##PAK#Z) referenced in function "int __stdcall
EnumWindowsProc(struct HWND__ *,long)" (?EnumWindowsProc##$$FYGHPAUHWND__##J#Z)
1>wndowfind.obj : error LNK2028: unresolved token (0A000347) "extern "C" long __stdcall
GetWindowLongW(struct HWND__ *,int)" (?GetWindowLongW##$$J18YGJPAUHWND__##H#Z) referenced in
function "int __stdcall EnumWindowsProc(struct HWND__ *,long)"
(?EnumWindowsProc##$$FYGHPAUHWND__##J#Z)
1>wndowfind.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall
EnumWindows(int (__stdcall*)(struct HWND__ *,long),long)"
(?EnumWindows##$$J18YGHP6GHPAUHWND__##J#ZJ#Z) referenced in function "int __cdecl
main(int,char * * const,char * * const)" (?main##$$HYAHHQAPAD0#Z)
1>c:\users\afnan\documents\visual studio 2010\Projects\wndowfind\Debug\wndowfind.exe : fatal
error LNK1120: 10 unresolved externals
1>
1>Build FAILED.
UPDATED
By including the libraries (as suggested in the answers), I was able to run the program successfully. But I am not able to understand why only first character of string is printing not the complete one, as can be seen in the output:
00010060 2652 S (None)
002502B2 5820 C (None)
00090402 5160 w (None)
00050392 5160 w (None)
00060292 3520 F (None)
000C02BA 3520 M (None)
0001021A 3736 E (None)
00040018 896 I (None)
00010170 3580 A (None)
0002003E 2684 D (None)
00030316 4956 N (None)
000202DE 3736 D (None)
0001031E 2652 S (None)
000100EA 2652 P (None)
In the output above, S is actually "start", C is "console" etc I confirmed through spy++ tool.
How can I print the complete string instead of just first character?
CLR projects by default do not include the standard Windows libraries, such as user32.lib.
Edit your project properties, find the Linker Inputs option, and add kernel32.lib user32.lib advapi32.lib which are the usual libraries needed by Win32 code.
Related
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.
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")
I statically link fw.lib in my C++/CLI project, and get these errors:
Error 1 error LNK2028: unresolved token (0A00001B) "extern "C" unsigned int __cdecl func(unsigned int,unsigned char *)" (?func##$$J0YAIIPAE#Z) referenced in function "public: static void __clrcall Sdk::Native::Method(void)" (?Method#Native#Sdk##$$FSMXXZ) C:\project\Sdk.obj Sdk
Error 2 error LNK2019: unresolved external symbol "extern "C" unsigned int __cdecl func(unsigned int,unsigned char *)" (?func##$$J0YAIIPAE#Z) referenced in function "public: static void __clrcall Sdk::Native::Method(void)" (?Method#Native#Sdk##$$FSMXXZ) C:\project\Sdk.obj Sdk
What do I do wrong ? I did this:
Added the lib path in [project properties -> Library Directories]
Added fw.lib file under [project properties -> Additional Dependencies]
Wrapped all functions in fw.h with extern "C" {...}
Any idea ?
Problem solved...
Apparently when you link, for instance, x64 bit into x32 project (and vice versa ??), the linker produces a very general link-error.
FYI :-)
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()?
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;
}