Qt linker error LNK2019 on qInitResources() - RCC - c++

I want to compile a project with Qt in Visual Studio 2010.
I have built all the prerequisite libraries and linked them in project properties.
I have also made the .cpp file from the project.qrc file (rcc) with the command below:
rcc project.qrc -name project -o qrc_project.cpp
Followed the instructions from http://www.qtcentre.org/archive/index.php/t-3425.html .
The project.coo file is produced with the following lines:
int QT_MANGLE_NAMESPACE(qInitResources_project)()
{
QT_PREPEND_NAMESPACE(qRegisterResourceData)
(0x01, qt_resource_struct, qt_resource_name, qt_resource_data);
return 1;
}
Q_CONSTRUCTOR_FUNCTION(QT_MANGLE_NAMESPACE(qInitResources_project))
I have also included the .cpp file in the project.
Although, I get the error below:
> Error 2611 error LNK2019: unresolved external symbol "int __cdecl
> qInitResources_project(void)" (?qInitResources_project##YAHXZ)
> referenced in function main D:\usr\Windows\main.obj project
Have I done something wrong with the rcc? Could anyone please help?

I solved my problem by producing a .rcc file and a .cpp file, both with the name of the project and not with the "qrc_" at the beginning of it. I have also linked both files on my project.
It seems that the compiler could not find the proper file, this is why i had the linking error.
The commands I used in order to produce the files mentioned above are:
rcc -binary <path_to_qrc_file>.qrc -o <path_and_filename>.rcc
rcc <path_to_qrc_file>.qrc -name <project_name> -o <path_and_filename>.cpp

For someone using CMake:
you should enable AUTORCC to auto compile *.qrc files.
add_executable(project project.cpp project.qrc)
// enable autorcc and automoc
set_target_properties(project PROPERTIES AUTOMOC TRUE)
set_target_properties(project PROPERTIES AUTORCC TRUE)

I had found a really trivial workaround if nothing on SO works for you:
In VS, you can simply put your resource(images or icons) not in the library qrc file, but directly in the main project qrc file. Then, in library project, you can refer to the images using the path in main project, and there is even no need to call QT_INIT_RESOURCE.
I think this is not a good practice, but just note down for whoever tried all the solutions online and still not working.

If your project.qrc file is in the same project than the main and the main is not in a namespace, juste add Q_INIT_RESOURCE(project); in your main.
Elsewise define a function with just Q_INIT_RESOURCE(project); in it outside of any namespace in the library you have your project.qrc file.
Call that function in your main.

Related

OpenVDB linker error: unresolved external symbols

