Accessing a function from another file (C++, QT) Unresolved external - c++

I've been beating my head against this for too long now, and have researched everything I cannot find what's wrong.
I'm writing a GUI program in QT and have a few functions in some files external to the main program file, but within the project. The structure is something like this:
Source files:
main.cpp
mainwindow.cpp
signal.cpp
signalparse.cpp
header files:
mainwindow.h
signal.h
signalparse.h
Signal is an object that stores some data. Signalparse uses signal, parses a text file and reads it into signal. I tested this functionality on a barebones console application before putting it into my GUI program so I know it works(worked).
Now I find myself trying to call a function ReturnSignal from my SignalParse.cpp file inside my mainwindow.cpp file.
SignalParse.h is below:
#include<vector>
#include <iostream>
#include "signal.h"
void ParseLine(std::string*);
std::string ReturnTitle();
std::vector<Signal> ReturnSignal(std::string);
Showing I have declared ReturnSignal.
Inside SignalParse.cpp the function is defined as:
vector<Signal> ReturnSignal(string filename)
{
//does stuff
}
Inside mainwindow.cpp I declare an
std::vector<Signal> signalList;
to store the returned vector from ReturnSignal.
I then use
signalList = ReturnSignal(signalFilePath);
inside mainwindow.cpp to finally call this function and return my data. When commented out, there are no errors. However, this line causes the error:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "class std::vector<class Signal,class std::allocator<class Signal> > __cdecl ReturnSignal(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?ReturnSignal##YA?AV?$vector#VSignal##V?$allocator#VSignal###std###std##V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##2##Z) referenced in function "private: void __cdecl MainWindow::on_file_open_triggered(void)" (?on_file_open_triggered#MainWindow##AEAAXXZ)
I cannot for the life of me figure out why this wont work. Does anyone have any ideas? I've been looking for hours now!
I would love to re-write the question to be more generally applicable if i find a solution, but at the moment I'm completely stumped.
Thanks a lot.
EDIT: Compiler being used is Microsoft Visual C++ Compiler 12.0

After some serious headaches I found a very simple solution. Having only really used either command line compilers or Visual Studio, this did not occur to me.
It turns out that in order to fix most of the unresolved external errors I was dealing with, I had to simply go into the build menu, clean my project and then "Run qmake". After this, all my unresolved externals disappeared and my program worked.
I don't know why it was so hard for me to find this information, hopefully it will be of help to someone in a similar spot.

Related

c++ unresolved external symbols: cannot find the function where the symbol is referenced

I am writing c++ codes. When compiling (win7 system, compile in cmd with build.bat) I get error like this:
handler.lib(Media.obj):error LNK2019: unresolved external symbol _lua_pcallk referenced in
function "public:void __thiscall Media::loadScript(struct luabridge::lua_State *,class
std::basic_string<char, struct std::char_traits<char>, class std::allocator<char> > const &)".....
Therefore, I tried to locate the function
public:void __thiscall Media::loadScript(....)
to see what is going on, but I cannot find this function in my workplace...I have following ideas about why I cannot find this function and why this error occurs (Just to make things clearer, the whole system contains 11730 files which are quite complicated):
The thing following "public:void __thiscall" is a variable or something similar to a variable and somewhere in the codes this variable has been changed. So that's why I cannot find the exact function.
There are many codes containing main functions and I need to register some necessary lua environment in the main function. Probably I chose a wrong entry main function?
These are just my thoughts, are they reasonable and any other ideas? Oh and, in this work we use LuaBridge to do C++ Lua interaction, anyone who did similar work before encountered the same problem?

SQLite3 functions in DLL file cause unresolved external

I am creating a DLL in Visual Studio 2008 which requires use of SQLite3. I have included the file sqlite3.h as found here. Right now my files only have one function, which simply takes one parameter, the database name, and opens it. My files are as follows:
DBOPEN.H
#include <string>
using namespace std;
__declspec(dllexport) bool OpenDatabase(std::string);
DBOPEN.CPP
#include "dbopen.h"
#include "sqlite3.h"
bool OpenDatabase(std::string name)
{
sqlite3 *database;
int isOpen = sqlite3_open(name.c_str(), &database);
}
It's worth noting I've never written DLLs before, and I was basing the formatting on what I've found at various sources online. When I try to build this, however, I get this error:
1>dbopen.obj : error LNK2019: unresolved external symbol _sqlite3_open referenced in function "bool __cdecl OpenDatabase(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?OpenDatabase##YA_NV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
1>C:\Users\me\Documents\Visual Studio 2008\Projects\dbopen\Debug\dbopen.dll : fatal error LNK1120: 1 unresolved externals
I'm honestly not really sure how to fix this. I've tried switching various things, changing function definitions, etc., but the sqlite3_open keeps causing errors. I noticed if I try to use other sqlite3 functions, they all give the same error, however the sqlite3 object definition works. Does anyone know how this can be fixed? Thanks in advance for the help!

