I'm getting
error LNK2005: "bool __cdecl hasLogin(void)" (?hasLogin##YA_NXZ) already defined in B.obj
error. func is declared in B.cpp file, and it's called from A.cpp file. B.cpp is included in A.cpp. Google says that it's not good to include cpp in antother cpp. If I copy/paste func in A.cpp there's no problem. How to solve tsis problem correctly?
PS I'm new to c++, so forgive if the question is dump.
Create a header file named B.h and add the following function declaration:
#ifndef _B_HEADER_
#define _B_HEADER_
bool hasLogin(void);
#endif
Instead of #include "B.cpp" in A.cpp use #include "B.h".
You should generally declare your classes/functions in .h (header) files, and implement them in .cpp files. It's also usually a good idea to put include guards in your .h files so including them multiple times is not a problem. That way your .cpp files can include any .h file without causing any conflicts.
An example; test.h
#ifndef TEST_H
#define TEST_H
class test {
public:
void bop();
}
bool hasLogin();
#endif
and the corresponding .cpp file
#include <iostream>
#include "test.h"
void test::bop() {
std::cout << "bop" << std::endl;
}
bool hasLogin() {
return false;
}
write #pragma once in the .cpp//.h where the error is coming, sometimes it avoids the error.
Related
I am having a problem when compiling: Multiple definitions of "myFunction()"
I will greatly simplify the problem here. Basically, I have 3 files: "main", "header", and "myLibrary".
main.cpp
#include "header.hpp"
int main() { }
header.hpp
#ifndef HEADER_HPP
#define HEADER_HPP
#include "myLibrary.hpp"
// ...
#endif
header.cpp
#include "header.hpp"
// ...
myLibrary.hpp
#ifndef LIB_HPP
#define LIB_HPP
#if defined(__unix__)
#include <dlfcn.h>
std::string myFunction() { return std::string(); }
#endif
#endif
myLibrary.cpp
#include "myLibrary.hpp"
//...
So, why does the compiler say that I have Multiple definitions of "myFunction()"?
One clue I found: When I take header.cpp and erase the line that says #include "header.hpp", the program compiles without complaining. On the other hand, if I erase myFunction (from myLibrary.hpp) instead, the program also compiles without complains
You are defining the body of the function inside the header file. So every translation unit that you include that header in (in this case, main.cpp and header.cpp) will end up with its own copy of that function body. And when you try to link those multiple units together, you get the "duplicate definition" error.
The function needs to be declared in the hpp file, and defined in the cpp file:
myLibrary.hpp
#ifndef LIB_HPP
#define LIB_HPP
#if defined(__unix__)
#include <dlfcn.h>
#include <string>
std::string myFunction();
#endif
#endif
myLibrary.cpp
#include "myLibrary.hpp"
#if defined(__unix__)
std::string myFunction()
{
return std::string();
}
#endif
//...
Include guards only prevent the same header from being included twice within the same translation unit, which in practice is usually a single .cpp file. For example, it prevents errors when doing this:
#include "header.h"
#include "header.h"
int main()
{
}
But, more generally, it means that it doesn't matter if you've include a header that has already been included as a dependency of another header.
However, if you have two .cpp files include the same header, and that header contains the definition of a function (such as your myLibrary.hpp) then each .cpp file will have its own definition (the include guard won't help because the header is being included in two separate translation units / .cpp files).
The simplest thing to do is to declare the function in the header, which tells every file that includes your header that the function exists somewhere, and then define it in the .cpp file so that it is only defined once.
You should to define functions in the .cpp files, not in the header files. You declare them in the header files. What you're doing is defining it in the header file, so when it gets included into multiple files, the function is getting duplicated. Cross-file duplicate symbols will throw an error, unless they're static.
myLibrary.cpp:
#include "myLibrary.hpp"
#ifdef(__unix__)
std::string myFunction() { return std::string(); }
#endif
myLibrary.hpp:
#ifndef LIB_HPP
#define LIB_HPP
#if defined(__unix__)
#include <dlfcn.h>
#include <string>
std::string myFunction();
#endif
#endif
I have declared a global variable in header.h and included that header in source.cpp and main.cpp but linker is giving error
Source.obj : error LNK2005: "int globalVariable" (?globalVariable##3HA) already defined in Main.obj
GlobalVariableAndLinkageIssue.exe fatal error LNK1169: one or more multiply defined symbols found
header.h
int globalVariable;
source.cpp
#include "header.h"
main.cpp
#include"header.h"
void main() {}
Move the declaration to a .cpp file. You can use a declaration in a header file by using:
extern int globalVariable; // declaration in header - can have as many as you need
But the .cpp file should have the definition:
int globalVariable; // definition in .cpp - only need one across all your files
C and C++ use textual pre-processor to include headers, this is basically a text insertion, not a smart module system as in some languages. By including it as you were, you are creating multiple definitions, one per .cpp file.
As a matter of good practice, you need to get used to using include guards to protect against multiple nested includes (though it would not solve your current issue). If using Visual C++, you can use #pragma once or to use a portable solution wrap your header code in:
#ifndef _INCLUDE_FOO_H_
#endif
To create a global variable, you should do the following. Note that in the header, we mark the variable as extern, and we actually create the object in a cpp file.
header.h
extern int globalVariable;
header.cpp
#include "header.h"
int globalVariable;
main.cpp
#include "header.h"
int main() {}
Put global variable in some .c or .cpp file, so that it can be defined only once and refer in header file using extern
for example,
header.h
extern int globalVariable;
header.cpp
int globalVariable = 0;
source.cpp
#include "header.h"
main.cpp
#include"header.h"
int main() {
return 0;
}
Because BOTH sources #include your header, and thus it is DEFINED twice.
In such situation,it is common to use some #define as follows:
//header.h
#ifdef DEFINE_VARS
#define DEFINE_OR_DECLARE
#else
#define DEFINE_OR_DECLARE extern
#endif
DEFINE_OR_DECLARE int globalVariable;
//main.cpp
#define DEFINE_VARS
#include "header.h"
...
//header.cpp
#include "header.h"
...
I can't understand something that causes LNK2005 errors that I've encountered.
Assume that you have a class A:
File A.h:
#ifndef A_H
#define A_H
class A{
public:
static foo(void);
private:
static bool m_someVar;
};
bool A::m_someVar = false;
#endif
File A.cpp:
#include "A.h"
void A::foo(){
m_someVar = true;
}
The code above causes a LNK 2005 however the following code doesn't:
File A.h:
#ifndef A_H
#define A_H
class A{
public:
static foo(void);
private:
static bool m_someVar;
};
#endif
File A.cpp:
#include "A.h"
bool A::m_someVar = false;
void A::foo(){
m_someVar = true;
}
Can someone explain why is this happening even though I have include guards? Should I also add #pragma once?
Thanks in advance.
EDIT: here's the compilation error:
"error LNK2005: "private: static bool GameManager::m_isGameOver" (?m_isGameOver#GameManager##0_NA) already defined in Execution.obj"
include guards(both #ifndef and #pragma) doesn't work across compilation units, which is one of the reasons you should never ever ever define anything in a header file, only declare them. Except for templates of course.
A compilation unit is one .cpp-file and all included headers. Each .cpp create an object file containing a middle stage binary representation of the code, this is the compilation stage. These object files are then linked together in the linking stage. Since each .cpp is handled separately in c++ if you have "float foo;" in header.hpp and both a.cpp and b.cpp include header.hpp, how would the compiler know which foo you mean when running the application?
A "static" variable inside a class declaration is actually a declaration for an extern variable that is in the scope of that class. Like every extern variable, it needs a definition in exactly one source file. Otherwise, the linker will complain about it.
You probably include the .h file from more than one source file, so you have more than one definition.
I have a test class declared in a header file and defined in a separate file. The class must compile differently under Windows, so i use #if defined ( _WINDOWS_ ). When I compile the cpp file which also contains #if defined ( _WINDOWS_ ), the file is compiled as if the symbol _WINDOWS_ was not defined, although it is defined in another file. When I compile the code, I am getting the following error:
Error Code : error lnk2019 unresolved external symbol public
source code
// test.h
class Test
{
public:
#if defined (_WINDOWS_)
void printwindow();
#endif
void notwindows();
};
//test.cpp
#include "test.h"
#if defined (_WINDOWS_)
void Test::printwindow()
{
cout << "i am windows ";
}
#endif
void test::notwindows()
{
cout << " not windows " ;
}
//main.cpp
#include "windows.h"
#include "test.h"
void main()
{
test t1 ;
t1.printwindow() // OK I have declared function so my _WINDOWS_ is available but when i run it i get
}
Error Code : error lnk2019 unresolved external symbol public
NOTE : if i define the function directly it works without any problem
// test.h
class Test
{
public:
#if defined (_WINDOWS_)
void printwindow(){couT << "i am window" }
#endif
void notwindows();
};
but I don't like this method. I prefer to define them in separate files (h and cpp).
It would be better for you to use _WIN32 instead of _WINDOWS_ for your conditional compilation test. _WINDOWS_ is defined only if windows.h has been included, while the compiler automatically defines _WIN32 for any build for a Windows target, regardless of what headers are included. In your situation, _WINDOWS_ is defined when you compile main.cpp but not when you compile test.cpp becuase test.cpp doesn't include windows.h.
Also, the _WINDOWS_ macro definition is an implementation detail of the windows.h header, and is not guaranteed to be used. For example, the MinGW version of windows.h does not define _WINDOWS_.
You need to include windows.h in your test.cpp file
When you compile test.cpp it does not have the _WINDOWS_ symbol defined. So it creates an object file without the windows function.
Then in test.hpp it uses the symbol because you included windows.h in main.cpp
If you change the order of the includes it will behave differentially. You should rethink the way how you want to select the windows version which does not depend on the order of includes (for example see the other answer about _WIN32_).
You can not change the code in an already compiled object file (test.cpp) using some define in another code file (main.cpp)
I had a very weird problem with precompile header. The linker generates LNK2019: unresolved external symbol error when I implement method in .cpp file. However, the program could be compiled if I implement method in .h file. I happened to find out a solution but I have no idea about the root cause of this error.
My project structure looks like this
Solution
->project 1
->project 2
Project 1 has 3 files. A.h, A.cpp and stdafx.h
file A.h
#pragma once
class A
{
public:
int num;
A();
};
file A.cpp
#include "stdafx.h"
A::A()
{
num = 2;
}
file stdafx.h
...
#include "A.h"
...
In project 2. I want to use A class.
file whatever.cpp
#include "stdafx.h"
#include "../Project1/A.h"
...
A* a_obj = new A();
...
In compile time, the linker reports unresolved external symbol error for the A construction function. If I implement the constructor in A.h file. the project2 could be successful complied. I want to know, why can not put the implementation in A.cpp file? What is the right way to organize precompile header?
Thank you
Project 2 does not include the definition of the A constructor - one way to give it visibility of this is to include the definition in the header file (which you did).
Another way would be to include the A.cpp file in project 2.
A third way would be to export the A class, or the A constructor using a .def file or using the dllexport directive.
Put this in the precompiled header file:
// set up export import macros for client project use
// define the symbol "PROJ1" before including this file in project 1 only
// leave it undefined for other projects
#ifdef PROJ1
#define DLLEXP __declspec(dllexport)
#else
#define DLLEXP __declspec(dllimport)
#endif
Then declare the A class in the A header:
DLLEXP class A
{
public:
A();
...
};
Or:
class A
{
public:
DLLEXP A();
...
};