Using RegOpenKey and RegEnumKey in dynamic QT with MSVC2010 - c++

I want to use 2 winapi functions for registry access, as stated.
I have QT 5.3.2 with QT Creator 3.2.0, MSVC2010 build, Windows SDK v7.1 and debugging tools installed.
windows.h is included in my project and both functions are seen, but linker fails with
usb_part.obj:-1: error: LNK2019: unresolved external symbol __imp__RegEnumKeyW#16 referenced in function "bool __cdecl GetUsbName(class QString &)" (?GetUsbName##YA_NAAVQString###Z)
usb_part.obj:-1: error: LNK2019: unresolved external symbol __imp__RegOpenKeyW#12 referenced in function "bool __cdecl GetUsbName(class QString &)" (?GetUsbName##YA_NAAVQString###Z)
I suppose it tries to link those functions dynamically (hense the "__imp"), how can I set it to link against a static .lib file? Or, if that is not possible with dynamically built QT, how do I link those to .dll?
UPD:
Tried these:
LIBS += -ladvapi32
LIBS += AdvAPI32.Lib
LIBS += "c:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\AdvAPI32.Lib"
to no avail
UPD2:
Problem was in QT not recreating makefiles after changes to .pro. After I deleted makefiles, LIBS += -ladvapi32 worked fine. Thought that "Clean project" does that, but it turns out that it doesn't.

You should add to your .pro file
LIBS += -lAdvapi32
or
LIBS += Advapi32.lib
After that run qmake and build again.
[Update] It may be needed to remove generated makefiles manually and recreated them with qmake run again.

Related

sf::Sound functions cause linker errors

I have started to use SFML recently. So I downloaded the 2.4.2 Win64 version of the library and started to play around the examples.
The only problem I have encountered so far is a linker error when I call play/pause/stop on a sf::Sound object. For example for play I get:
main.obj:-1: error: LNK2019: unresolved external symbol
"__declspec(dllimport) public: virtual void __cdecl
sf::Sound::play(void)" (__imp_?stop#Sound#sf##UEAAXXZ) referenced in
function main
Note that I am linking against sfml-audio.lib and can use other objects of the audio lib like AudioBuffers or even other functions of the sf::Sound like setBuffer which is wierd to me.
I am using Qt Creator as IDE so I link in the .pro file like this:
LIBS += -LC:/SFML/lib
CONFIG(release, debug|release): LIBS += -lsfml-audio -lsfml-graphics -lsfml-main -lsfml-network -lsfml-window -lsfml-system
CONFIG(debug, debug|release): LIBS += -lsfml-audio-d -lsfml-graphics-d -lsfml-main-d -lsfml-network-d -lsfml-window-d -lsfml-system-d
INCLUDEPATH += C:/SFML/include
DEPENDPATH += C:/SFML/include
Does anyone have a clue on whats happening?
Thanks in advance.
Well, finally I solved it just downloading the SFML Master and building the library with CMake + Visual Studio.
The libs/dlls generated that way work flawlessly.

Qt 5.3.1 - static building results in linker errors

I just built the static libraries from Qt 5.3.1 for Windows x86 with MSVC 2013. The second step is obviously a static linked application that uses these libraries. So I created a small application (just a MainWindow with some controls, for testing purposes). This is my *.pro-file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = projectname
TEMPLATE = app
CONFIG += static
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
LIBS += -LC:\statics\lib\ -lQt5Core \
-LC:\statics\lib\ -lQt5Gui \
-LC:\statics\lib\ -lQt5Widgets \
-LC:\statics\lib\ -lqtmain \
-LC:\statics\lib\ -lQt5PlatformSupport \
INCLUDEPATH += C:\statics\include
Yeah. This is what I got from several tutorials/my memory from when I worked with 5.1. But when compiling I get those errors:
Qt5Core.lib(Qt5Core.dll):-1: error: LNK2005: "public: __thiscall QString::~QString(void)" (??1QString##QAE#XZ) allready defined in mainwindow.obj.
mainwindow.obj:-1: error: LNK2019: Unresolved extern symbol ""public: static struct QArrayData const * const QArrayData::shared_null" (?shared_null#QArrayData##2QBU1#B)" in Funktion ""public: __thiscall QString::QString(void)" (??0QString##QAE#XZ)".
moc_mainwindow.obj:-1: error: LNK2001: Unresolved extern symbol ""public: static struct QMetaObject const QMainWindow::staticMetaObject" (?staticMetaObject#QMainWindow##2UQMetaObject##B)".
Does somebody know how to handle these errors? Do I have to link against some other libraries not mentioned above, or include some files? I googled this topic and found out that many people experienced the same problem, but they mostly use older versions of Qt oder VS. I also tried to export the project to Visual Studio (working from Qt Creator), but the errors remain.
Thank you for your answers :)
QT += core gui
LIBS += -LC:\statics\lib\ -lQt5Core \
-LC:\statics\lib\ -lQt5Gui \
You are loading both Qt libraries twice: default and custom versions. By default, QT contains both core and gui libraries... you must delete them manually:
QT -= core gui
Just remove LIBS, INCLUDEPATH from project file and run qmake from static build on the project file to create makefile. If this will not help check your PATH

qt-creator and msvc can't add a (static) library

