This question already has answers here:
Why should I not include cpp files and instead use a header?
(14 answers)
Closed 2 years ago.
Having 2 simple files like that:
Main.c:
#include "Initialization.cpp"
int main() {
return 0;
}
and Initialization.cpp:
int main2() {
return 0;
}
I'm getting en error:
..."int __cdecl main2(void)" (?main2##YAHXZ) already defined in Initialization.obj...
What's peculiar when i complied the program the first time everything was ok. After recompilation this error starts appearing.
PS. I'm using Visual Studio c++ 2019
The preprocessor copies everything in the include file into Main.c which will look
int main2() {
return 0;
}
int main() {
return 0;
}
Both Initialization.o and Main.o now have definition for
main2(). Thus, you break the one definition rule and invoke undefined behavior.
Related
This question already has answers here:
Why does const imply internal linkage in C++, when it doesn't in C?
(7 answers)
Closed 1 year ago.
I have this cpp file which contains 2 arrays:
const float arr[123]={/*..*/};
const float ave[213]={/*..*/};
And in my main.cpp I have:
int main()
{
extern float arr[123];
float temp[123];
for(unsigned i=0; i<123;i++)
temp[i]=arr[i];
return 0;
}
When I build my project I got an error saying that there’s no definition for arr.
It’s a linker error.
What could be the problem?
Your const variable has internal linkage by default.
You need to declare it as extern as well to counteract that.
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I'm making a prime generator with 3 file (two of them are .cpp and one is .h).
However when i try to build the whole project it in onlinegdb gives this error
/tmp/ccI5GCGK.o: In function `main':
main.cpp:(.text+0x11f): undefined reference to `int primegen(int&, long*, long*)'
collect2: error: ld returned 1 exit status
main.cpp
#include <iostream>
#include "primegen.h"
int main(void)
{
//taking input for number of test cases
int test_case{2};
long int lower_lim[MAX] = {5, 15}, upper_lim[MAX] = {15, 25};
//function present in primefunc.cpp
primegen(test_case,lower_lim,upper_lim);
}
primefunc.cpp
// to make SUCCESS known to this file
extern int SUCCESS;
//main function to prime generator between limits
int primegen(int &test, auto *low, auto *up)
{
static int cases=0;
if(cases == test)
return SUCCESS;
int diff=up[cases]-low[cases];
for(int i=0;i<diff;i++)
{
//some code to be added
}
}
primegen.h
// for making arrays of lower and upper limit
constexpr int MAX = 10;
constexpr int SUCCESS = 2;
// for printing out prime number
int primegen(int &, auto *, auto *);
EDIT :- I tried moving the function from the 2nd cpp to the main.cpp and it worked and also individual builds also gives success.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I am experiencing something weird with my c++ source file or perhaps the compiler itself. It seems that when I attempt to compile the file, it hits me with a message -
undefined reference to "Basic_int_stack::Basic_int_stack()
undefined reference to "Basic_int_stack::Push(int)
Here is my code (I'm still a beginner so don't expect any crazy professional code )
Header file:
class Basic_int_stack
{
public:
// This part should be implementation independent.
Basic_int_stack(); // constructor
void push( int item );
int pop();
int top();
int size();
bool empty();
private:
// This part is implementation-dependant.
static const int capacity = 10 ; // the array size
int A[capacity] ; // the array.
int top_index ; // this will index the top of the stack in the array
};
Implementations:
#include "basic_int_stack.h"// contains the declarations of the variables and functions.
Basic_int_stack::Basic_int_stack(){
// the default constructor intitializes the private variables.
top_index = -1; // top_index == -1 indicates the stack is empty.
}
void Basic_int_stack::push( int item ){
top_index = top_index + 1;
A[top_index] = item ;
}
int Basic_int_stack::top(){
return A[top_index];
}
int Basic_int_stack::pop(){
top_index = top_index - 1 ;
return A[ top_index + 1 ];
}
bool Basic_int_stack::empty(){
return top_index == -1 ;
}
int Basic_int_stack::size(){
return top_index;
}
Main Function:
#include "basic_int_stack.h"
#include <iostream>
int main()
{
int var;
Basic_int_stack s1;
while((std::cin >> var)>=0){
s1.push(var);
}
return 0;
}
This is happening because you're building your main file without building and linking your class implementation file as well. You need to adjust your build settings somehow.
It is because you don't include Basic_int_stack.cpp when you complile.
Simplely speaking, when you encounter Undefined reference to xxx, it is a error generated by linker, when means the compliler can't find the implements. So you need check if you include the cpp file or dynamic library or static library.
I faced the same problem. Finally I found a fix by including .cpp file in the main file.
#include "file_name.cpp" //In the main file
This question already has answers here:
Is there a way to get warned about unused functions?
(6 answers)
Closed 8 years ago.
I am wondering if there are any compiler flags you can set to pick up this case. Say I have the following files:
a.h
class a
{
public:
int lala(void);
int lala2(void);
};
a.cpp
#include "a.h"
int a::lala(void)
{
return 5;
}
main.cpp
#include <iostream>
#include "a.h"
int main()
{
a thi;
std::cout << thi.lala() << std::endl;
return 0;
}
The problem here is that the function lala2 is not implemented and although its not used not even a warning is issued.
So i don't know how it led to this but basically in a large portion of code there was an un-implemented function. I am just wondering if there are any compiler flags that will allow us to pick this up? Using g++ -pedantic -Wall was not enough.
The compiler can't do that. The compiler compiles source files one by one. You may have the implementation of lala in one source file and of lala2 in another. The compiler has no way of knowing whether there's a lala2 implementation somewhere else.
The linker will display an error if you try to use lala2. If you don't, the code will just work.
This question already has answers here:
Why are C++ inline functions in the header?
(8 answers)
Closed 9 years ago.
I have the following code:
IFile.h
class IFile
{
public:
IFile();
~IFile(void);
inline bool IsValidFileType() const;
};
IFile.cpp
IFile::IFile()
{
//IsValidFileType();
}
IFile::~IFile(void)
{
}
inline bool IFile::IsValidFileType() const
{
return true;
}
main.cpp
int main(int argc, char* argv[])
{
IFile* pFile = new IFile();
pFile->IsValidFileType();
return 0;
}
When compiling the code I get the following error:
error LNK2019: unresolved external symbol "public: bool __thiscall IFile::IsValidFileType(void)const " (?IsValidFileType#IFile##QBE_NXZ) referenced in function _main
If I change wither "inline" or "const" qualiferes for the function, or call it inside the constructor, the program will complile.
Can you please explain this behaviour?
How can the compiler inline a function whose code it cannot see while it is compiling? When compiling main.cpp, the compiler is being asked to do just this.
An inline function's code gets compiled into each translation unit that references it (that's the idea, after all). Meaning, you need to include the code in the header file.
The inline keyword promises to the compiler that it will be able to see the definition in each translation unit (*.cpp file) in which it is used. You break this promise, since main.cpp can't see the definition although it includes IFile.h.
Usually functions with the inline keyword should be defined in a header file, not a source file.
Since the function is inline, you have to define it in the header file, not in the cpp file.