Cannot link to WinApi in Qt Console application [duplicate] - windows

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.

Related

LNK2019 while using SDL2 and vcpkg [duplicate]

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.

basic setup of C++/CLI code so that it can be consumed by unmanaged C/++

I am trying to learn C++/CLI, with the plan of writing a DLL which will be consumed by (unmanaged) C code. However, I cannot get the most basic example to build, as is reproducible below:
I am working in Visual Studio Express 2013.
Create new project -> CLR ->class library
LearnCli.h:
extern "C" __declspec(dllexport)
int __stdcall TestFunc();
LearnCli.cpp:
#include "stdafx.h"
#include "LearnCli.h"
int __stdcall TestFunc()
{
return 3;
}
Build with no problems.
Add Project -> Win32 ->Console Application
From the context menu in solution explorer for the new console project:
Add -> reference -> LearnCli
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include "..\LearnCli\LearnCli.h"
ConsoleApplication.cpp
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int z;
z=TestFunc();
cout << "Function returns:" << z << endl;
cin.get();
return 0;
}
intellisense has no problems, but on build:
Error 1 error LNK2019: unresolved external symbol _TestFunc#0 referenced in function _wmain [path]\Projects\LearnCli\ConsoleApplication1\ConsoleApplication1.obj ConsoleApplication1
What am I missing which is not allowing the win32 console app to find the function? Cheers.
Edit
Thanks to the comment and link, I have change the LearnCli.h file to
#ifdef LEARNCLIAPI_EXPORTS
#define LearnCliApi_DECLSPEC __declspec(dllexport)
#else
#define LearnCliApi_DECLSPEC __declspec(dllimport)
#endif
And gone to Project -> Properties -> C/C++ -> Preprocessor ->Definitions
and added LEARNCLIAPI_EXPORTS. unfortuately the error is unchanged
You need to link your application(exe) project with the .lib built from dll project.
You can add that from Project settings >> Linker >> Input files or simply put a line on your source.
i.e.
pragma(comment, "lib:<your_lib.lib>")

LNK 2019 ShellExecuteEx unresolved external symbol Qt Creator

I work on Windows7 x64 in c++ language.
I created a project that open firefox browser when I show a marker to my web cam, using ShellExecuteEx function. My project works well with visual studio 2010.
But, when I try to run my project with Qt Creator, I get this error:
main_cam.obj:-1: error: LNK2019: riferimento al simbolo esterno __imp__ShellExecuteExW#4 non risolto nella funzione _main
debug\cam.exe:-1: error: LNK1120: 1 esterni non risolti
The code is:
#include <iostream>
#include <stdio.h>
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <windows.h>
#include <ShellAPI.h>
include [...]
int main () {
[...]
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "firefox.exe";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
[...]
if (condition_is_verified) {
ShExecInfo.lpParameters = (LPCWSTR)"www.google.it";
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
}
[...]
}//end main
I think problem is shell32.lib . If it is, I haven't this library in my pc. how can I fix it?
Can you help me?
Thanks in advance!
It is very simple. Just right click the the project pro file and add external library. Choose system library, static and give the path of shell32.lib which is
C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib
This is my solution to this problem :
in the .pro file : INCLUDEPATH += "C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib"
( the path to the file shellAPI.lib )
in my mainwindow.h :
#define NOMINMAX // You have to do this for windows.h to work, else u'll get mistakes with datetime.h
#include <windows.h> // (without the space)
#pragma comment(lib, "Shell32.lib")
#include <ShellAPI.h> // without space also

mpic++ main(int, char**) - deprecated conversion from string constant to ‘char*’

during compiling older C++ code on Ubuntu 11.10 with mpic++ (Open MPI C++ wrapper compiler) I got this warning connected with int main(int argc, char **argv){...}:
main(int, char**) : warning: deprecated conversion from string constant to ‘char*’
I tried to edit the code to int main(int argc, const char *argv[]){...} but it did not help (contrary to e.g. this issue).
What did I wrong? How can I fix it?

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