I am holily convinced that this is an extremely basic thing that every living human being is perfectly able to perform, yet somehow it's not working for me. The FAQ and many other threads/questions found through Google have not fixed this for me.
The situation is as follows: I have one Visual Studio 2010 project. This project is located in a Visual Studio 2010 solution. The project has the following files:
add.h:
class Adder {
public:
static int add(int i1, int i2);
};
add.cpp:
class Adder {
public:
static int add(int i1, int i2) {
return i1 + i2;
}
};
main.cpp:
#include "add.h"
int main() {
Adder::add(5, 6);
return 0;
}
When attempting to run this code, which looks fairly basic to me, I get the following linker error:
main.obj : error LNK2019: unresolved external symbol "public: static int __cdecl Adder::add(int,int)" (?add#Adder##SAHHH#Z) referenced in function _main
From what I could gather, this implies that the linker could not find the implementation for Adder::add(). However, the files are in exactly the same project, the generated .obj files are in exactly the same folder, and every single tutorial I've read on the subject of classes and header files tells me that this should work.
I've tried the following potential solutions thus far:
Add the project's own folder to additional includes/dependencies/sources/whatevers
Add extern identifiers to various things
Use a seperate namespace
Various other small adjustments in the hope they would work
So I guess my problem boils down to: How can I get the linker to notice the add.cpp file? Any help is appreciated, I've spent several hours on this now without success, while this seems like an extremely basic thing to me.
The following defines a whole new class
//add.cpp:
class Adder {
public:
static int add(int i1, int i2) {
return i1 + i2;
}
};
different from the one in the header, available only to this translation unit ("add.cpp").
What it should contain is:
//add.cpp
#include "add.h"
int Adder::add(int i1, int i2) {
return i1 + i2;
}
This implements the method defined in the class from the header add.h.
Related
I have a Visual C++ solution with 2 projects: rectangle and project3.
In rectangle project I have rect.cpp and rect.h.
rect.h
#ifndef rect_h
#define rect_h
class Rect
{
public:
Rect();
int m_h;
int m_w;
};
#endif //rect_h
rect.cpp
#include "rect.h"
Rect::Rect()
{
m_h = 1;
m_w = 5;
}
whenever I try to create rect object from the rectangle project it succeeds.
But when I try to do the same from the project3 it produces a linker error.
LNK2019: unresolved external symbol "public: __thiscall
Rect::Rect(void)" (??0Rect##QAE#XZ) referenced in function _main
1>C:\Users\mbaro\documents\visual studio
2017\Projects\Project2\Debug\Project3.exe : fatal error LNK1120: 1
unresolved externals
main.cpp (in project 3)
#include "rect.h"
using namespace std;
int main()
{
Rect* a = new Rect();
return 0;
}
I kind of feel that class definition is picked up successfully, but the linker can not link the constructor code from rect.cpp.
What is the problem and how to solve it?
Thanks.
The error is normal: you told the compiler where it could find the .h files, but you did not tell the linker where it could find the .obj files.
It may depend on the exact VS version, but in Project/Properties, you should find Linker/Input and there Additional dependencies. If you only need one or two object files (xxx.obj) from the other project, add them here. That way, you avoid code duplication, which will be a nightmare for future maintenance...
If you have many common files, you should considere to put them in an auxilliary project that would build a (static)library in the same solution, and then link the library in both project (and of course give access to header files of the library project for the other projects using the library).
I have already started writing a long, long answer. Then i realized, what You may be missing is that despite Your class is named "Person" the header file You should have added is named "rect.h".
Also Your constructor cannot have a declaration of values in the header file (EDIT:not true, I was mistaken). In the header file, try using:
Person(int h, int w);
You declare what will be needed, not what You already have. If You want those to be specifically what You wrote the constructor should be:
Person();
in .h
and
Person::Person()
{
m_h = 1;
m_w = 5;
}
in .cpp.
If You need more detailed description of using include, I have already written a big part of it, so don't hesitate to ask.
I have read this informative stackoverflow question regarding unresolved external symbols, but I am still not sure how to solve my issue.
Within Visual Studio 2012, I have a solution consisting of multiple projects, one of which is a static library called common. Each project that produces an executable consists of a header and associated cpp file of all global functions used throughout that specific program, called programglobals. In the process of developing one of these projects, I started duplicating some of this code from one project's programglobals to another. Now that I have completed all the projects, I want to extract the duplicate code into a associated header and cpp file within the common library, but I believe I might be referencing them incorrectly, which is producing these unresolved external symbol errors
Here is a dumbed down example of what I am currently attempting.
Common Library Files
//solutionglobals.h
void commonFunction();
//solutionglobals.cpp
void commonFunction() {
int asdf;
}
Project A Files
// programglobals.h
#include "../common/solutionglobals.h
void functionUsedInProjectA();
// programglobals.cpp
void functionUsedInProjectA() {
int x;
}
// output.h
#include "programglobals.h"
void asdfA();
// output.cpp
void asdfA() {
int x;
functionUsedInProjectA();
commonFunction();
}
Project B Files
// programglobals.h
#include "../common/solutionglobals.h
void functionUsedInProjectB();
// programglobals.cpp
void functionUsedInProjectB() {
int x;
}
// output.h
#include "programglobals.h"
void asdfB();
// output.cpp
void asdfB() {
int x;
functionUsedInProjectB();
commonFunction();
}
Any reference to commonFunction() results in an unresolved external symbol error.
Thanks!
You will have to specify in your executable projects that they reference the static lib. May be http://msdn.microsoft.com/en-us/library/vstudio/ms235627%28v=vs.110%29.aspx#uselibinapp helps (lower third of the article).
Before you can use the math routines in the static library, you must
reference it. To do this, open the shortcut menu for the MyExecRefsLib
project in Solution Explorer, and then choose References. In the
MyExecRefsLib Property Pages dialog box, expand the Common Properties
node, select Framework and References, and then choose the Add New
Reference button.
The linker cannot 'see' your function and as such thinks that it does not have an external symbol referencing it, hence the error.
You must #pragma comment(lib, [library here]) to reference the external function.
The following code can be used to reproduce this error:
[header file- test.h]:
#include "StdAfx.h"
void someOtherFunction();
void someFunction(string thisVar);
[code file- test.cpp]:
#include "StdAfx.h"
#include "test.h"
void someOtherFunction()
{
printf("Hello World!");
}
[function body for someFunction(string thisVar) is missing!]
I am trying to build a dll to go along with my program in c++. The dll will be a basic library with a bunch of inheritable classes and general utilities, which can then be used dynamically by multiple other applications that will accompany the final product(A Game). I threw something together, only to find that I am recieving I maddening error. It is the famous "error LNK2019: unresolved external symbol". First off, I already found the solution to this, and here is the link: Unresolved External Symbol- Error in guide?
The chosen answer works. If I put my class into only a header file, it compiles perfectly fine, runs, and is all nice and pretty. However, I want to keep the declaration and implementation separate, and if I try to move the implementation to a separate cpp I receive the LNK2019 error. This is due to some kind of inlining, I just want to know how I can bloody fix it before I start tearing my hair out.
Can anyone help me with this? Here is my class:
Header:
#ifndef MYDLLTESTCLASS_H
#define MYDLLTESTCLASS_H
class __declspec( dllexport ) MyDLLTestClass {
public:
MyDLLTestClass();
void setX( int x );
int getX();
private:
int x;
};
#endif // MYDLLTESTCLASS_H
CPP:
#include "MyDLLTestClass.h"
MyDLLTestClass::MyDLLTestClass() {
}
void MyDLLTestClass::setX( int x ) {
this->x = x;
}
int MyDLLTestClass::getX() {
return x;
}
Separate like above, the code wont compile. But if I throw the declaration and implementation together It works, so if I do this:
#ifndef MYDLLTESTCLASS_H
#define MYDLLTESTCLASS_H
class __declspec( dllexport ) MyDLLTestClass {
public:
MyDLLTestClass();
void setX( int x );
int getX();
private:
int x;
};
MyDLLTestClass::MyDLLTestClass() {
}
void MyDLLTestClass::setX( int x ) {
this->x = x;
}
int MyDLLTestClass::getX() {
return x;
}
#endif // MYDLLTESTCLASS_H
It will work. Again, I WANT the declaration and implementation separate.
Here is the build report when I use separate declaration and implementation.
http://pastebin.com/HMEpeEgn
This was actually far easier to solve than I realized; it turns out the it was a visual studio setting all along. First, you need to add the dll project to your current project(Which will be using it). Then you need to right click on you current project and Follow these two pictures:
And then add your dll, which might just need to be check-marked(Mine was simply missing the checkmark).
After that your project will compile perfectly fine! No need for nasty-ass #ifndef macros like every webpage has attempted to portray(which also didn't work mind you)
I keep getting an ununderstood "unresolved externals error from C++ from Visual Studio 2013. Thank you very much for your help so far. I have reduced the code even more to the following form (but the Problem persists):
main.cpp:
#include "Fibonacci.h"
using namespace std;
int main(void){
int RandInteger = 3;
Fibonacci Fib(RandInteger);
}
Fibonacci.h
class Fibonacci{
public:
Fibonacci(int n=0);
protected:
int m_n0, m_n1, m_n;
};
Fibonacci.cpp:
#include "Fibonacci.h"
Fibonacci::Fibonacci(int n){
m_n0 = 0;
m_n1 = 1;
m_n = n;
}
This code produces the following error in Visual Studio 2013:
Error 2 error LNK1120: 1 unresolved externals C:\Dropbox\todo\c++\Exam\Ex2\doesn't work\Exercise 2\fibo1\Fibo1\Debug\Fibo1.exe Fibo1
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Fibonacci::Fibonacci(int)" (??0Fibonacci##QAE#H#Z) referenced in function _main C:\Dropbox\todo\c++\Exam\Ex2\doesn't work\Exercise 2\fibo1\Fibo1\main.obj Fibo1
I persists, but as soon as I replace the line in main.cpp with
Fibonacci Fib();
i.e. I do not pass the integer to the constructor, everything works (well it compiles an does nothing as expected).
Thanks for your help! I am really still stuck.
I finally found my error. Turns out that there is really nothing wrong with the code itself, but I've somehow destroyed my VisualStudio project. I am really not an expert for these things, but here is what worked for me:
create a new empty project in Visual Studio
copy your cpp files (all of them also the *.h files) into the new project folder
add them to this new project by right clicking the project and using "add New item"
I know this is the straightforward way to do it, but I cannot see how I broke the old project (it should be simple enough after all)
Thanks to all of you - thanks especially to otisonoza and Angew, for setting me on the right track that there is nothing wrong with the code (which works on their end), but with the Visual Studio project.
Your main function should return int
void main(void){
Should be
int main(){
EDIT: Thanks to otisonoza in the comments for pointing out that some compilers accept this definition of main. Other than this, I can't see any cause for compiler errors in your system. Are you sure you pasted the code exactly as you wrote it?
Also, what's up with the random tick after your definition of main?
}`
Also, you don't need to have semi-colons after each function in your .cpp file:
Fibonacci::Fibonacci(int na){
m_n0 = 0;
m_n1 = 1;
m_n = 2;
};
int Fibonacci::getNext(int FnM1, int FnM2){
return FnM1 + FnM2;
};
can be
Fibonacci::Fibonacci(int na){
m_n0 = 0;
m_n1 = 1;
m_n = 2;
}
int Fibonacci::getNext(int FnM1, int FnM2){
return FnM1 + FnM2;
}
I think I am having a similar problem to LNK2005, LNK1169 errors, "int __cdecl g(void)" (?g##YAHXZ) already defined but I cannot find the issue.
I am working with Visual Basic and I am having the following files
main.cpp
#include <iostream>
#include "functions.h"
using namespace std;
int main(){
number();
return 0;
}
I had a functions.cpp but after reading the question I linked before I renamed it for functions.h
int number(){
int i = 1;
return i;
}
Now it is displaying error LNK2005: "int __cdecl number(void)" (?number##YAHXZ) already defined in functions.obj
Is there anything wrong with the function number() in functions.h?
You are having linking issues.
Your immediate issue is that the functions.obj contains code that is being linked in. Then you redefine number() in main.cpp so they collide. Go ahead and clean the project (which should delete functions.obj, and you should be able to compile. I, however, recommend doing it this way.
functions.hpp (or functions.h)
int number();
functions.cpp
int number(){
int i = 1;
return i;
}
main.cpp
#include <iostream>
#include "functions.h"
using namespace std;
int main(){
number();
return 0;
}
When you compile, your program will create 2 objects with compiled code functions.obj, and main.obj. Since you use number in the main file, the compiler looks for the implementation of that function. Since the implementation of that function is in the functions.obj object, then you need to link that in.
If you are going to use number() across several C++ files, then you should always separate the code out into its own file and implementation.
functions.h should only declare the functio, as in
int number();
then functions.cpp should contain the function definition
int number(){
int i = 1;
return i;
}
Of course functions.cpp needs to be compiled (add it to the project).
The problem here is that you are including functions.h in multiple files. The issue could be avoided also by simply declaring the function static as in
static int number(){
int i = 1;
return i;
}
However, since it seems you are just learning, I would suggest you to study the basics of compiling c++ code.
In one of the modules you link in there is a function with the name number(). You define you own implementation so the linker does not know which one to use.
Either rename your function or use namespaces.