I have followed some instructions to construct Visual studio code C/C++ compile and debug environment. But MSVC compiler can only compile the selected cpp file, so the included .h file associated the cpp file can not compiled. then the terminal shows
*
main.obj : LNK2019 error: reference to an unresolved external
character "int __cdecl func(void)" (?func##YAHXZ) in the main
function.C:\TESTmsvc\main.exe : fatal error LNK1120: unresolved
external elements: 1
The code as below:
the a.h file
int func();
the a.cpp file
#include <iostream>
#include "a.h"
using namespace std;
int func(){
return 111;
}
the main.cpp file
#include "a.h"
using namespace std;
int main()
{
int b = func();
cout << b << endl;
}
Related
ConsoleApplication1.cpp
#include <iostream>
#include "Test.h"
#include "Head1.h"
int main()
{
std::cout << "Hello World!\n";
}
Test.h
#pragma once
int AAA = 1;
Head1.h
#pragma once
#include "Test.h"
Head2.h
#pragma once
class Head2C
{
public:
void Print();
};
Head2.cpp
#include "Head2.h"
#include "Head1.h"
#include <iostream>
using namespace std;
void Head2C::Print()
{
cout << "Head2::Print() " << endl;
}
Why #pragma once can't work for this sample code?
The linker error : error LNK2005: "int AAA" (?AAA##3HA) already defined in ConsoleApplication1.obj
Every non-constant variable should be defined only once in the entire program. #pragma once only ensures that it is defined once for each translation unit (cpp file). With two translation units, you are defining the variable twice in the entire program.
Make AAA constant or move the definition into a cpp file.
I've been struggling with this error on a more complex solution for days, so I simplified a test project to see if I can't figure it out and it still gets the same error:
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1> Test.cpp
1> MainThing.cpp
1> main.cpp
1> Generating Code...
1>MainThing.obj : error LNK2019: unresolved external symbol "public: static int __cdecl Test::add(int)" (?add#Test##SAHH#Z) referenced in function "public: void __thiscall MainThing::run(void)" (?run#MainThing##QAEXXZ)
1>P:\Leif's Documents\Visual Studio 2013\Projects\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
MainThing.h
#pragma once
class MainThing
{
public:
MainThing();
~MainThing();
void run();
private:
int result;
};
MainThing.cpp
#include "MainThing.h"
#include "Test.h"
#include <cstdlib>
#include <iostream>
MainThing::MainThing()
{
}
MainThing::~MainThing()
{
}
void MainThing::run() {
int result = Test::add(5);
std::cout << result;
}
Test.h
#pragma once
class Test
{
public:
static int add(int a);
};
Test.cpp
#include "Test.h"
int add(int a){
int b = a;
int c = 5;
int d = b + c;
return d;
}
main.cpp
#include <iostream>
#include "MainThing.h"
int main(int argc, char** argv){
MainThing mainGame;
mainGame.run();
return 0;
}
This has been super frustrating and the fact that it even effects a fairly simple recreation test without any external dependencies makes it that much more so.
I think you just have a typo. Test.cpp should be
#include "Test.h"
int Test::add(int a){
int b = a;
int c = 5;
int d = b + c;
return d;
}
The way you have, you've written a free function that other things in Test.cpp could use, but no one else knows about. The compiler is looking for Test's method called add and isn't finding one.
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 7 years ago.
I know this problem is well known, but none of the solutions work for me. I know a popular cause of this error is the compiler can't find the definition of a function in any of the source files, but I have defined the function them.
I am using Visual studio 2015 community.
Form.h
#pragma once
template<typename T>
class Form
{
public:
void GenerateForm(T i);
};
Form.cpp
#include "stdafx.h"
#include "Form.h"
template<typename T>
void Form<T>::GenerateForm(T i)
{
std::cout << i << endl;
}
Main.cpp
#include "stdafx.h"
#include "Form.h"
int main()
{
Form<int> f;
f.GenerateForm(12);
return 0;
}
Error:
PrimeForm.obj : error LNK2019: unresolved external symbol "public: void __thiscall Formula<double>::GenerateForm(int)" (?GenerateForm#?$Formula#N##QAEXH#Z) referenced in function _main
C:\Users\John\Dropbox\Visual Studio 2015\PrimeForm\Debug\PrimeForm.exe : fatal error LNK1120: 1 unresolved externals
When you try to compile form.cpp the compiler doesn't know what type T will be. Therefore it won't be able to compile this as an object file to be linked with your compiled main.cpp object file.
You'll need to include all of the declarations and definitions of a templated class to the files that need it (in this case your main.cpp file).
This can be simply done as follows:
Form.h
#pragma once
template<typename T>
class Form
{
public:
void GenerateForm(T i);
};
#include "Form.template" /* Note include Form.template here */
Form.template
#include "stdafx.h"
/* Don't include form.h in this file */
template<typename T>
void Form<T>::GenerateForm(T i)
{
std::cout << i << std::endl;
}
main.cpp
#include "stdafx.h"
#include "Form.h" /* including Form.h will include Form.template as well */
int main()
{
Form<int> f; /* Compiler now knows to instantiate a Form class with type Int as the template parameter */
f.GenerateForm(12);
return 0;
}
Note the main difference is that you don't include "Form.h" in Form.template but include "Form.template" at the bottom of Form.h
It is better practice to use the ".template" file ending for templated class implementation files.
This is my first time having separate files and first time writing a header file, however I keep getting the same error I can't fix. Here are the files:
//main.cpp
#include <iostream>
#include "Bike.h"
/*
class Bike{
public:
int tyreDiameter;
int getTyreDi(){
return tyreDiameter;
}
}; */
int main(){
Bike b;
b.tyreDiameter = 50;
std::cout << b.getTyreDi();
while (1){
continue;
}
return 0;
}
//Bike.cpp
class Bike{
public:
int tyreDiameter;
int getTyreDi(void){
return tyreDiameter;
}
};
//Bike.h
#ifndef BIKE_H
#define BIKE_H
class Bike{
public:
int tyreDiameter;
int getTyreDi(void);
};
#endif
Now if I have only one file and use the class that is commented out in main.cpp everything works fine. But as soon as I try to separate the Bike class into another cpp file I get this error:
Error 1 error LNK2019: unresolved external symbol "public: int
__thiscall Bike::getTyreDi(void)" (?getTyreDi#Bike##QAEHXZ)
Error 2 error LNK1120: 1 unresolved externals
I am using Microsoft Visual Studio 2013.
Any help would be much appreciated
Why are you defining class Bike twice? in the cpp and in the h, the correct way would be this:
header
//Bike.h
#ifndef BIKE_H
#define BIKE_H
class Bike{
public:
int tyreDiameter;
int getTyreDi(void);
};
#endif
cpp
//Bike.cpp
#include "Bike.h"
int Bike::getTyreDi(void)
{
//implementation like return tyreDiameter;
}
I am trying to create a solution which one project is the .exe and the other project is a simple dll. What i am trying to learn is how to link between two projects. I have searched stack-overflow and found really nice answers which I have followed, such as declaring the right header bath on:
Properties->Configuration Properties->C/C++->General->Additional Include Directories
Then setting the .lib on:
Properties->Configuration Properties->Linker->Input->Additional Dependencies
I used macros to generate that .lib file also. Here is the my simplified code:
The .exe:
cpp:
#include "stdafx.h"
#include "../ConsoleApplication2/HelloWorld.h"
int _tmain(int argc, _TCHAR* argv[])
{
hello_world hw;
hw.printHello();
getchar();
return 0;
}
The dll:
header:
#pragma once
#ifdef is_hello_world_dll
#define hello_world_exp __declspec(dllexport)
#else
#define hello_world_exp __declspec(dllimport)
#endif
class hello_world_exp hello_world
{
public:
hello_world();
~hello_world();
void printHello();
};
cpp:
#include "stdafx.h"
#include "HelloWorld.h"
#include <iostream>
hello_world::hello_world()
{
}
hello_world::~hello_world()
{
}
void printHello()
{
std::cout << "Hello World" << std::endl;
}
A note: The solution compiles fine when I don't call hw.printHello(); however when I do call it, the linker generates :
Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall hello_world::printHello(void)" (__imp_?printHello#hello_world##QAEXXZ) referenced in function _wmain C:\Users\usteinfeld\Desktop\Private\Students\Yana\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.obj ConsoleApplication1
This function is defined as a free function based on how you wrote it
void printHello()
It belongs to the class hello_world so you should scope it as such
void hello_world::printHello()
{
std::cout << "Hello World" << std::endl;
}