I wish to use OpenVDB library in a project. The library has compiled successfully on Windows7 x64 in release version using \MD (as described in https://github.com/rchoetzlein/win_openvdb).
The OpenVDB_cookbook compiles and executes successfully as well. If I put my own code in cookbook, it compiles and executes successfully as well.
But,
If I link the library to an existing project,
or I copy paste the same code (from cookbook) in a new project and use the same cmakelists.txt, Find*.cmake files and all the same options from project properties as from the cookbook,
it throws a linker error 'LNK2019: unresolved external symbols "void_cdecl openvdb::v3_0_0::initialize(void)" referenced in function main'
Even the simplest code of the form:
#include <openvdb.h>
void main()
{
openvdb::initialize();
}
throws this linker error.
Any ideas what am I doing wrong? I am on it for a few days and haven't been able to solve it.
All libraries IlmBase, OpenEXR, glew, glfw, boost and OpenVDB are successfully linked using cmake and are present in the project properties page.
Thank you for the help.
The problem was solved by adding dir\src. and dir\src.. to Additional Include Directories under properties->C/C++->General. Where dir is the directory where the source and build is. I don't know why I had to add this though.

Error when trying to compile using sqlite3_open in Visual Studio 2013

I'm working in a Cocos2dx (c++) win 32 project and trying to use sqlite to save the game data. My knowledge of c++ / Visual Studio is very limited right now.
This is part of the code that I'm trying to compile.
#include <sqlite3\include\sqlite3.h>
...
void HelloWorld::SaveAndLoadTest()
{
sqlite3 *pdb = NULL;
sqlite3_open("writablePath", &pdb);
...
}
But when I try to compile the line with the sqlite3_open command I get the following error:
Error 7 error LNK2019: unresolved external symbol _sqlite3_open referenced in function...
I've been trying to find an answer for this many hours. The most similar question I found was this one but I don't understand the answer.
Error: undefined reference to `sqlite3_open'
You need to link the sqlite3 library along with your program:
g++ main.cpp -lsqlite3
I'm new to Visual Studio and I don't understand how to solve this, anyone?
The error LNK2019 means that references are missing probably because a library is mising.
To add sqlite to a MSVC project, you have to make sure that:
the header is included in your source files
sqlite3.dll is in the path or in the directory of the executable
AND that sqlite3.lib is added to the additional dependencies in the VS project (options of the project > Linker > Input > Additional dependencies)
This last point is mandatory, because the lib tells the linker which functions are stored in the dll.
The solution, quite simply, is to link sqlite3 to your project. Libraries need to be linked (via the linker) for you to be able to use them. Head over here and download the pre-compiled binaries for your platform of choice (in this case, Win32). You may also choose to compile sqlite3 from source. You should end up with a .lib file. Go to Project -> Configuration Properties -> Linker -> General -> Additional Include Directories and add the path to your library file to it. Then go to Linker -> Input -> Additional Dependencies and put in sqlite3.lib.
And remember that you must build sqlite3.lib from file SQLite3.def:
Download source from source (https://www.sqlite.org/download.html)
For example: source https://www.sqlite.org/2022/sqlite-amalgamation-3390300.zip
Or download binary from binary
For example: binary https://www.sqlite.org/2022/sqlite-dll-win64-x64-3390300.zip
Extract both archives to the same directory
Open Developer Command Prompt for VS 2017 by typing Developer Command in Windows Search
Go to directory where you've extracted source code and binary files (via opened cmd)
Run lib /DEF:sqlite3.def /OUT:sqlite3.lib /MACHINE:x64
(Remember if win32, replace "MACHINE:x64" by MACHINE:x86)

Qt: LNK2001 and LNK2019 Errors arise after implementing a signal/slot across source files

This consists of the declarations in my MainWindow file, I haven't included the library inclusions for brevity. I have included QObject.h in both, and all required libraries. It was compiling fine before the inclusion of the second connect call. The first Ctrl-C handler works fine.
I took out my code, thought my employer wouldn't like it. See the appropriate response below!
I am using QT Creator. When I googled these error messages, most of the solutions provided revolved around fixing the make command/path or something of the sort which occurs when compiled in Visual Studio.
I reiterate, these errors showed up ONLY after the inclusion of that second connect call of the global GCamera and the MainWindow. The program was compiling and running before this.
Thank you in advance, I apologize for the lengthy post and look forward to receiving any input.
You should have a file named moc_tcamera.cpp that implements the missing symbol in the build directory.
If it isn't present, you should "run qmake", and "Rebuild" your project (both actions are in Qt Creator Build menu).
Why the error occured:
qmake adds a moc (Qt's meta object compiler) step to the Makefile for all the source files that contains the macros Q_OBJECT or Q_GADGET, such file is said to be "mocable". Once a file is detected as mocable or non mocable, that status doesn't change until qmake is rerun.
QtCreator only runs qmake by itself when the .pro file changes (for example, when you add or remove a file).
It means you probably compiled the project once without the Q_OBJECT macro in the file tcamera.h, and added that macro afterward. And because you didn't need the meta object until you added the call to connect, VC++ didn't try to resolve the missing symbols.
You can get linker errors when you, say, include the header in your .pro file, but not the source. For example in your .pro file:
HEADERS += t_camera.h foo.h
SOURCES += foo.cpp
would cause you to have linker errors, typically LNK2001 and LNK2019. However, IIRC QtCreator manages the .pro files for you, right? In other words, make sure your .pro file is correct. I'd be surprised if it wasn't, but errors in the .pro file can cause the issue you're seeing.

Linking Issues in a Multiproject Visual Studio 2005 solution

I'm working on a solution file (VS 2005) which contains multiple projects.
There are two projects of my concern right now - one called core and another called log.
The core project is the startup project and the log project has core ticked in its project dependencies.
I have two files in core project - AB.h(declarations) and AB.cpp(definitions)
Many other cpp files from the core project refer to AB.h and has no issues finding the definitions in AB.cpp.
Now comes the issue. I have a cpp file called CD.cpp in the log project which requires referring to AB.h. I have added the include directory of AB.h to both the core and log projects (AB.cpp also resides in same folder)
In addition the log project has $(OutDir) in its "Additional Library Directories" parameter in the Linker part of project settings. Also have added "core.lib" to the Additional Dependencies field in Properties->Linker->Input
The core project compiles into a dll but there is also a core.lib in the same folder amongst other junk. Solution compiled as the Release version.
In CD.cpp, when I right click the #include "AB.h" line, it takes me to .h file in the core folder. So far everything is fine. However when I try to compile the solution, the log project does not build properly, showing these two linker errors.
log.obj : error LNK2001: unresolved external symbol "class myNamespace::myClass myInstance" (?myInstance##3VmyClass#myNamespace##A)
log.obj : error LNK2001: unresolved external symbol "bool infFlag" (?infFlag##3_NA)
The class is part of an h file included in AB.h (instance declared in AB.cpp and included as extern in CD.cpp. infFlag is declared in AB.cpp and declared in CD.cpp as extern.
What do I do to let log build properly? Thanks
OK. You guys need more details. Sure.
One Solution file. Multiple Projects.
[Core]
includes AB.h and AB.cpp
-AB.h
--includes EF.h from external lib (functions from this lib is working properly for files within the same core project).
--declares some functions
-AB.cpp
--defines those functions which was declared in AB.h
-some other cpp files which can use those functions from AB.h without issues
[/Core]
[Log -> Project Dependencies -> Core]
-No h files
-CD.cpp
--requires AB.h to work. Is included with a #include command and its linked properly (I can right click and click "open file" to open the real .h file)
--uses functions from AB.h
[/Log]
Hopefully this will better let you guys understand the project structure in my solution.
Issue comes when compiling Log project. It always comes up with a link error for any references to functions in AB.h saying "unresolved external symbol". I guess this comes because it cannot find the definitions for the functions declared in AB.h (probably because it is a different project). Since log is dependent on core, and core will have compiled as of then, I added in $(OutputDir) to Library directories (both core and log build into the same directory) so that it can find core.lib and also added "core.lib" to "Additional Dependencies" under Input tab under Linker (in project properties).
So there you go. Any ideas?
In addition the log project has $(OutDir) in its "Additional Library Directories" parameter in the Linker part of project settings.
Probably wrong. It should be that dir where "core.lib" resides, the $(OutDir) is where the log will be put.
The class is part of an h file included in AB.h (instance declared in AB.cpp and included as extern in CD.cpp. infFlag is declared in AB.cpp and declared in CD.cpp as extern.
So where are definitions?

Troubles at Including (Linking) a static library inside another one

I'll try to explain shortly what I want to do:
A project using a static library which have another one as depandency.
It produce a project called MyProject linking on MyLib1 linking on MyLib2.
Here is the compile order:
MyLib2
MyLib1 (linking to MyLib2)
MyProject (linking to MyLib1)
I'm using Visual Studio 2008 and I have some troubles at defining include.
When linking, I use the property "Additional Include Directory" (on project property C/C++ node).
This seems working between MyProject and MyLib1 but not MyLib1 and MyLib2.
For Exemple: I've a file in MyLib2 called foo.cpp; Using #include "foo.cpp" makes visual studio telling that foo.cpp is unknow (missing file or folder).
To ensure it's NOT a wrong path I gave, I've done many attemps like following: copy-paste the path shown in Command Line (used to compile the library) into win explorer: I well see the source code of my second library. I've remake the project many times and each times I used differents names (forcing me to pay attention to this) and everything seems well defined (but not "including").
The only way I actually find to make it works: using #include "c:\\foo.cpp" as include... Very nice for portability !
Here is a Zip of the Solution to test it yourself and tell me what's wrong: MyProject.rar
Thanks for taking some time to help me !
Lucyberad
First, never include *.cpp files.
Second, use forward declaration of your external functions:
void appellib2(void);
void appellib1(void)
{
appellib2();
}
Third, right-click each project in the Solution Explorer, and select "Project dependencies..." and set-up proper dependencies: MyProject -> MyLib1 -> MyLib2.
At last, in properties for MyProject, set up additional dependencies MyLib1.lib.
Now I can build and run your project without errors.
UPDATE
Never rename *.cpp to *.h just to solve linking problems. If you have a definition in your *.h file you will unable to include it twice or more.
Here is your project YourProject.rar.
I've changed .cpp to .h. I Know I've never to include a CPP file but it was for testing purpose.
I've added prototype of each function but it doesn't works better (It don't include the file mylib2.cpp, the prototype is better but not usefull in this test).
I've set up project dependencies. It still fail.
I've set up additional dependencies MyLib1.lib to MyProject (I think this was the next error I would be able to find, producing a linking a error).
With theses modifications, I still got include error mylib2.h: no such file or directory.
Here is the new archive: MyProject.rar
If you reach to get it working, can you make me an archive of the working solution?
Thanks,
Lucyberad.