Source code of CvCaptureFromFile(..)

I want to use the source code of the CvCaptureFromFile(..) function as I do not want to use OpenCV library functions in my project. So I went inside the function by pressing F10,F11 and I copied the things I needed and converted them into C code. Till now everything was ok.
But when I tried to run I am getting linking error inside the function icvIntFFMPEG(void)
the errors are as follows:
Error 1 error LNK2019: unresolved external symbol
"__declspec(dllimport) void * __stdcall GetProcAddress(struct
HINSTANCE__ *,char const *)"
(__imp_?GetProcAddress##YGPAXPAUHINSTANCE__##PBD#Z) referenced in
function "void __cdecl icvInitFFMPEG(void)" (?icvInitFFMPEG##YAXXZ)
Error 2 error LNK2019: unresolved external symbol "struct HINSTANCE__
* __stdcall LoadLibrary(char const *)" (?LoadLibrary##YGPAUHINSTANCE__##PBD#Z) referenced in function "void
__cdecl icvInitFFMPEG(void)" (?icvInitFFMPEG##YAXXZ)
I copied everything as it is in the source code. Any idea where I am going wrong? How do I solve this error?
UPDATE: IF I wrap the header file (where I was coping the source code) inside an extern "C" something like this-
extern "C" {
#include "defination.h"
}
Then the first error is gone, and the error with loadLibrary changes to
error LNK2019: unresolved external symbol __imp__LoadLibrary#4 referenced in function _icvInitFFMPEG
OpenCv uses ffmpeg - which is a really big project. And ffmpeg uses some other tools behind curtains. You may want to go using ffmpeg, but to take the source code from ffmpeg itself is foolish - you will spend years fiddling with that code - really.
So, decide on a video input library - be it OpenCV, ffmpeg, Direct3D or whatever, and use it.
Do not cling yourself on the idea that 'you cannot use external libraries' because you can't go without them. It takes years to develop such a library - I am pretty sure you do not want to go that way.
Well, it seems your get unresolved calls to LoadLibrary()/GetProcAddress() functions that are inside Kernel32.dll. What IDE/project type are you using? Kernel32.lib should be linked in by default unless /NODEFAULTLIB is specified somewhere, at least as far as I remember. You can try to add #pragma comment(lib, "kernel32.lib") to the file where you copied the code, but without more info about your setup it is hard to answer what is causing this.

C++ linkage error

I compile in Visual studio 2008 and get this error. I have researched linkage error but am still uncertain to what it is. This is the finished code to a poker game so I would rather not post the code. Can someone translate this error message for me?
error LNK2019: unresolved external symbol "void __cdecl betFold(double)" (?betFold##YAXN#Z) referenced in function "void __cdecl flopAction(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?flopAction##YAXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) OH-DLL.obj
Your function void flopAction(std::string arg) uses a function betFold(double) that gets referenced and declared in some header, but is not implemented so that the linker is able to find it.
It means that you have declared this method but not defined it. Or at least the linker cannot find the definition, either because it’s in a library that you didn’t reference or else because it’s in an object file (source file) that is not part of your build process.
Sound like you forgot to specify the *.lib file that belongs to the *.dll. You can edit the list under your Project Property Pages -> Configuration properties -> Linker -> Input, remember to do this for the Debug and Release configuration.
And please try to refrain from phrases like wtf etc :)
Also, you could check your signature(function declaration), so that it contains only the type in it's parameter lists, while inside the definition(.cpp file), it contains both the type and parameter names. For eg,
in the .h file where the declaration sits:
void myfunc(int, char*);
and in the .cpp file where the definition sits:
void myfunc(int num, char* name)
{
//
}
I learnt this before in my college, but don't sure if Dev C++ supports it, left this things for a long time ago, just using Borland at that time.
hope this helps.
thanks.

MSVS2010 linker error sadness - not entirely sure what is wrong

I am using a library of code from a tutorial for providing functionality for passing function points of non-static member functions to a function that expects a static function pointer, probably helps to know what I am suing, so here is the link http://www.codeproject.com/KB/cpp/thunk32.aspx This code uses the Boost library, which I have downloaded and set-up more or less everything from.
In the Thunk library, one of the header files has a section that reads
#define BOOST_PP_ITERATION_PARAMS_1 (3,(0,THUNK32_MAX_ARGS,"Thunk32_template.h"))
??=include BOOST_PP_ITERATE()
#undef BOOST_PP_ITERATION_PARAMS_1
but this gives me epic amounts of errors, which I can solve by changing it to
#define BOOST_PP_ITERATION_PARAMS_1 (3,(0,THUNK32_MAX_ARGS,"Thunk32_template.h"))
#include BOOST_PP_ITERATE()
#undef BOOST_PP_ITERATION_PARAMS_1
This code that is downloaded is included in my solution as a second project, which is able to compile and build happily. But my project that is using this code has issues linking, to save people asking, I get these error messages
1>WebCamera.obj : error LNK2019: unresolved external symbol "protected: __thiscall indev::Thunk32Base::Thunk32Base(void)" (??0Thunk32Base#indev##IAE#XZ) referenced in function "public: __thiscall indev::Thunk32<class WebCamera,void __cdecl(struct HWND__ *,struct videohdr_tag *)>::Thunk32<class WebCamera,void __cdecl(struct HWND__ *,struct videohdr_tag *)>(void)" (??0?$Thunk32#VWebCamera##$$A6AXPAUHWND__##PAUvideohdr_tag###Z#indev##QAE#XZ)
1>WebCamera.obj : error LNK2019: unresolved external symbol "protected: __thiscall indev::Thunk32Base::~Thunk32Base(void)" (??1Thunk32Base#indev##IAE#XZ) referenced in function "public: __thiscall indev::Thunk32<class WebCamera,void __cdecl(struct HWND__ *,struct videohdr_tag *)>::~Thunk32<class WebCamera,void __cdecl(struct HWND__ *,struct videohdr_tag *)>(void)" (??1?$Thunk32#VWebCamera##$$A6AXPAUHWND__##PAUvideohdr_tag###Z#indev##QAE#XZ)
1>WebCamera.obj : error LNK2019: unresolved external symbol _capCreateCaptureWindowA#32 referenced in function "public: bool __thiscall WebCamera::Init(struct HWND__ *)" (?Init#WebCamera##QAE_NPAUHWND__###Z)
I think this is trying to say that the constructor and destructor are not declared and that my WebCamera.Init()is messed up as well. I have made sure that the library that the Thunk32 project exports is included in my other project, but still I get these errors.
I would like to know if I have made the correct assumption that ??=include should have been changed to #include and if I have, what have I done wrong or failed to do that results in these linker errors. Or if you can provide me with a different way of being able to pass a function pointer to a non-static member function that would be awesome.
Thanks
??= is a "trigraph" sequence for the # character. according to the standard, trigraphs are supposed to be handled as one of the first steps in processing (in phase 1 - before the preprocessor handles directives),so:
??=include "whatever"
Should be equivalent to:
#include "whatever"
so you should be able to use that form (I wonder why the trigraph was put there in the first place - some sort of evil joke perhaps?)
However, trigraphs cause problems and confusion (probably more than they help), so compilers seem to be moving towards warning about them and/or defaulting to not handling them. The compiler in VS 2010 has trigraph processing turned off by default - you have to use the /Zc:trigraphs option to turn it on.
See Purpose of Trigraph sequences in C++? for more details.
Ah, Einar, good man. Doing flash and Sharepoint stuff these days, ouch. Norwegian, might explain the trigraphs.
Anyhoo, nothing complicated, you just forgot to tell the linker to look at some libraries. Right-click your project, Project Dependencies, tick the Thunk project. That makes sure that Thunk32.lib gets looked at and resolves the ctor and dtor.
Right-click again, Properties, Linker, Additional dependencies, add "winmm.lib". That resolves the capCreateCaptureWindow symbol.
Do you have a constructor and a destructor declared in the indev::Thunk32Base class that you forgot to define in its cpp file?
Ok, so I have managed to solve this now.
Michael Burr nicley said that ??= is basically the same as typing # but in a way that people who dont have the hash symbol can type it, see Purpose of Trigraph sequences in C++?
Hans Passant then got the ball rolling for me buy letting me know that I had not fully linked in stuff. I needed to right click on my main project, select 'Project Dependencies' and select my other project that has the thunk32 code. I also needed to tell my main project to look at where the Thunk project is saving the lib, which turned out to be in a folder in my documents (explain that one!). I also needed to add the Thunk32d.lib (note the 'd' because I was/am in debug mode. Hans said that I needed winmm.lib but it turned out (when googling the function that was giving me the error that I needed Vfw32.lib instead.
Thanks guys! I hope that by giving the full answer like this it can help some one else who has a similar problem.