I have a 3rd party (closed source) static library (.lib) written in C, with a C++ compatible header and a very simple C++ test program.
After creating a new project in Qt-creator, I tried to compile the program and link to the static library, but the linker cannot resolve the symbols: "Unresolved external symbol", error LNK2019.
I am using Qt 5.3 with the compiler from VisualC++ 2013 Express (which I think is MSVC12), and the .pro file used in Qt-creator is the following:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
LIBS += -L"C:/Users/myuser/myproject" -lsomelib
SOURCES += main.cpp
HEADERS += \
somelib.h
assuming that in C:\Users\myuser\myproject there are the following two files:
libsomelib.lib
somelib.h
I am not sure about what to provide in the -l flag, so I tried with libsomelib.lib, libsomelib and somelib as I would do in Linux, but every combination was unsuccessful.
This is my first time using MSVC from Qt-creator (not that I have much experience with it), so I cannot really understand the linking command issued by qt-creator, which includes manifest files that I do not know, and - apparently - no files or libraries are listed explicitly in the command:
link /NOLOGO /DYNAMICBASE /NXCOMPAT /DEBUG /SUBSYSTEM:CONSOLE "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'" /MANIFEST /MANIFESTFILE:debug\myproject.exe.embed.manifest /OUT:debug\myproject.exe #C:\Users\myuser\AppData\Local\Temp\myproject.exe.4260.531.jom
Note that if I do not use qmake and compile this example by hand using cl and link, the program links just fine. To achieve this, I use the commands:
cl /MT /EHsc /c main.cpp
link /LIBPATH:C:\Users\myuser\myproject libsomelib.lib main.obj /out:test.exe
How can I fix that? Thanks in advance!
Somehow I managed to compile the code using MSVC.
I tried to reproduce the compilation on Qt-Creator but without success; also tried to export a qmake project from VS using the Qt Add-in, but the issue persist in qt creator. So I'm giving up and continue to use VS to compile.

Qt: windows functions are unresolved external symbols

I'm trying to compile a simple helloworld-like non-Qt C++ application using te WinAPI in QtCreator. Here's the code:
#include <windows.h>
int main()
{
HWND cons = GetConsoleWindow();
SetWindowText(cons, L"I am the console window");
MessageBox(cons, L"Hello world!", L"I am the MessageBox", MB_OK | MB_ICONERROR);
return 0;
}
Looks very simple, but when I've tried to build it, the compilation fails with:
main.obj:-1: error: LNK2019: unresolved external symbol __imp__MessageBoxW#16 referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol __imp__SetWindowTextW#8 referenced in function _main
I started to seek, and I found this, but it wasn't helping me at all, because when I had written this:
LIBS += -L"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib"
and even this:
LIBS += -L"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib\\shell32.lib"
in my .pro, these "symbols" still stand unresolved. I ran qmake after each change to the .pro-file contents. So, any ideas?
-L sets the search paths for DLLs, but it doesn't actually link anything. The actual linking is done via -l. Setting the search path for system libraries shouldn't be necessary, but you'll need to link against user32:
win32:LIBS += -luser32
Additional to Frank's answer (which helped me very much, thanks for that!) I would like to add that this is only required vor MSVC, MinGW doesn't seem to need that line. Which was for me the most confusing part, I first thought I had problems with the msvc toolchain.
My inclusion now looks like this to reflect this fact:
msvc: LIBS += -luser32

How to link against a static library which uses static libraries itself using scons and Visual C++?

My SConstruct file looks something like this:
jnglLibs = Split("freetype png ogg vorbisfile") # left out a few
env.Library(target="jngl", source=source_files, LIBS=jnglLibs)
env.Program("test.cpp", LIBS=Split("jngl") + jnglLibs)
The static library links fine but the program fails with unresolved external symbols from the libraries in jnglLibs.
lib /nologo /OUT:jngl.lib finally.obj freetype.obj main.obj opengl.obj sprite.ob
j tess.obj texture.obj window.obj windowptr.obj callbacks.obj ConvertUTF.obj aud
io.obj win32\message.obj win32\time.obj win32\window.obj
cl /Fotest.obj /c test.cpp /TP /nologo /EHsc /MD -O2 -DNDEBUG /I.
test.cpp
link /OUT:test.exe /LIBPATH:. /LIBPATH:lib jngl.lib freetype.lib png.lib opengl3
2.lib glu32.lib user32.lib shell32.lib gdi32.lib z.lib jpeg.lib dl.lib openal32.
lib alut.lib ogg.lib vorbisfile.lib test.obj
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
jngl.lib(freetype.obj) : error LNK2019: unresolved external symbol FT_Done_FreeT
ype referenced in function "public: __cdecl jngl::Font::~Font(void)" (??1Font#jn
gl##QEAA#XZ)
... and so on
Why doesn't it find the symbol FT_Done_FreeType which is in freetype.lib? It works perfectly with GCC.
There are a lot of quirks with static linking on Windows.. I've had one or two similar problems to this but mostly my problem was once the 'static' libs were compiled they still depended on the .dll's being there. Anyway, I've been feeling your pain for a while now but got through it for my purposes... here's one page that really helped me.
http://xmlsoft.org/XSLT/tutorial2/libxslt_pipes.html#windows-build
It doesn't have to do with those particular libraries you listed but it might give you some clues. One thing is figuring out which C-runtime library each of those .libs (or '_a.libs,' which you might want to look into) were compiled against and make sure they are all the same when you statically link. So since you're using /MD, make sure all those libs were also compiled with /MD and not /MT. That COULD be a reason it isn't finding that freetype symbol, but it's just one guess. Good luck!