c++ LNK2001 & LNK1120 compile errors [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I am debugging some C++ software, and want to modify an existing function slightly, so that it changes the value of a particular variable used elsewhere in the program.
The function is currently defined as so:
void DataStore::setEraseSelected(){
...
// Function code here
...
}
As it stands, the function works correctly with no problems whatsoever. But, I now want to modify the function, so that it will change the value of a variable used elsewhere in the program. To do this, I have tried passing the variable into the function as a parameter (I have also updated the header file to reflect the changes to the function), and then assigning a value to the variable inside the function:
void DataStore::setEraseSelected(toAMS::DataMessage statusMsg){
...
// Function code here
...
statusMsg.CODE_ERASE = Types::Activated;
...
}
As mentioned, I have added the declaration to the header file, so that it now has the declaration for the function with the parameter, as well as the one for the function without the parameter:
void DataStore::setEraseSelected(toAMS::DataMessage statusMsg);
But when I try and build the code (using Visual Studio 2010), I get the following two compile errors:
error LNK2001: unresolved external symbol "public void_thiscall DataStore::setEraseSelected(void)" (? setEraseSelected#DataStore::QAEXXZ)
error LNK1120: 1 unresolved externals
The first error highlights the project .obj file, which I have tried deleting and building again, but get the same error, and second one highlights the project .exe file, which I have also tried deleting and building again, but get the same error.
Anyone have any ideas why? I've had a look on SO for questions regarding these errors, but none of them seem to clearly explain why I might be getting them. They all seem to suggest that the compiler is possibly looking in the wrong place, but if I undo my changes, then the code compiles with no problems, and I haven't told the compiler to look anywhere else when building the code with my changes...

void DataStore::setEraseSelected(int );
Pass that variable

Related

Use the glDrawBuffer and glReadBuffer functions in Qt

I would like to use the glDrawBuffer and glReadBuffer functions in Qt using Qt Creator. I tried to do this:
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
This code would not compile, it gave me linking errors like this one:
main.obj:-1: error: LNK2019: unresolved external symbol __imp__glDrawBuffer#4 referenced in function "public: void __thiscall Simple3DWidget::OpenGLWindow::render(void)" (?render#OpenGLWindow#Simple3DWidget##QAEXXZ)
This error basically means that the functions were declared in an included header file but not correctly linked.
After searching for this issue on the web, I found out that I have to use the QOpenGLFunctions_3_1 class. I tried both these codes:
QOpenGLFunctions_3_1 m_openGL31Functions;
m_openGL31Functions.glDrawBuffer(GL_NONE);
m_openGL31Functions.glReadBuffer(GL_NONE);
and:
QOpenGLFunctions_3_1().glDrawBuffer(GL_NONE);
QOpenGLFunctions_3_1().glReadBuffer(GL_NONE);
Both these codes compile correctly, but when I run the program, in both cases, the program crashed and gave me the following error:
Access violation reading location 0x00000090
The strange thing is that I usually get this error when I assign the wrong value to a pointer, but none of these codes contain pointers (or maybe the functions contain pointers which generate this error, in which case there could be a bug in Qt's QOpenGLFunctions_3_1 class?).
What is the correct way of using the glDrawBuffer and glReadBuffer functions in Qt?
This error basically means that the functions were declared in an included header file but not correctly linked.
Correct. You have to link against OpenGL. opengl32.lib on Windows, libGL.so on *nix/Linux, -framework OpenGL on MacOS.
but none of these codes contain pointers (or maybe the functions contain pointers which generate this error, in which case there could be a bug in Qt's QOpenGLFunctions_3_1 class?).
Yes, they do. The OpenGL interface for anything that goes beyond a certain version is loaded at runtime into function pointers. This is what the QOpenGLFunctions class does. Before properly loading these pointers, they are invalid – in fact, depending on the OpenGL version your're using some may be valid and others not; that's why Qt puts the OpenGL version into the class name.
Answer the following questions:
Does the program create a OpenGL context to use?
Is the QOpenGLFunctions_… instance initialized with the context being made active on the calling thread?
Did initialization of the QOpenGLFunctions_… instance succeed?
If you'd answered any of these questions with "no" or "I don't know", that's where your problem is.

LNK2001 unresolved external symbol with CPP_XLOPER

I'm migrating a XLL from 32-Bit to 64-Bit with VS2015 and C++.
I started changing the datatypes. So I switched 'int' to '_int64'.
CPP_XLOPER Create_XLOperHeader_form_Str(const wchar_t*aBegin,_int64 strlen,bool aTranspose){
static CPP_XLOPER xlDefault(L" ");
_int64 l = strlen;
and it throws me the error:
Error
LNK2001 unresolved external symbol
"class CPP_XLOPER __cdecl Create_XLOperTable_from_Str(wchar_t const *,__int64,bool,bool)"
(?Create_XLOperTable_from_Str##YA?AVCPP_XLOPER##PEB_W_J_N2#Z)
I guess I have to modify 'class CPP_XLOPER', isn't it?
Any hint much appreciated,
thx in advance;
surplus
The method linker is complaining about has last two parameters of type bool - ...,int64,bool,bool), while the one you have modified has only one last parameter of type bool. Either you have deleted another existing method, or you have also removed one bool from the implementation (and only you know if this was or was not intentional). But yeah, in general you should fix the function declaration to match the definition.

Unresolving external symbol error

New to C++ programming I am trying to get some open source software running so I can build upon it.
I have 3 *cpp modules. One contains...
const double Qcf[6]= { 1.0, 448.831, 0.64632,0.02832, 28.317, 2.4466};
The other two modules make use of this constant and have a reference to it included in their body
extern double Qcf[];
Each of these modules then has a method/ function
i.e. something like
void routine1()
{
double i;
i = 2 * Qcf[1];
}
I have built the code and it appears that it has compiled (i.e. the *cpp goes to obj) but I get 2 error messages.
LNK2001 unresolved external symbol "double Qcf"
Each error pertains to each of the *obj files
(I also get another LNK1120 Two unresolved externals error, but I think if I solve the LNK2001 issue this will self correct).
Any advice on how to correct this is appreciated in advance.
Qcf is defined as a const double[] in one module, and declared as double[] in the other. You should declare it const too.
(or std::array<double, 6> instead of a C-style array, if your compiler supports C++11)

unresolved external symbol "void __cdecl boost::tss_cleanup_implemented(void)"

I was very surprised when I tried to search this error as I got only 4 results, two of which seem to be in Chinese. I am getting this error when compiling my project and boost sources alltogether. I searched the boost library and found this:
namespace boost
{
/*
This file is a "null" implementation of tss cleanup; it's
purpose is to to eliminate link errors in cases
where it is known that tss cleanup is not needed.
*/
void tss_cleanup_implemented(void)
{
/*
This function's sole purpose is to cause a link error in cases where
automatic tss cleanup is not implemented by Boost.Threads as a
reminder that user code is responsible for calling the necessary
functions at the appropriate times (and for implementing an a
tss_cleanup_implemented() function to eliminate the linker's
missing symbol error).
If Boost.Threads later implements automatic tss cleanup in cases
where it currently doesn't (which is the plan), the duplicate
symbol error will warn the user that their custom solution is no
longer needed and can be removed.
*/
}
}
My question is what's tss cleanup, why is it needed and how do I implement it.
I don't know about TSS, but to fix this linker error, you need to define
BOOST_THREAD_WIN32
and
BOOST_THREAD_BUILD_LIB
so boost/thread/src/win32/tss_pe.cpp implements the missing function (empty).
I assume there are few results on the Internet for this question, because the build scenario is rare. Most people do not build boost within their own project, but build boost with b2.

Class is not linked properly [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
Sorry if this question maybe asked before but I couldn't find a proper answer for it.
I am trying to use my own class file inside another class which was already written by different people. The whole program is based on Omnet++. Before building everything seems to be alright but as soon as I build the program gives me an error of
undefined reference to `RatePrediction::RatePrediction(double, double, double)
I am trying to use RatePrediction in CentralEntity and this is what I've done:
In RatePrediction.cc :
#include "CentralEntity.h"
RatePrediction::RatePrediction(double gs,double gi, double dataRate):
gs(gs), gi_int(gi), reqDataRate(dataRate), result(0), error(0){
//some codes
}
In CentralEntity.h :
#include "RatePrediction.h"
class CentralEntity : public cAsyncModule
{
//friend classes ....
protected:
virtual void initialize();
}
In CentralEntity.cc:
#include "RatePrediction.h"
void CentralEntity::initialize()
{
RatePrediction RateObj(1.1e-13,1.2-13,1e4);
}
EDIT:
#hildensia Thanks for your comment. I actually did this and now it works. But I am not sure if it can be reliable and stable or not.
I edited the Makefile manually and I added:
OBJS= RatePrediction.o
RatePrediction.o: RatePrediction.cc
$(CXX) -c $(COPTS) RatePrediction.cc
The problem lies within the compilation. RatePrediction.cc seems to be not compiled at all. Thus the linker can't find the Constructor, because there is no object file with it. Your solution in the Makefile seems to be appropriate for now, but will probably be overwritten by your build system. So you should gather information about how you can define which files should be build by Eclipse.
You should define the construction function of Class RatePrediction in RatePrediction.h file.
and it may be better to add such lines "#ifndef *_H #define *_H #endif" in every *.h files.